diff --git a/src/cbb_refresh_verify/file_probe.py b/src/cbb_refresh_verify/file_probe.py index ccb1a9e..234b4cb 100644 --- a/src/cbb_refresh_verify/file_probe.py +++ b/src/cbb_refresh_verify/file_probe.py @@ -13,8 +13,9 @@ from uuid import uuid4 @contextmanager -def hold_release_files(library, roots=None, *, saved_project=None, timeout_seconds=10, stop=None): +def hold_release_files(library, roots=None, *, saved_project=None, timeout_seconds=None, stop=None): """Only this pure probe may be terminated. stdin EOF releases its handles.""" + from .operation_queue import native_phase with tempfile.TemporaryDirectory(prefix='cbb-file-probe-') as folder: job = Path(folder)/'request.json' token = uuid4().hex @@ -27,30 +28,34 @@ def hold_release_files(library, roots=None, *, saved_project=None, timeout_secon stderr=subprocess.DEVNULL, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)) try: started = time.monotonic() - deadline = started + max(.1, min(10, timeout_seconds)) + limit = 30 if roots is None and saved_project is None else 10 + budget = limit if timeout_seconds is None else min(limit, timeout_seconds) + deadline = started + max(.1, budget) result_path = job.with_name('result.json') - while True: - if stop is not None and stop.is_set(): - raise InterruptedError('FILE_PROBE_CANCELLED') - if time.monotonic() >= deadline: + # The isolated helper has its own deadline and cancel signal. + with native_phase(read_only=True): + while True: + if stop is not None and stop.is_set(): + raise InterruptedError('FILE_PROBE_CANCELLED') + if time.monotonic() >= deadline: + try: + progress = json.loads(job.with_name('progress.json').read_text('utf-8')) + except (OSError, ValueError): + progress = {} + stage = progress.get('stage', 'startup') + path = progress.get('path', str(library)) + raise TimeoutError(f'FILE_PROBE_TIMEOUT: 文件占用检查超时' + f'({time.monotonic()-started:.1f}s,{stage},{path}),未证明释放') try: - progress = json.loads(job.with_name('progress.json').read_text('utf-8')) - except (OSError, ValueError): - progress = {} - stage = progress.get('stage', 'startup') - path = progress.get('path', str(library)) - raise TimeoutError(f'FILE_PROBE_TIMEOUT: 文件占用检查超时' - f'({time.monotonic()-started:.1f}s,{stage},{path}),未证明释放') - try: - record = json.loads(result_path.read_text('utf-8')) - break - except FileNotFoundError: - if process.poll() is not None and not result_path.exists(): - raise OSError('FILE_PROBE_UNCONFIRMED: 文件探测未返回证据') from None - except PermissionError: - # Windows may temporarily share-lock a newly published receipt. - pass - time.sleep(.02) + record = json.loads(result_path.read_text('utf-8')) + break + except FileNotFoundError: + if process.poll() is not None and not result_path.exists(): + raise OSError('FILE_PROBE_UNCONFIRMED: 文件探测未返回证据') from None + except PermissionError: + # Windows may temporarily share-lock a newly published receipt. + pass + time.sleep(.02) if stop is not None and stop.is_set(): raise InterruptedError('FILE_PROBE_CANCELLED') if record.get('token') != token or record.get('status') != 'HELD' or process.poll() is not None: diff --git a/tests/test_library_lock.py b/tests/test_library_lock.py index 7c3d577..1cda3ef 100644 --- a/tests/test_library_lock.py +++ b/tests/test_library_lock.py @@ -343,3 +343,24 @@ def test_probe_timeout_reports_last_io_phase_and_path(tmp_path, monkeypatch): with pytest.raises(TimeoutError, match=r'FILE_PROBE_TIMEOUT.*open_exclusive.*SymbolLibs/icdb.dat'): with file_probe.hold_release_files(library, timeout_seconds=.7): pytest.fail('a timed-out probe cannot prove release') + + + +def test_full_library_probe_budget_accounts_for_both_tree_scans(tmp_path, monkeypatch): + from cbb_refresh_verify import file_probe + from types import SimpleNamespace + + class Process: + stdin = SimpleNamespace(close=lambda: None) + def wait(self, *_): + return 0 + def poll(self): + return None + + monkeypatch.setattr(file_probe.subprocess, 'Popen', lambda *a, **k: Process()) + ticks = iter([0., 11., 31., 32.]) + monkeypatch.setattr(file_probe.time, 'monotonic', lambda: next(ticks)) + monkeypatch.setattr(file_probe.time, 'sleep', lambda _: None) + with pytest.raises(TimeoutError, match=r'FILE_PROBE_TIMEOUT.*32.0s'): + with file_probe.hold_release_files(tmp_path/'lib.lmc'): + pytest.fail('the fake probe never returns a receipt')