From 9b2372e3894f268147d302555a0a5a82ca9697df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Thu, 15 Oct 2026 18:14:29 +0800 Subject: [PATCH] fix: bound slow CAD startup and retain blocked COM diagnostics --- .../trial_task_center/isolated_cad.py | 16 +++++++++++- tests/v1_1/test_isolated_cad.py | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/dx_productivity/trial_task_center/isolated_cad.py b/src/dx_productivity/trial_task_center/isolated_cad.py index b79d971..38d2b48 100644 --- a/src/dx_productivity/trial_task_center/isolated_cad.py +++ b/src/dx_productivity/trial_task_center/isolated_cad.py @@ -101,7 +101,7 @@ class IsolatedCadSession: probe = self.desktop.start_process(_helper_command('probe', bootstrap, self.cad.pid), env=_child_environment(environment), stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: - deadline = time.monotonic() + 75 + deadline = time.monotonic() + 120 while probe.poll() is None: self._check_abort() if time.monotonic() >= deadline: @@ -189,6 +189,20 @@ def run_isolated_process(command, timeout, *, abort_event=None, cooperative_abor def native_action(action, evidence, expected_pid): """Runs in a child created with lpDesktop, never in a Python thread switch.""" + if action != 'probe': + return _native_action(action, evidence, expected_pid) + # COM startup can block inside a native call, so keep diagnostics outside + # the redirected pipes and retain them when the supervisor ends the child. + import faulthandler + with Path(str(evidence) + '.fault.txt').open('a', encoding='utf-8') as trace: + faulthandler.dump_traceback_later(20, repeat=True, file=trace) + try: + return _native_action(action, evidence, expected_pid) + finally: + faulthandler.cancel_dump_traceback_later() + + +def _native_action(action, evidence, expected_pid): import pythoncom import win32com.client import win32gui diff --git a/tests/v1_1/test_isolated_cad.py b/tests/v1_1/test_isolated_cad.py index 3820ce5..531950c 100644 --- a/tests/v1_1/test_isolated_cad.py +++ b/tests/v1_1/test_isolated_cad.py @@ -107,6 +107,31 @@ def test_success_quits_owned_cad_before_releasing_desktop(monkeypatch, tmp_path) assert record['state'] == 'released' and record['clean'] +@pytest.mark.parametrize('ready_after', [90, None]) +def test_slow_startup_is_bounded_and_cleanup_remains_owned(monkeypatch, tmp_path, ready_after): + events, _ = fake_desktop(monkeypatch, tmp_path) + clock = [0.0] + monkeypatch.setattr(runtime.time, 'monotonic', lambda: clock[0]) + monkeypatch.setattr(runtime.time, 'sleep', lambda seconds: clock.__setitem__(0, clock[0] + seconds)) + original = runtime.PrivateDesktop.start_process + def start(self, command, **kwargs): + process = original(self, command, **kwargs) + if command[0] == 'probe': + process.poll = lambda: 0 if ready_after is not None and clock[0] >= ready_after else None + return process + monkeypatch.setattr(runtime.PrivateDesktop, 'start_process', start) + if ready_after is None: + with pytest.raises(runtime.PrivateCadStartupError, match='bootstrap timed out'): + with runtime.IsolatedCadSession(tmp_path): + pytest.fail('unresponsive startup entered project work') + assert 119 <= clock[0] <= 121 + assert ('release', False) in events + else: + with runtime.IsolatedCadSession(tmp_path): + assert clock[0] >= ready_after + assert ('release', False) in events + + def test_repeated_session_preserves_prior_release_evidence(monkeypatch, tmp_path): fake_desktop(monkeypatch, tmp_path) with runtime.IsolatedCadSession(tmp_path) as first: