fix: renew release progress at completed proof boundaries

This commit is contained in:
阿白叔 committed 2026-11-17 05:30:51 +08:00
1 parent e0bb2438e8
commit de81bbf293
3 files changed
+122

No files matched your search

+7
View File
@@ -498,6 +498,7 @@ def owned_snapshot(path):
@contextmanager
def _prove_library_released(library, block_name=None, *, block_names=(), allow_unknown_checkpoint=False, stop=None):
"""Hold all native database read handles during explicit lease release."""
from .operation_queue import note_metadata_progress
from .owner_probe import query_owners
from .native_management import _retained_workers
from .native_export_guard import assert_retained_exports_idle, prove_export_copies_released
@@ -546,6 +547,7 @@ def _prove_library_released(library, block_name=None, *, block_names=(), allow_u
owners.append(item)
if owners:
raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}')
note_metadata_progress()
return owners
check_owners()
from .file_probe import hold_release_files
@@ -558,6 +560,7 @@ def _prove_library_released(library, block_name=None, *, block_names=(), allow_u
blocked = LibraryBusy(str(error))
blocked.winerror = getattr(error, 'winerror', None)
raise blocked from error
note_metadata_progress()
if files['addresses']:
# Native inspection needs the database; reacquire proof handles
# and compare the exact address/project binding before commit.
@@ -578,6 +581,7 @@ def _prove_library_released(library, block_name=None, *, block_names=(), allow_u
'exclusive_files', 'addresses', 'signatures', 'inspection_targets')):
raise LibraryBusy('原生检查期间资源范围或地址身份变化。')
files = current
note_metadata_progress()
export_proof = (prove_export_copies_released(library, allow_unknown_checkpoint=True)
if allow_unknown_checkpoint else prove_export_copies_released(library))
export_copies = stack.enter_context(export_proof)
@@ -600,6 +604,7 @@ def release_retained_library(library, evidence_dir, *, block_name=None, block_na
not the correctness of a partially changed library or its recovery state.
"""
import win32con, win32file
from .operation_queue import note_metadata_progress
library = Path(library).resolve(strict=True)
from .session_record import (retained_session_snapshot, acknowledge_library_release,
reconciled_retained_sessions)
@@ -689,6 +694,7 @@ def release_retained_library(library, evidence_dir, *, block_name=None, block_na
'operation_ids': [item['operation_id'] for item in reconciled_sessions]}
pending = {**record, 'status': 'RELEASE_PENDING', 'lease_state': 'PENDING'}
(root/'release.json').write_text(json.dumps(pending, ensure_ascii=False, indent=2), encoding='utf-8')
note_metadata_progress()
for index, checkpoint in enumerate(checkpoints):
if checkpoint.exists():
data = checkpoint.read_bytes()
@@ -696,6 +702,7 @@ def release_retained_library(library, evidence_dir, *, block_name=None, block_na
archived.write_bytes(data)
if archived.read_bytes() != data:
raise OSError('阻止记录归档校验失败,保持库锁。')
note_metadata_progress()
if stop is not None and stop.is_set():
raise InterruptedError('已停止释放检查')
checkpoint.unlink() # Checkpoint JSON only; never .lock.
@@ -624,6 +624,7 @@ def release_targets_complete(rows):
def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False, cancel_native=False):
from .operation_queue import note_metadata_progress
library = Path(plan['library']).resolve()
root = Path(evidence_dir).resolve()/('sessions-'+uuid4().hex[:12])
if root.is_relative_to(library.parent) or library.parent.is_relative_to(root):
@@ -663,6 +664,7 @@ def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False, ca
result['consent_path'] = str(consent_path)
try:
fresh = {r['pid']: r for r in query_process_snapshot()}
note_metadata_progress()
blocks = plan.get('block_names', ())
roots = (tuple(library.parent/'ReusableBlocksLP'/b/'Work' for b in blocks)
+ (library.parent/'SymbolLibs',)) if blocks else (library.parent,)
@@ -700,7 +702,9 @@ def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False, ca
row.update(outcome)
if not save():
return result # Preserve completed facts; stop further actions on storage failure.
note_metadata_progress()
after = {r['pid']: r for r in query_process_snapshot()}
note_metadata_progress()
remaining_windows = _process_windows({r['pid'] for r in result['sessions']
if r['release_status'] == 'WAITING' and r['pid'] in after})
for row in result['sessions']:
+111
View File
@@ -0,0 +1,111 @@
"""Completed release work renews inactivity; a late proof cannot release a lease."""
from contextlib import contextmanager
import json
from pathlib import Path
from threading import Event
import time
from types import SimpleNamespace
import pytest
from test_issue15_resource_release import library_file, cleanup
from cbb_refresh_verify import library_lock, operation_queue, owner_probe, file_probe, residual_sessions
@pytest.fixture
def release_work(tmp_path, monkeypatch):
library = library_file(tmp_path)
with library_lock.LibraryLock(library, safe_to_release=lambda: False) as lease:
pass
offset = [0]
clock = time.monotonic
monkeypatch.setattr(operation_queue, 'time', SimpleNamespace(monotonic=lambda: clock()+offset[0]))
steps = []
state = {'delay': lambda name: None}
def complete(name):
steps.append(name)
state['delay'](name)
monkeypatch.setattr(owner_probe, 'query_owners', lambda *_: (complete('owners') or []))
monkeypatch.setattr(residual_sessions, 'query_process_snapshot', lambda: (complete('snapshot') or []))
monkeypatch.setattr(residual_sessions, '_process_windows', lambda *_: {})
hold = file_probe.hold_release_files
@contextmanager
def files(*args, **kwargs):
with hold(*args, **kwargs) as proof:
complete('files')
yield proof
monkeypatch.setattr(file_probe, 'hold_release_files', files)
write_text, write_bytes = Path.write_text, Path.write_bytes
def text(path, content, *args, **kwargs):
result = write_text(path, content, *args, **kwargs)
if path.name == 'release.json' and json.loads(content)['status'] == 'RELEASE_PENDING':
complete('pending_record')
return result
def data(path, content, *args, **kwargs):
result = write_bytes(path, content, *args, **kwargs)
if path.name.startswith('blocker-before-'): complete('archive')
return result
monkeypatch.setattr(Path, 'write_text', text)
monkeypatch.setattr(Path, 'write_bytes', data)
yield library, lease, offset, state, steps
cleanup(lease)
@pytest.mark.parametrize('ui', [False, True])
def test_aggregate_completed_release_checks_keep_final_proof_visible(release_work, tmp_path, monkeypatch, qtbot, ui):
library, lease, offset, state, steps = release_work
def advancing(name):
offset[0] += 6 if name in ('pending_record', 'archive') else 4
time.sleep(.06) # The real context waiter observes each completed boundary.
state['delay'] = advancing
if ui:
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda *_: {'library':str(library),'sessions':[]})
dialog = ResidualReleaseDialog(library, tmp_path/'evidence')
qtbot.addWidget(dialog); dialog.show()
qtbot.waitUntil(lambda: not dialog.busy)
proofs = []; dialog.released.connect(proofs.append)
dialog.confirm_button.click()
qtbot.waitUntil(lambda: not dialog.busy, timeout=5000)
assert len(proofs) == 1, dialog.message.text()
assert '已释放 0 个会话' in dialog.message.text()
proof = proofs[0]
else:
context = operation_queue.BatchTaskContext(str(library))
try:
proof = context.run(lambda: library_lock.release_retained_library(library, tmp_path/'evidence'),
timeout_seconds=10)
finally: context.close(timeout_seconds=3)
assert proof['status'] == 'RELEASE_CONFIRMED'
assert proof['exclusive_files'] == [str(library)]
assert offset[0] >= 24 and steps.count('owners') == 2
assert not library_lock.has_retained_library(library)
assert not library_lock.blocker_path(library).exists()
@pytest.mark.parametrize('step', ['owners', 'files', 'pending_record', 'archive'])
@pytest.mark.parametrize('cancel', [False, True])
def test_stalled_or_cancelled_completed_step_cannot_commit_release(release_work, tmp_path, step, cancel):
library, lease, offset, state, steps = release_work
marker = library_lock.blocker_path(library); original = marker.read_bytes()
context = operation_queue.BatchTaskContext(str(library)); done = Event()
delayed = []
def delayed_step(name):
if name == step and not delayed:
delayed.append(name)
if cancel: context.cancel()
offset[0] += 11
time.sleep(.08)
state['delay'] = delayed_step
def release():
try: return library_lock.release_retained_library(library, tmp_path/'evidence', stop=context.stop)
finally: done.set()
try:
with pytest.raises(operation_queue.QueueCancelled): context.run(release, timeout_seconds=10)
assert done.wait(3), 'Observe completion before closing the context; close must not mask late release'
assert delayed == [step]
assert marker.read_bytes() == original
assert library_lock.has_retained_library(library)
for path in (tmp_path/'evidence').rglob('release.json'):
assert json.loads(path.read_text())['status'] != 'RELEASE_CONFIRMED'
finally: context.close(timeout_seconds=3)