Retry transient Windows sharing conflicts when publishing private CAD state

This commit is contained in:
阿白叔 committed 2026-10-15 13:30:45 +08:00
1 parent 980198a987
commit 7610a0fd86
2 files changed
+54 -2

No files matched your search

@@ -31,6 +31,21 @@ def _child_environment(environment):
return environment
def _replace_record(temporary, target):
# Windows readers can temporarily deny delete access to the old JSON.
# Keep the complete old record until replacement succeeds, with a finite
# retry budget so persistent permissions failures still reach the caller.
deadline = time.monotonic() + 1.0
while True:
try:
temporary.replace(target)
return
except PermissionError:
if time.monotonic() >= deadline:
raise
time.sleep(0.02)
def _designer_executable():
import winreg
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'Viewdraw.Application\CLSID') as key:
@@ -65,13 +80,13 @@ class IsolatedCadSession:
target = self.root / 'private-desktop.json'
temporary = target.with_suffix('.tmp')
temporary.write_text(json.dumps(self.record, ensure_ascii=False, indent=2), encoding='utf-8')
temporary.replace(target)
_replace_record(temporary, target)
history = self.root / 'private-desktop-sessions'
history.mkdir(exist_ok=True)
archived = history / (self.token + '.json')
pending = archived.with_suffix('.tmp')
pending.write_text(json.dumps(self.record, ensure_ascii=False, indent=2), encoding='utf-8')
pending.replace(archived)
_replace_record(pending, archived)
def __enter__(self):
try:
+37
View File
@@ -1,7 +1,9 @@
import io
import json
import os
from pathlib import Path
from threading import Event
from threading import Timer
from types import SimpleNamespace
import pytest
@@ -10,6 +12,41 @@ from dx_productivity.trial_task_center import isolated_cad as runtime
from dx_productivity.trial_task_center.worker_monitor import WorkerMonitorError
@pytest.mark.skipif(os.name != 'nt', reason='Windows state-file sharing semantics')
def test_private_record_survives_a_reader_temporarily_denying_replacement(tmp_path):
import win32con
import win32file
session = runtime.IsolatedCadSession(tmp_path)
session._record(state='starting')
held = win32file.CreateFile(str(tmp_path / 'private-desktop.json'), win32con.GENERIC_READ,
win32con.FILE_SHARE_READ, None, win32con.OPEN_EXISTING, 0, None)
reader_done = Timer(0.1, held.Close)
reader_done.start()
try:
session._record(state='released', clean=True)
finally:
reader_done.join()
current = json.loads((tmp_path / 'private-desktop.json').read_text())
archived = json.loads((tmp_path / 'private-desktop-sessions' / (session.token + '.json')).read_text())
assert current == archived and current['state'] == 'released' and current['clean']
@pytest.mark.skipif(os.name != 'nt', reason='Windows state-file sharing semantics')
def test_private_record_reports_persistent_denial_and_keeps_previous_record(tmp_path):
import win32con
import win32file
session = runtime.IsolatedCadSession(tmp_path)
session._record(state='starting')
held = win32file.CreateFile(str(tmp_path / 'private-desktop.json'), win32con.GENERIC_READ,
win32con.FILE_SHARE_READ, None, win32con.OPEN_EXISTING, 0, None)
try:
with pytest.raises(PermissionError):
session._record(state='released', clean=True)
assert json.loads((tmp_path / 'private-desktop.json').read_text())['state'] == 'starting'
finally:
held.Close()
def fake_desktop(monkeypatch, tmp_path, *, abort=None, quit_code=0, cad_exit_code=0):
events = []
cad = SimpleNamespace(pid=123, returncode=None)