fix: retry session control publication on Windows sharing locks

This commit is contained in:
阿白叔 committed 2026-10-17 14:10:34 +08:00
1 parent 9803993d49
commit b9193dbb21
2 files changed
+19 -4

No files matched your search

@@ -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:
+5 -2
View File
@@ -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'