fix: bound read cancellation and forward safe project pause
This commit is contained in:
1 parent
75ee8bd33a
commit
a8da30aad2
5 files changed
+265
-17
No files matched your search
@@ -0,0 +1,140 @@
|
||||
"""Selected lxb nonblocking contracts; disposable workers only, no CAD."""
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from dx_productivity.trial_task_center import worker_monitor
|
||||
from dx_productivity.trial_task_center.stop_control import StopControl
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt', reason='Windows owned Job and read cancellation sample')
|
||||
def test_cancel_blocked_read_has_deadline_even_when_progress_keeps_arriving(tmp_path, monkeypatch):
|
||||
"""A huge ordinary idle limit must not delay an explicit import cancel."""
|
||||
script = tmp_path / 'reader.py'
|
||||
script.write_text('''import pathlib,sys,time
|
||||
root=pathlib.Path(sys.argv[1]);job=sys.argv[4];token=sys.argv[5]
|
||||
while not (root/f'monitor-{token}.ready').exists(): time.sleep(.01)
|
||||
(root/'reading').touch()
|
||||
for n in range(1,500):
|
||||
print(f'DX_PROGRESS\\t{token}\\t{job}\\t{n}\\tnative_snapshot_read\\t0\\t1\\t0',flush=True)
|
||||
time.sleep(.02)
|
||||
''')
|
||||
monkeypatch.setattr(worker_monitor, 'READ_ONLY_CANCEL_GRACE_SECONDS', .15, raising=False)
|
||||
control = StopControl()
|
||||
def cancel():
|
||||
while not (tmp_path / 'reading').exists():
|
||||
time.sleep(.01)
|
||||
control.set()
|
||||
thread = threading.Thread(target=cancel, daemon=True)
|
||||
thread.start()
|
||||
started = time.monotonic()
|
||||
command = (sys._base_executable, '-u', str(script), str(tmp_path), sys._base_executable, '', 'job')
|
||||
from dx_productivity.trial_task_center.isolated_desktop import _Win32Api
|
||||
api = _Win32Api()
|
||||
def start(launch, **options):
|
||||
return api.create_process(launch, None, tmp_path, None, **options)
|
||||
try:
|
||||
with pytest.raises(worker_monitor.WorkerMonitorCancelled):
|
||||
worker_monitor.run_monitored_process(command, 150, abort_event=control,
|
||||
cooperative_abort=True, force_control=control, process_factory=start)
|
||||
assert time.monotonic() - started < 3
|
||||
assert not control.waiting_for_resolution
|
||||
assert not (tmp_path / 'uncertain.flag').exists()
|
||||
finally:
|
||||
control.set()
|
||||
thread.join(1)
|
||||
api.close_pending()
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt', reason='Windows owned worker watchdog sample')
|
||||
def test_frozen_read_uses_short_read_watchdog_instead_of_execution_idle_limit(tmp_path, monkeypatch):
|
||||
script = tmp_path / 'frozen.py'
|
||||
script.write_text('import time; time.sleep(1.5)')
|
||||
monkeypatch.setattr(worker_monitor, 'READ_ONLY_IDLE_TIMEOUT_SECONDS', .15, raising=False)
|
||||
command = (sys._base_executable, '-u', str(script), str(tmp_path), sys._base_executable, '', 'job')
|
||||
started = time.monotonic()
|
||||
with pytest.raises(subprocess.TimeoutExpired):
|
||||
worker_monitor.run_monitored_process(command, 150, cooperative_abort=True)
|
||||
assert time.monotonic() - started < 1.3
|
||||
|
||||
|
||||
def test_project_open_has_short_watchdog_only_at_a_safe_boundary():
|
||||
watch = worker_monitor.ProgressWatch('token', 'job', 150, 0)
|
||||
assert watch.accept(b'DX_PROGRESS\ttoken\tjob\t1\topen_project\t0\t1\t1\n', 0)
|
||||
assert watch.action(45) == 'request_stop'
|
||||
assert watch.action(90) == 'unresponsive'
|
||||
unsafe = worker_monitor.ProgressWatch('token', 'job', 150, 0)
|
||||
assert unsafe.accept(b'DX_PROGRESS\ttoken\tjob\t1\topen_project\t0\t1\t0\n', 0)
|
||||
assert unsafe.action(90) is None
|
||||
|
||||
|
||||
def test_project_step_forwards_pause_to_its_worker_before_checkpoint(tmp_path):
|
||||
from dx_productivity.trial_task_center.project_session import ProjectSessionWorker
|
||||
control = StopControl()
|
||||
control.set()
|
||||
requested = []
|
||||
def worker(command, timeout, *, abort_event, **_controls):
|
||||
requested.append(abort_event.wait(.5))
|
||||
return subprocess.CompletedProcess(command, 2, '', 'stopped at safe boundary')
|
||||
runtime = SimpleNamespace(stop_event=control, run_worker=worker)
|
||||
session = ProjectSessionWorker(tmp_path / 'board.prj', tmp_path, runtime=runtime)
|
||||
command = (sys.executable, '-u', 'sample', str(tmp_path), sys.executable, '', 'job')
|
||||
try:
|
||||
assert session.run_step(command, 150).returncode == 2
|
||||
assert requested == [True]
|
||||
assert not control.force_event.is_set()
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt', reason='Windows Job with a real unsafe worker')
|
||||
@pytest.mark.parametrize('deny_stop', [False, True])
|
||||
def test_ordinary_pause_cannot_kill_write_before_queued_unsafe_progress_is_read(tmp_path, monkeypatch, deny_stop):
|
||||
script = tmp_path / 'writing.py'
|
||||
script.write_text('''import pathlib,sys,time
|
||||
root=pathlib.Path(sys.argv[1]);job=sys.argv[4];token=sys.argv[5]
|
||||
while not (root/f'monitor-{token}.ready').exists(): time.sleep(.005)
|
||||
print(f'DX_PROGRESS\\t{token}\\t{job}\\t1\\tnative_replace\\t0\\t1\\t0',flush=True)
|
||||
(root/'writing').touch()
|
||||
while not (root/f'monitor-{token}.stop').exists(): time.sleep(.005)
|
||||
(root/'pause-seen').touch()
|
||||
while not (root/'finish-write').exists(): time.sleep(.005)
|
||||
(root/'saved').touch()
|
||||
''', encoding='utf-8')
|
||||
control = StopControl()
|
||||
atomic = worker_monitor._atomic_text
|
||||
stop_attempts = []
|
||||
def publish(path, value):
|
||||
if path.suffix == '.stop':
|
||||
stop_attempts.append(path)
|
||||
if deny_stop and len(stop_attempts) == 1:
|
||||
raise PermissionError('controlled stop token sharing violation')
|
||||
atomic(path, value)
|
||||
if path.suffix == '.ready':
|
||||
deadline = time.monotonic() + 3
|
||||
while not (tmp_path / 'writing').exists() and time.monotonic() < deadline:
|
||||
time.sleep(.005)
|
||||
assert (tmp_path / 'writing').exists()
|
||||
control.set() # unsafe progress is queued, but watch still says starting/safe.
|
||||
monkeypatch.setattr(worker_monitor, '_atomic_text', publish)
|
||||
observed = []
|
||||
def complete_write():
|
||||
deadline = time.monotonic() + 3
|
||||
while not (tmp_path / 'pause-seen').exists() and time.monotonic() < deadline:
|
||||
time.sleep(.005)
|
||||
observed.append((tmp_path / 'pause-seen').exists())
|
||||
(tmp_path / 'finish-write').touch()
|
||||
driver = threading.Thread(target=complete_write, daemon=True)
|
||||
driver.start()
|
||||
command = (sys._base_executable, '-u', str(script), str(tmp_path), sys._base_executable, '', 'job')
|
||||
try:
|
||||
result = worker_monitor.run_monitored_process(command, 10, abort_event=control, force_control=control)
|
||||
assert result.returncode == 0 and (tmp_path / 'saved').exists()
|
||||
assert not control.force_event.is_set() and not (tmp_path / 'uncertain.flag').exists()
|
||||
finally:
|
||||
(tmp_path / 'finish-write').touch()
|
||||
driver.join(4)
|
||||
assert observed == [True]
|
||||
assert len(stop_attempts) >= (2 if deny_stop else 1)
|
||||
Reference in new issue
Block a user