fix: clear late owned startup tips after normal close
This commit is contained in:
1 parent
4402486baa
commit
a64636ba0d
4 files changed
+278
-6
No files matched your search
@@ -1,5 +1,6 @@
|
||||
"""Close only an idle document whose native identity was already established."""
|
||||
import json
|
||||
from contextlib import ExitStack
|
||||
import os
|
||||
from pathlib import Path
|
||||
import time
|
||||
@@ -69,16 +70,18 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
|
||||
read/write question is declined; no ownership is taken or version restored.
|
||||
Unknown windows and documents are retained. WM_CLOSE is cooperative.
|
||||
"""
|
||||
import win32api, win32con, win32gui, win32process
|
||||
import win32api, win32con, win32gui, win32process, win32event
|
||||
from .mentor_adapter import _process_details
|
||||
from .native_repair_ui import _current_desktop_name
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
from . import residual_sessions
|
||||
root = Path(job['root'])
|
||||
result = {'status': 'RETAINED', 'pcb_pid': pcb_pid}
|
||||
result['release'] = {'pcb_exited': False, 'launcher_exited': launcher_process is None,
|
||||
'safe_to_continue': False}
|
||||
if launcher_process is not None:
|
||||
result['launcher_pid'] = launcher_process.pid
|
||||
handles = ExitStack()
|
||||
try:
|
||||
desktop = job.get('desktop', '')
|
||||
if (job.get('interactive_input') or not desktop.startswith('CbbVerify_')
|
||||
@@ -93,9 +96,18 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
|
||||
declined_kind = None
|
||||
cancelled_open = False
|
||||
dismissed_tips = set()
|
||||
close_handle = None
|
||||
close_identity = None
|
||||
while True:
|
||||
records = [r for r in _process_details('ExpeditionPCB.exe') if r[0] == pcb_pid]
|
||||
if not records:
|
||||
if (close_handle is not None
|
||||
and win32event.WaitForSingleObject(close_handle, 0) != win32con.WAIT_OBJECT_0):
|
||||
result['detail'] = 'STARTUP_EXIT_UNCONFIRMED: 原进程句柄尚未确认退出'
|
||||
if time.monotonic() >= deadline:
|
||||
break
|
||||
time.sleep(.2)
|
||||
continue
|
||||
if declined_hwnd is not None:
|
||||
cancelled_open = True
|
||||
result['cancelled_prompt'] = declined_kind
|
||||
@@ -200,15 +212,32 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
|
||||
win32gui.EnumChildWindows(main[0], child, None)
|
||||
addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])
|
||||
if len(main) == 1 and not unknown and not documents and (cancelled_open or not addresses):
|
||||
close_handle = handles.enter_context(residual_sessions._process_handle(pcb_pid))
|
||||
close_identity = residual_sessions._handle_identity(close_handle)
|
||||
if Path(close_identity['image']).name.casefold() != 'expeditionpcb.exe':
|
||||
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
|
||||
win32gui.PostMessage(main[0], win32con.WM_CLOSE, 0, 0)
|
||||
sent = True
|
||||
emit(stage='startup_close_requested', pcb_pid=pcb_pid, hwnd=main[0])
|
||||
# Observe exit even when the caller requested no extra wait.
|
||||
continue
|
||||
result['detail'] = 'STARTUP_IDLE_UNCONFIRMED: 等待空闲窗口和数据库释放'
|
||||
# After the single owned WM_CLOSE, WMI may lose the executable/
|
||||
# command fields before removing the PID. Only observe exit here;
|
||||
# never issue another close or treat missing fields as release.
|
||||
# WMI fields can disappear during teardown. The held identity binds
|
||||
# any late Tip action to the original process, never a reused PID.
|
||||
if sent and time.monotonic() < deadline:
|
||||
if _current_desktop_name() != desktop:
|
||||
raise ValueError('STARTUP_DESKTOP_UNCONFIRMED')
|
||||
snapshot = residual_sessions._process_windows({pcb_pid})
|
||||
windows = snapshot.get(pcb_pid, [])
|
||||
if all(w.get('desktop') == desktop for w in windows):
|
||||
dismissed = residual_sessions._dismiss_startup_tip(
|
||||
dict(pid=pcb_pid, name='ExpeditionPCB.exe', scope_confirmed=True,
|
||||
**close_identity), close_handle, windows,
|
||||
incomplete=residual_sessions._window_error(snapshot, pcb_pid), dismissed=dismissed_tips)
|
||||
if dismissed is not None:
|
||||
dismissed_tips.add(dismissed)
|
||||
emit(stage='startup_tip_dismissed', pcb_pid=pcb_pid, hwnd=dismissed)
|
||||
continue
|
||||
if time.monotonic() >= deadline:
|
||||
if sent:
|
||||
result['detail'] = 'STARTUP_EXIT_UNCONFIRMED: 已请求正常关闭,但 PCB 尚未退出'
|
||||
@@ -216,6 +245,8 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
|
||||
time.sleep(.2)
|
||||
except Exception as error:
|
||||
result['detail'] = str(error)
|
||||
finally:
|
||||
handles.close()
|
||||
(root/'startup-close.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
emit(stage='startup_close_result', **result)
|
||||
return result
|
||||
|
||||
Reference in new issue
Block a user