fix: await natural worker tree exit before reconciling completed work
Core regression / Python core and release configuration (push) Successful in 39s
Core regression / JavaScript syntax (push) Successful in 2s
Core regression / JavaScript syntax (pull_request) Successful in 2s
Core regression / Python core and release configuration (pull_request) Failing after 40s

This commit is contained in:
阿白叔 committed 2026-11-09 09:47:35 +08:00
1 parent 189ddc238b
commit f12ddaddae
4 files changed
+97 -6

No files matched your search

@@ -1260,6 +1260,11 @@ class TrialTaskCenterWindow(MaterialMainWindow):
def _run_async(self, operation, callback, button=None, *, expected_task_id=None,
retain_result=False, operation_area='', on_finished=None, **_unused):
"""Call on_finished after releasing the worker lane, including failures.
A modal result callback can receive finished before that callback returns.
Closing windows suppress the continuation.
"""
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)):
@@ -52,6 +52,12 @@ def _worker_tree_exited(process, owned):
return False
def _wait_worker_tree_exit(process, owned, deadline):
while not _worker_tree_exited(process, owned) and time.monotonic() < deadline:
time.sleep(.01)
return _worker_tree_exited(process, owned)
def _retain_worker(root, token, job_id, process, owned, close_process, error):
from .batch_resources import process_identity, write_record
from .stop_control import protect_interruption
@@ -276,6 +282,7 @@ class _OwnedWorkers:
win32job.AssignProcessToJobObject(self.job, process._handle)
def stop_workers(self, process: subprocess.Popen) -> list[str]:
import pywintypes
import win32api
import win32job
@@ -301,8 +308,9 @@ class _OwnedWorkers:
else:
failures.append(f'preserved_non_worker pid={process_id}')
except Exception as error:
code = getattr(error, 'winerror', error.args[0] if error.args else None)
if handle is not None or code != 87: # OpenProcess may observe an already exited PID.
vanished = (handle is None and isinstance(error, (OSError, pywintypes.error))
and getattr(error, 'winerror', None) == 87)
if not vanished: # OpenProcess may observe an already exited PID.
failures.append(f'child_cleanup pid={process_id} error={error}')
finally:
if handle is not None:
@@ -487,6 +495,7 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
raise WorkerMonitorSafeTimeout('project open made no progress; safe stop was requested',
stage=watch.stage)
raise subprocess.TimeoutExpired(command, idle_timeout * 2)
exit_deadline = time.monotonic() + 10
drain_deadline = time.monotonic() + 5
while (any(thread.is_alive() for thread in threads) or not messages.empty()) and time.monotonic() < drain_deadline:
try:
@@ -494,7 +503,8 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
except queue.Empty:
pass
_publish_progress(progress_path, watch.snapshot())
if not cooperative_abort and not _worker_tree_exited(process, owned):
# A root can exit before Job accounting confirms its descendants exited.
if not cooperative_abort and not _wait_worker_tree_exit(process, owned, exit_deadline):
raise WorkerMonitorError('worker root exited while owned descendants remain; reconciliation required')
return subprocess.CompletedProcess(command, process.returncode, _decode(output[0]), _decode(output[1]))
except Exception as error:
@@ -524,8 +534,7 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
# Root exit and Job accounting need not become observable in
# the same tick. Confirm naturally exiting descendants within
# the existing worker-cleanup budget; never force unknown ones.
while not _worker_tree_exited(process, owned) and time.monotonic() < cleanup_deadline:
time.sleep(.01)
_wait_worker_tree_exit(process, owned, cleanup_deadline)
diagnostic = watch.snapshot() | {'monitor_error': str(error), 'worker_cleanup_notes': failures}
try:
_atomic_text(progress_path, json.dumps(diagnostic, ensure_ascii=False))