fix: retain unknown startup windows and expose native dispatch errors
This commit is contained in:
1 parent
46c3e72364
commit
ffa7e84d43
4 files changed
+154
-8
No files matched your search
@@ -103,7 +103,14 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
|
||||
and win32gui.GetWindowText(w).strip() in (
|
||||
'Expedition PCB', 'Expedition PCB - Reusable Block')
|
||||
and win32gui.IsWindowEnabled(w)]
|
||||
dialogs = any(win32gui.GetClassName(w) == '#32770' for w in windows)
|
||||
observed = [{'hwnd': w, 'class': win32gui.GetClassName(w),
|
||||
'title': win32gui.GetWindowText(w), 'enabled': bool(win32gui.IsWindowEnabled(w))}
|
||||
for w in windows]
|
||||
benign = [w for w in observed if w['title'] == '' and w['class'] in (
|
||||
'UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator')]
|
||||
unknown = [w for w in observed if w['hwnd'] not in main and w not in benign]
|
||||
result.update(owned_windows=observed, unknown_windows=unknown,
|
||||
benign_auxiliary_windows=benign)
|
||||
documents = []
|
||||
def child(hwnd, _):
|
||||
if (win32gui.GetClassName(hwnd) == 'MDIClient'
|
||||
@@ -112,7 +119,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
|
||||
if len(main) == 1:
|
||||
win32gui.EnumChildWindows(main[0], child, None)
|
||||
addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])
|
||||
if len(main) == 1 and not dialogs and not documents and not addresses:
|
||||
if len(main) == 1 and not unknown and not documents and not addresses:
|
||||
win32gui.PostMessage(main[0], win32con.WM_CLOSE, 0, 0)
|
||||
sent = True
|
||||
emit(stage='startup_close_requested', pcb_pid=pcb_pid, hwnd=main[0])
|
||||
|
||||
@@ -218,23 +218,48 @@ def _close_probe_dialogs(desktop, owned_pids):
|
||||
return # Never close a parent with an unreleased child.
|
||||
|
||||
|
||||
def _probe_add_dialog(app, job, desktop, owned_pids, root):
|
||||
def _exception_details(error):
|
||||
import traceback
|
||||
return {'type': f'{type(error).__module__}.{type(error).__name__}', 'message': str(error),
|
||||
'args': json.loads(json.dumps(error.args, default=str)),
|
||||
'hresult': getattr(error, 'hresult', None),
|
||||
'traceback': ''.join(traceback.format_exception(type(error), error, error.__traceback__))}
|
||||
|
||||
|
||||
def _dispatch_diagnostics(root, stage, app, library, *, paths=None):
|
||||
"""Read only three known DISPIDs, preserving errors hidden by dynamic COM."""
|
||||
record = {'stage': stage, 'members': {}}
|
||||
for label, obj, name in (('application.OnReusableBlocksEditor', app, 'OnReusableBlocksEditor'),
|
||||
('application.Quit', app, 'Quit'), ('library.Close', library, 'Close')):
|
||||
try:
|
||||
record['members'][label] = {'dispid': obj._oleobj_.GetIDsOfNames(name)}
|
||||
except Exception as error:
|
||||
record['members'][label] = {'error': _exception_details(error)}
|
||||
if paths is not None:
|
||||
record['paths'] = paths
|
||||
with (Path(root)/'dispatch-diagnostics.jsonl').open('a', encoding='utf-8') as stream:
|
||||
stream.write(json.dumps(record, ensure_ascii=False, default=str) + '\n')
|
||||
|
||||
|
||||
def _probe_add_dialog(app, job, desktop, owned_pids, root, *, library=None):
|
||||
try:
|
||||
return _probe_add_dialog_impl(app, job, desktop, owned_pids, root)
|
||||
return _probe_add_dialog_impl(app, job, desktop, owned_pids, root, library=library)
|
||||
finally:
|
||||
import gc
|
||||
gc.collect() # Release UIA wrappers before closing native child dialogs.
|
||||
_close_probe_dialogs(desktop, owned_pids)
|
||||
|
||||
|
||||
def _probe_add_dialog_impl(app, job, desktop, owned_pids, root):
|
||||
def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None):
|
||||
"""Diagnostic only: proven editor/UIA tab and resource1103 controls.
|
||||
|
||||
This function has no production capability override. It is called only
|
||||
after this worker itself creates a new disposable library.
|
||||
"""
|
||||
import pythoncom, win32con, win32gui, win32process
|
||||
_dispatch_diagnostics(root, 'before-pywinauto-import', app, library)
|
||||
from pywinauto import Desktop
|
||||
_dispatch_diagnostics(root, 'after-pywinauto-import', app, library)
|
||||
from .backup_recovery import _align_library
|
||||
from .management import source_project
|
||||
source, pcb, top = source_project(job['source'], job.get('top_level_block', ''))
|
||||
@@ -242,6 +267,8 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root):
|
||||
if not source.is_relative_to(Path(root).parent.resolve()/source_folder):
|
||||
raise ValueError('隔离新增只允许使用本次复制的来源工程。')
|
||||
_align_library(source, pcb, Path(job['library']))
|
||||
_dispatch_diagnostics(root, 'after-source-alignment', app, library,
|
||||
paths={'source': str(source), 'pcb': str(pcb), 'library_argument': job['library']})
|
||||
validate_name(job['name'])
|
||||
deadline = time.monotonic() + float(job['timeout_seconds'])
|
||||
def find_window(title):
|
||||
@@ -545,7 +572,7 @@ def _catalog_com(job, root):
|
||||
if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve():
|
||||
raise RuntimeError('原生返回中心库身份不一致。')
|
||||
if job['operation'] in ('probe_create_add', 'probe_readd'):
|
||||
_probe_add_dialog(app, job, desktop, owned_pids, root)
|
||||
_probe_add_dialog(app, job, desktop, owned_pids, root, library=library)
|
||||
entries = []
|
||||
parts = library.Partitions(7)
|
||||
for i in range(1, int(parts.Count) + 1):
|
||||
@@ -569,14 +596,19 @@ def _catalog_com(job, root):
|
||||
result.update(success=True, library=str(library.FullName), flow=int(library.FlowType), entries=entries)
|
||||
except Exception as error:
|
||||
result['detail'] = str(error)
|
||||
result['exception'] = _exception_details(error)
|
||||
finally:
|
||||
if library is not None:
|
||||
try: library.Close()
|
||||
except Exception as error: result['close_detail'] = str(error)
|
||||
except Exception as error:
|
||||
result['close_detail'] = str(error)
|
||||
result['close_exception'] = _exception_details(error)
|
||||
library = None
|
||||
if app is not None:
|
||||
try: app.Quit()
|
||||
except Exception as error: result['quit_detail'] = str(error)
|
||||
except Exception as error:
|
||||
result['quit_detail'] = str(error)
|
||||
result['quit_exception'] = _exception_details(error)
|
||||
app = None
|
||||
stop.set()
|
||||
if watcher:
|
||||
@@ -630,6 +662,7 @@ def management_worker_main(request_path):
|
||||
raise ValueError('未验证的原生管理操作')
|
||||
except Exception as error:
|
||||
result['detail'] = str(error)
|
||||
result['exception'] = _exception_details(error)
|
||||
finally:
|
||||
_write_json(root/'result.json', result)
|
||||
return 0 if result['success'] and result['safe_to_continue'] else 2
|
||||
Reference in new issue
Block a user