diff --git a/docs/testing/2026-11-08-pr24-review-repair.md b/docs/testing/2026-11-08-pr24-review-repair.md index b625e5d..2defe93 100644 --- a/docs/testing/2026-11-08-pr24-review-repair.md +++ b/docs/testing/2026-11-08-pr24-review-repair.md @@ -14,6 +14,7 @@ | 跨模块 Desktop.created 泄漏 | 共享模拟运行时工厂自身初始化 Desktop/Cad 状态,不依赖另一个测试文件的 autouse fixture。 | | 未知弹窗样例的组合失败 | 先断言未知弹窗未被自动点击,再模拟操作员按记录指纹明确选择样例按钮、等待自然退出。确认无强制结束,不依赖异常杀进程清理测试窗口。 | | 强制结束样例的就绪标记 | 子进程先写完临时 PID 标记再原子发布,父测试线程不能读到空文件。新增可控慢写样例,仍须通过完整强制结束保护和不重放断言;测试驱动出错时仅清理自己启动的样例。 | +| 安全清理后的确认强制释放 | 不再依赖零延时定时器抢占事件顺序。记录清理已结束与用户已确认,两者齐备才接续一次;覆盖确认框内先结束、确认后结束及拒绝授权,关闭中的窗口不接续。 | ## 生产逻辑修复 @@ -22,6 +23,7 @@ - 仅转换 `problem_retry_scope`、`quick_run_uncertain` 两类安全拒绝;资源清理失败和存储中断继续升级,原预留和一次重试预算不重置。 - 安全打开超时清理时,根进程退出和 Job 子进程计数可能错开;在现有 10 秒清理预算内等待明确的整树退出证明。没有证明继续保留句柄,不扩大超时、不强制结束未知子进程。 - Job 枚举后 `OpenProcess` 返回 87 表示该 PID 已退出;仅在尚未取得句柄时忽略。取得句柄后的查询或结束错误仍保留为清理失败,并有反例测试。 +- 清理线程完成回调释放执行通道后,才接续已明确授权的强制释放;模态确认框嵌套事件循环提前收到完成信号时也不会丢失授权或重复执行。 ## 原 PR 的模块意图与风险 diff --git a/src/dx_productivity/trial_task_center/ui/main_window.py b/src/dx_productivity/trial_task_center/ui/main_window.py index d4438d1..72599cf 100644 --- a/src/dx_productivity/trial_task_center/ui/main_window.py +++ b/src/dx_productivity/trial_task_center/ui/main_window.py @@ -890,7 +890,26 @@ class TrialTaskCenterWindow(MaterialMainWindow): show_cad_dialogs(self) self._update_gates() return + force_confirmed = False + cleanup_finished = False + force_started = False + def start_force_if_ready(): + nonlocal force_started + if force_confirmed and cleanup_finished and not force_started and not self._close_pending: + force_started = True + self._run_async(lambda: self.service.force_pending_owned_resources(confirmed=True), + forced, self.run_panel.cleanup_resources_button) + def after_cleanup(): + nonlocal cleanup_finished + cleanup_finished = True + start_force_if_ready() + def forced(remaining): + self._resource_status.refresh() + self._set_operator_message('仍有资源未能确认释放,请保留诊断。' if remaining else + '自有残留资源已释放;相关工程仍须核验后恢复。', 'warning') + self._update_gates() def done(pending): + nonlocal force_confirmed hints = tuple(dict.fromkeys(row['resolution_hint'] for row in pending if row.get('resolution_hint'))) self._set_operator_message( '\n'.join(hints) if hints else '后台资源仍待清理,请保留诊断记录后重试。' if pending else '后台资源已释放,可以继续。', @@ -903,18 +922,11 @@ class TrialTaskCenterWindow(MaterialMainWindow): '可能存在未保存或部分保存的结果。中断证据会保留,相关工程必须先核验,不能直接再次替换。', QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, QMessageBox.StandardButton.No) == QMessageBox.StandardButton.Yes: - def forced(remaining): - self._resource_status.refresh() - self._set_operator_message('仍有资源未能确认释放,请保留诊断。' if remaining else - '自有残留资源已释放;相关工程仍须核验后恢复。', 'warning') - self._update_gates() - # The cleanup worker still occupies _workers during this result - # callback. Submit after its finished signal releases the lane. - QTimer.singleShot(0, lambda: self._run_async( - lambda: self.service.force_pending_owned_resources(confirmed=True), forced, - self.run_panel.cleanup_resources_button)) + force_confirmed = True + # A modal question can deliver finished before returning Yes. + start_force_if_ready() self._run_async(self.service.cleanup_batch_resources, done, - self.run_panel.cleanup_resources_button) + self.run_panel.cleanup_resources_button, on_finished=after_cleanup) def _startup_resource_recheck(self): if self._closed: @@ -1247,7 +1259,7 @@ class TrialTaskCenterWindow(MaterialMainWindow): self._set_operator_message(state.message) def _run_async(self, operation, callback, button=None, *, expected_task_id=None, - retain_result=False, operation_area='', **_unused): + retain_result=False, operation_area='', on_finished=None, **_unused): if (self._workers or self._closed or (self._dsh_task_creation is not None and self._dsh_task_creation.dialog is not None) or (self._dsh_lifecycle is not None and self._dsh_lifecycle.dialog is not None) or (self._dsh_operations is not None and self._dsh_operations.dialog is not None)): @@ -1311,6 +1323,8 @@ class TrialTaskCenterWindow(MaterialMainWindow): QTimer.singleShot(0, self.close) if not self._closed: self._update_gates() + if on_finished is not None and not self._close_pending: + on_finished() worker.signals.succeeded.connect(succeeded) worker.signals.failed.connect(failed) worker.signals.finished.connect(finished) diff --git a/tests/v1_1/test_resource_reconciliation_ui.py b/tests/v1_1/test_resource_reconciliation_ui.py index 5104410..7aa5e30 100644 --- a/tests/v1_1/test_resource_reconciliation_ui.py +++ b/tests/v1_1/test_resource_reconciliation_ui.py @@ -1,24 +1,61 @@ from PySide6.QtWidgets import QMessageBox +from PySide6.QtCore import QTimer +from threading import Event + +import pytest from test_ui_remediation import window -def test_confirmed_force_runs_after_cleanup_worker_finishes(window, qtbot, monkeypatch): +@pytest.mark.parametrize('finish_order', ['normal', 'after_confirmation', 'during_confirmation']) +@pytest.mark.parametrize('confirmed', [False, True]) +def test_confirmed_force_runs_after_cleanup_worker_finishes(window, qtbot, monkeypatch, finish_order, confirmed): qtbot.waitUntil(lambda: not window._workers and not window._resource_status.busy) pending = ({'state': 'blocked', 'kind': 'retained_worker', 'force_available': True},) monkeypatch.setattr(window.service, 'pending_batch_resources', lambda: pending) monkeypatch.setattr(window.service, 'cleanup_batch_resources', lambda: pending) + from dx_productivity.trial_task_center.ui.workers import ServiceWorker + release, prompted, event_turn = Event(), Event(), Event() + original_run = ServiceWorker.run + def gated_run(worker): + if finish_order != 'normal' and worker.action is window.service.cleanup_batch_resources: + try: + worker.signals.succeeded.emit(worker.action()) + assert release.wait(5) + finally: + worker.signals.finished.emit() + else: + original_run(worker) + monkeypatch.setattr(ServiceWorker, 'run', gated_run) calls = [] def force(*, confirmed): calls.append(confirmed) return () monkeypatch.setattr(window.service, 'force_pending_owned_resources', force) - monkeypatch.setattr(QMessageBox, 'question', lambda *_a, **_k: QMessageBox.StandardButton.Yes) + def confirm(*_a, **_k): + prompted.set() + if finish_order == 'during_confirmation': + release.set() + qtbot.waitUntil(lambda: not window._workers) + assert calls == [] + QTimer.singleShot(0, event_turn.set) + return QMessageBox.StandardButton.Yes if confirmed else QMessageBox.StandardButton.No + monkeypatch.setattr(QMessageBox, 'question', confirm) window._resource_status.records = pending window._resource_status.known = True window._update_gates() - window.run_panel.cleanup_resources_button.click() - qtbot.waitUntil(lambda: calls == [True] and not window._workers) + try: + window.run_panel.cleanup_resources_button.click() + qtbot.waitUntil(lambda: prompted.is_set() and event_turn.is_set()) + if finish_order == 'after_confirmation': + assert calls == [] + assert window._workers + release.set() + qtbot.waitUntil(lambda: not window._workers) + assert calls == ([True] if confirmed else []) + finally: + release.set() + qtbot.waitUntil(lambda: not window._workers) def test_lost_handles_show_restart_hint_without_unusable_force_prompt(window, qtbot, monkeypatch):