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

@@ -8,6 +8,58 @@ import pytest
from dx_productivity.trial_task_center import worker_monitor
@pytest.mark.parametrize('exit_after', [0, .03, None])
@pytest.mark.parametrize('returncode', [0, 7])
@pytest.mark.parametrize('tree_witness', ['monitor', 'native_adapter'])
def test_normal_completion_waits_for_tree_exit_without_killing_or_replaying(tmp_path, monkeypatch, exit_after, returncode, tree_witness):
clock = SimpleNamespace(now=0.0)
monkeypatch.setattr(worker_monitor, 'time', SimpleNamespace(monotonic=lambda: clock.now,
sleep=lambda delay: setattr(clock, 'now', clock.now + delay)))
process = SimpleNamespace(pid=123, returncode=returncode, stdout=io.BytesIO(b'real receipt\n'),
stderr=io.BytesIO(), closed=False)
process.poll = lambda: process.returncode
process.close = lambda: setattr(process, 'closed', True)
def tree_confirmed():
return owners[0].allow_exit or exit_after is not None and clock.now >= exit_after
process.tree_exited = tree_confirmed if tree_witness == 'native_adapter' else lambda: True
class Owned:
def __init__(self, *_args):
self.closed = self.allow_exit = False
owners.append(self)
def tree_exited(self):
return tree_confirmed() if tree_witness == 'monitor' else True
def stop_workers(self, _process):
pytest.fail('observing a normal exit must not terminate a worker')
def close(self):
self.closed = True
owners, launches = [], []
def launch(*_args, **_kwargs):
launches.append(1)
return process
monkeypatch.setattr(worker_monitor, '_OwnedWorkers', Owned)
try:
if exit_after is None:
with pytest.raises(worker_monitor.WorkerMonitorError, match='owned worker retained'):
worker_monitor.run_monitored_process(('worker', '', '', str(tmp_path), 'python', 'job'), 1,
process_factory=launch)
assert 10 <= clock.now <= 10.02
assert not owners[0].closed and not process.closed
assert (tmp_path / 'uncertain.flag').exists()
assert any(entry['process'] is process for entry in worker_monitor.RETAINED_WORKERS.values())
else:
result = worker_monitor.run_monitored_process(('worker', '', '', str(tmp_path), 'python', 'job'), 1,
process_factory=launch)
assert result.returncode == returncode and result.stdout == 'real receipt\n'
assert exit_after <= clock.now <= exit_after + .02
assert owners[0].closed and process.closed
assert not (tmp_path / 'uncertain.flag').exists()
assert launches == [1]
finally:
owners[0].allow_exit = True
worker_monitor.cleanup_retained_workers(tmp_path)
assert not any(entry['process'] is process for entry in worker_monitor.RETAINED_WORKERS.values())
@pytest.mark.parametrize('descendants_exit', [True, False])
def test_safe_timeout_waits_for_positive_tree_exit_without_extending_cleanup_budget(tmp_path, monkeypatch, descendants_exit):
clock = SimpleNamespace(now=0.0)
@@ -75,6 +127,22 @@ def test_child_that_exits_during_job_enumeration_is_not_a_cleanup_failure(monkey
assert owned.stop_workers(process) == []
@pytest.mark.skipif(os.name != 'nt', reason='Win32 pre-handle exception classification')
@pytest.mark.parametrize('error', [ValueError(87), OSError(87, 'not a Windows error'),
OSError(5, 'denied')])
def test_non_windows_error_87_is_not_mistaken_for_a_vanished_pid(monkeypatch, error):
import win32api
import win32job
owned = worker_monitor._OwnedWorkers.__new__(worker_monitor._OwnedWorkers)
owned.job, owned.allowed = object(), set()
monkeypatch.setattr(win32job, 'QueryInformationJobObject', lambda *_: (1, 2))
def failed(*_args):
raise error
monkeypatch.setattr(win32api, 'OpenProcess', failed)
notes = owned.stop_workers(SimpleNamespace(pid=1, poll=lambda: 0))
assert len(notes) == 1 and notes[0].startswith('child_cleanup pid=2')
@pytest.mark.skipif(os.name != 'nt', reason='Win32 post-open cleanup error')
def test_error_87_after_opening_child_remains_a_cleanup_failure(monkeypatch):
import ctypes