diff --git a/src/dx_productivity/trial_task_center/project_session.py b/src/dx_productivity/trial_task_center/project_session.py index 3e1d3ed..5f41462 100644 --- a/src/dx_productivity/trial_task_center/project_session.py +++ b/src/dx_productivity/trial_task_center/project_session.py @@ -3,6 +3,7 @@ from __future__ import annotations from pathlib import Path import subprocess +import time from threading import Event, Thread from uuid import uuid4 @@ -11,8 +12,19 @@ from .worker_monitor import WorkerMonitorError, run_monitored_process def _write_control(path: Path, value: str): temporary = path.with_name(path.name + '.' + uuid4().hex + '.tmp') - temporary.write_text(value, encoding='utf-8') - temporary.replace(path) + try: + temporary.write_text(value, encoding='utf-8') + deadline = time.monotonic() + 1.0 + while True: + try: + temporary.replace(path) + return + except PermissionError: + if time.monotonic() >= deadline: + raise + time.sleep(.02) + finally: + temporary.unlink(missing_ok=True) class ProjectSessionWorker: diff --git a/tests/v1_1/test_native_report_io.py b/tests/v1_1/test_native_report_io.py index a1fc3d5..ba16c6d 100644 --- a/tests/v1_1/test_native_report_io.py +++ b/tests/v1_1/test_native_report_io.py @@ -2,12 +2,13 @@ from threading import Timer import pytest -@pytest.mark.parametrize('kind', ['native_report', 'queue_command']) +@pytest.mark.parametrize('kind', ['native_report', 'queue_command', 'session_control']) def test_native_report_waits_for_reader_to_release_windows_replace_lock(tmp_path, kind): import win32file import win32con from dx_productivity.trial_task_center.native_helper import atomic_text from dx_productivity.trial_task_center.quick_run import _write_atomic_queue_text + from dx_productivity.trial_task_center.project_session import _write_control report = tmp_path / 'native.json' report.write_text('old complete report') held = win32file.CreateFile(str(report), win32con.GENERIC_READ, @@ -17,8 +18,10 @@ def test_native_report_waits_for_reader_to_release_windows_replace_lock(tmp_path try: if kind == 'native_report': atomic_text(report, 'new complete report') - else: + elif kind == 'queue_command': _write_atomic_queue_text(report, 'new complete report', 'quick_run_command', 'publish failed') + else: + _write_control(report, 'new complete report') finally: timer.join() assert report.read_text() == 'new complete report'