fix: hand off UI shutdown without releasing native ownership

This commit is contained in:
阿白叔 committed 2026-11-16 23:28:36 +08:00
1 parent cd7a5e0ca1
commit dbc685b9a5
10 files changed
+454 -52

No files matched your search

+64 -4
View File
@@ -67,6 +67,10 @@ class _Call:
done: Event = field(default_factory=Event)
abandoned: bool = False
native_depth: int = 0
readonly_depth: int = 0
supervised_depth: int = 0
worker_sessions: list = field(default_factory=list)
shutdown_handoff: bool = False
native_handoff: str = ''
handoff_cancelled: bool = False
phase_started: float = field(default_factory=time.monotonic)
@@ -102,6 +106,24 @@ def remember_result(result):
return result
def bind_supervised_worker(path, record):
"""Called only after the common launcher has durably registered its worker."""
call = getattr(_local, 'call', None)
if call is not None:
with _tasks_guard:
call.worker_sessions.append(dict(path=str(path), operation_id=record['operation_id'],
processes=record['processes'], worker_lease_handle=record.get('worker_lease_handle'),
worker_protection=record.get('worker_protection'), resource_scope=record.get('resource_scope')))
def check_worker_admission(job):
check_active()
call = getattr(_local, 'call', None)
if (call is not None and call.readonly_depth and job.get('inspection_only') is not True
and job.get('operation') != 'catalog'):
raise RuntimeError('NATIVE_ROLE_REQUIRED: 只读观察不能启动原生写入进程。')
def hold_native_handoff(process, session_record_path):
"""Yield parent admission to controls; the gated worker retains write exclusion.
@@ -118,29 +140,39 @@ def hold_native_handoff(process, session_record_path):
@contextmanager
def native_phase():
def native_phase(*, read_only=False, supervised=False):
"""Never abandon a transaction between native writes (including Remove/Add)."""
call = getattr(_local, 'call', None)
if call is not None and call.metadata_only:
raise RuntimeError('A metadata-only call cannot enter native work')
if call is not None:
if call.readonly_depth and not read_only:
raise RuntimeError('NATIVE_ROLE_REQUIRED: 只读观察不能提交原生写入。')
if not read_only and (call.context.native_policy == 'restricted'
or call.context.native_policy == 'external' and not supervised):
raise RuntimeError('NATIVE_ROLE_REQUIRED: 界面写入必须通过已登记的独立工作进程。')
with _tasks_guard:
check_active()
if call is not None:
call.native_depth += 1
call.readonly_depth += int(read_only)
call.supervised_depth += int(supervised)
try:
yield
finally:
if call is not None:
with _tasks_guard:
call.native_depth -= 1
call.readonly_depth -= int(read_only)
call.supervised_depth -= int(supervised)
call.phase_started = time.monotonic()
@contextmanager
def native_channel(*, inspection_only=False):
def native_channel(*, inspection_only=False, read_only=False):
"""Shared by Verify, Repair and backup native clients; reentrant per thread."""
import win32con, win32event
with native_phase():
with native_phase(read_only=read_only or inspection_only, supervised=True):
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = False
try:
@@ -168,8 +200,11 @@ class BatchTaskContext:
operation_id: str = field(default_factory=lambda: uuid4().hex)
stop: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False)
_closed: Event = field(default_factory=Event, repr=False, compare=False)
native_policy: str = field(default='legacy', kw_only=True)
def __post_init__(self):
if self.native_policy not in ('legacy', 'restricted', 'external'):
raise ValueError('Unknown native task role')
object.__setattr__(self, 'library', str(self.library))
for name in ('aliases', 'targets', 'resources'):
object.__setattr__(self, name, tuple(str(v) for v in getattr(self, name)))
@@ -195,6 +230,29 @@ class BatchTaskContext:
'RUNNING' if pending else 'IDLE', pending=pending,
pending_native=any(c.native_depth for c in calls))
def shutdown(self):
"""Yield UI supervision, never a native worker's write/lease ownership.
External-role code only dispatches through register_worker_session. An
empty binding list means pre-dispatch preparation; its late gate is
cancelled by that hook. Legacy in-process transactions cannot opt in.
"""
self._closed.set()
self.cancel()
with _tasks_guard:
calls = [c for c in _calls.values() if c.context is self and not c.done.is_set()]
for call in calls:
readonly = call.native_depth and call.readonly_depth == call.native_depth
external = (self.native_policy == 'external' and
call.supervised_depth == call.native_depth and
all(s.get('worker_protection') in ('duplicated_lease', 'worker_owned_lease', 'inspection_only')
for s in call.worker_sessions))
if not call.native_depth or readonly or external:
call.shutdown_handoff = call.abandoned = True
return {**self.snapshot(), 'safe_to_continue': False,
'shutdown_handoff': all(c.shutdown_handoff for c in calls),
'worker_sessions': [s for c in calls for s in c.worker_sessions]}
def cancel(self):
self.stop.set()
@@ -234,7 +292,7 @@ class BatchTaskContext:
aliases = {_path_key(v) for v in (self.library, *self.aliases)}
call = _Call(self, stop, metadata_only=metadata_only)
with _tasks_guard:
if not metadata_only and any(c.abandoned and not c.metadata_only and aliases.intersection(
if not metadata_only and self.native_policy != 'restricted' and any(c.abandoned and not c.metadata_only and aliases.intersection(
_path_key(v) for v in (c.context.library, *c.context.aliases))
for c in _calls.values()):
raise TaskHandoff(self)
@@ -254,6 +312,8 @@ class BatchTaskContext:
Thread(target=execute, name='cbb-task-'+self.operation_id[:12], daemon=True).start()
while not call.done.wait(.02):
with _tasks_guard:
if call.shutdown_handoff:
raise TaskHandoff(self, cancelled=True, result=call.last_result)
if call.native_handoff:
cancelled = call.handoff_cancelled
call.abandoned = True
+15 -2
View File
@@ -128,14 +128,27 @@ def library_wait_reason(library):
def register_worker_session(worker, job):
"""The launcher must persist identity before opening the worker's gate."""
path = Path(job['session_record_path'])
from .operation_queue import check_worker_admission, current_context, bind_supervised_worker
check_worker_admission(job)
context = current_context()
protection = ('duplicated_lease' if job.get('worker_lease_handle') else
'worker_owned_lease' if job.get('operation') == 'native_backup' else
'inspection_only' if job.get('inspection_only') is True else None)
if context is not None and context.native_policy == 'external':
if protection is None or worker.pid == os.getpid():
raise RuntimeError('WORKER_OWNERSHIP_REQUIRED: 原生写入未登记独立进程和租约。')
from .icdb_ownership import session_resource_scope
write_session_record(path, {k: job.get(k) for k in (
record = ({k: job.get(k) for k in (
'operation_id', 'operation', 'library', 'block', 'desktop', 'cancel_signal', 'worker_lease_handle')}
| {'processes': [{'role': 'worker', **process_identity(worker.pid)}],
'lease_state': 'ACTIVE', 'resource_scope': session_resource_scope(job),
'blocked_resources': [], 'data_recovery_required': None})
'worker_protection': protection, 'task_operation_id': context.operation_id if context else None,
'blocked_resources': [], 'data_recovery_required': None})
write_session_record(path, record)
bind_supervised_worker(path, record)
worker.session_record_path = path
worker.cbb_library = job['library']
check_worker_admission(job) # A slow registration must not open a gate after UI shutdown.
def update_worker_state(job, phase, *, identities=(), observed_identities=(), write_outcome=None,
@@ -103,7 +103,7 @@ class BackupRecoveryDialog(QDialog):
self.status_label.setText('相同任务已提交,未重复执行。')
return
self.busy = True
self.context = BatchTaskContext(str(request.lmc_file), targets=(name,))
self.context = BatchTaskContext(str(request.lmc_file), targets=(name,), native_policy='external')
context = self.context
self.stop = context.stop
self._set_enabled(False)
+2 -1
View File
@@ -16,7 +16,8 @@ class IcdbCheckTask(QObject):
self.requests = tuple(requests)
self.queue, self.report_root = queue, report_root
self.path_lease = path_lease
self.context = BatchTaskContext(str(self.requests[0].lmc_file), targets=tuple(r.block_name for r in self.requests))
self.context = BatchTaskContext(str(self.requests[0].lmc_file),
targets=tuple(r.block_name for r in self.requests), native_policy='external')
self.status_reader = status_reader
self.verifier = verifier or (lambda req: run_verify(req, progress=self._emit_event,
stop=self.stop, version_prompt=choice_prompt))
+62 -37
View File
@@ -77,10 +77,6 @@ STATUS_LABELS = {
'CRASHED': '状态桥崩溃',
}
# Qt must not destroy a pool whose native observer is still handing off.
_shutdown_pools = set()
def application_icon_path():
return Path(__file__).resolve().parents[1] / 'assets' / 'cbb_refresh_verify.ico'
@@ -137,6 +133,9 @@ class MainWindow(QWidget):
self._lease_pending = 0
self._lease_workers = set()
self._lease_contexts = set()
self._shutdown_leases = {}
self._retention_intents = {}
self._retention_ready = set()
self._mapping_busy = 0
self._mapping_owners = Counter()
self._mapping_auto_owners = set()
@@ -367,7 +366,9 @@ class MainWindow(QWidget):
def _new_job(self, kind, *, library=None, targets=(), lease=None):
library = str(library or self.mentor_lmc_path or self.lmc_edit.text())
lease = self.library_lease if lease is None else lease
context = BatchTaskContext(library, aliases=tuple(_library_path_keys(library, lease)), targets=tuple(targets))
policy = 'external' if kind in ('verify','management','recovery','icdb','catalog','status','native_session') else 'restricted'
context = BatchTaskContext(library, aliases=tuple(_library_path_keys(library, lease)),
targets=tuple(targets), native_policy=policy)
job = SimpleNamespace(context=context, kind=kind, revision=self._view_revision,
lease=lease, requests=(), records={}, active=True, keep_session=False)
self._jobs[context.operation_id] = job
@@ -399,9 +400,11 @@ class MainWindow(QWidget):
paused.resume()
job.active = False
self._jobs.pop(job.context.operation_id, None)
job.context.close(timeout_seconds=0)
state = job.context.close(timeout_seconds=0)
if job.lease is not None and job.lease is not self.library_lease:
if not any(other.lease is job.lease for other in self._jobs.values()):
if state['pending'] or id(job.lease) in self._shutdown_leases:
self._retain_path_lease(job.lease)
elif not any(other.lease is job.lease for other in self._jobs.values()):
self._close_path_lease(job.lease)
if self._job_current(job): self._finished()
@@ -420,11 +423,20 @@ class MainWindow(QWidget):
self.logging_context.logger.warning('library_lease_release_failed error=%s', error)
def _close_path_lease(self, lease):
if id(lease) in self._shutdown_leases: return
if id(lease) in self._retention_intents and id(lease) not in self._retention_ready: return
self._lease_action(lease.close)
def _retain_path_lease(self, lease, *, hold_busy=False):
if lease is not None:
self._lease_action(lease.retain_for_recovery, hold_busy=hold_busy)
self._retention_intents[id(lease)] = lease
self._retention_ready.discard(id(lease))
def retained(_):
self._retention_ready.add(id(lease))
if (not self._closing and lease is not self.library_lease
and not any(job.lease is lease for job in self._jobs.values())):
self._close_path_lease(lease)
self._lease_action(lease.retain_for_recovery, retained, hold_busy=hold_busy, completed_always=True)
def _lease_action(self, action, completed=None, failed=None, *, hold_busy=False,
library=None, completed_always=False):
@@ -442,7 +454,8 @@ class MainWindow(QWidget):
self.busy = True
self._update()
library = library or getattr(getattr(action,'__self__',None),'mentor_path',None)
context = BatchTaskContext(str(library or self.mentor_lmc_path or self.loaded_path or self.data_root))
context = BatchTaskContext(str(library or self.mentor_lmc_path or self.loaded_path or self.data_root),
native_policy='restricted')
self._lease_contexts.add(context)
worker = ServiceWorker(lambda:context.run(action))
self._lease_workers.add(worker)
@@ -636,6 +649,7 @@ class MainWindow(QWidget):
revision, owner_job, lease = self._view_revision, self._current_job, self.library_lease
self.release_dialog = ResidualReleaseDialog(library, self.data_root/'release-checks', self)
dialog = self.release_dialog
dialog.path_lease = lease
self._dialogs.append(dialog)
self.release_dialog.native_queue = self.native_queue
from cbb_refresh_verify.network_path import release_retained_path_leases
@@ -644,7 +658,7 @@ class MainWindow(QWidget):
self.release_dialog.before_release.connect(lambda:release_check.cancel()
if release_check is not None and release_check.active else None)
self.release_dialog.released.connect(lambda result: self._complete_library_release(
library, result, lease=lease, revision=revision))
library, result, lease=lease, revision=revision, owner_job=owner_job))
self.release_dialog.finished.connect(lambda *_:self._release_dialog_finished()
if revision == self._view_revision and owner_job is self._current_job else None)
self.busy = True; self._update()
@@ -669,20 +683,26 @@ class MainWindow(QWidget):
self.summary.setText('会话已释放,可以继续未执行项。')
self.summary.show()
def _complete_library_release(self, library, result, *, lease=None, revision=None):
def _complete_library_release(self, library, result, *, lease=None, revision=None, owner_job=None):
from cbb_refresh_verify.network_path import release_retained_path_leases
lease = self.library_lease if lease is None else lease
revision = self._view_revision if revision is None else revision
if revision is None:
revision, owner_job = self._view_revision, self._current_job
def current():
return not self._closing and revision == self._view_revision and owner_job is self._current_job
def ready():
self._management_unsafe_libraries.discard(_path_key(library))
if revision == self._view_revision and not self._closing: self._library_release_ready(library, result)
if lease is not None and not getattr(lease, '_retained_for_recovery', False):
self._retention_intents.pop(id(lease), None)
self._retention_ready.discard(id(lease))
if current(): self._library_release_ready(library, result)
try:
if (not isinstance(result, dict) or result.get('status') != 'RELEASE_CONFIRMED'
or not (_library_path_keys(result.get('library'), lease)
& _library_path_keys(library, lease))):
raise ValueError('RELEASE_PROOF_UNCONFIRMED: 释放证明未确认或不属于当前中心库。')
except Exception as error:
if revision == self._view_revision: self._display_error(error)
if current(): self._display_error(error)
return
if result.get('mapping_cleanup_complete') is True:
ready()
@@ -694,8 +714,8 @@ class MainWindow(QWidget):
if lease is not None: lease.retain_for_recovery()
raise
self._lease_action(release, lambda _: ready(),
lambda error:self._display_error(error) if revision == self._view_revision else None,
hold_busy=revision == self._view_revision, library=library, completed_always=True)
lambda error:self._display_error(error) if current() else None,
hold_busy=current(), library=library, completed_always=True)
def _library_release_ready(self, library, result):
current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text().strip())
@@ -1382,7 +1402,7 @@ class MainWindow(QWidget):
def _start_catalog(self, read_job, initial, lease, library, deliver):
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
context = BatchTaskContext(str(library), aliases=tuple(_library_path_keys(library, lease)))
context = BatchTaskContext(str(library), aliases=tuple(_library_path_keys(library, lease)), native_policy='external')
job = SimpleNamespace(context=context, kind='catalog', revision=read_job.revision,
lease=lease, requests=(), records={}, active=True, keep_session=False)
self._jobs[context.operation_id] = job
@@ -1851,7 +1871,8 @@ class MainWindow(QWidget):
if callback:
callback(records)
library, lease = self.mentor_lmc_path or self.loaded_path, self.library_lease
self._start(lambda: self._read_affected_records(targets, library=library, lease=lease), refreshed)
job = self._new_job('status', library=library, targets=targets, lease=lease)
self._start(lambda: self._read_affected_records(targets, library=library, lease=lease), refreshed, job=job)
return True
def _progress(self, event):
@@ -2167,8 +2188,16 @@ class MainWindow(QWidget):
else:
self._closing = True
self._close_started = time.monotonic()
for job in tuple(self._jobs.values()):
job.context.close(timeout_seconds=0)
owned = {**self._retention_intents, **{id(job.lease):job.lease for job in self._jobs.values()
if job.lease is not None and (job.active or job.keep_session or job.context.snapshot()['pending'])}}
for dialog in self._dialogs:
lease = getattr(dialog,'path_lease',None)
if lease is not None and getattr(dialog,'busy',False): owned[id(lease)] = lease
if self._native_backup_session is not None:
lease = self._native_backup_session.path_lease
if lease is not None: owned[id(lease)] = lease
self._shutdown_leases.update(owned)
handoffs = [job.context.shutdown() for job in tuple(self._jobs.values())]
for context in tuple(self._lease_contexts): context.close(timeout_seconds=0)
for context in tuple(self._lifecycle_contexts): context.close(timeout_seconds=0)
self.native_queue.wake()
@@ -2180,19 +2209,26 @@ class MainWindow(QWidget):
if dialog is not None and getattr(dialog, 'busy', False):
dialog.stop.set()
context = getattr(dialog, 'context', None)
if context is not None: context.close(timeout_seconds=0)
if context is not None: handoffs.append(context.shutdown())
self.busy = True; self._update()
self.setWindowTitle('CBB 刷新校验 · 等待任务安全结束')
self.status_label.setText('正在等待当前操作安全结束,完成后关闭。')
self.close_wait_timer.start()
retained = None
if self._native_backup_session is not None:
self.native_backup_timer.stop()
retained = self._native_backup_session.path_lease
lease, self.library_lease = self.library_lease, None
def release():
if retained is not None: retained.retain_for_recovery()
if lease is not None and lease is not retained: lease.close()
for retained in owned.values(): retained.retain_for_recovery()
if lease is not None and id(lease) not in owned: lease.close()
# Original worker/session registrations already precede native
# admission; this receipt records the UI shutdown, never release.
from cbb_refresh_verify.session_record import write_session_record
receipt = self.data_root/'shutdown'/('close-'+str(time.time_ns())+'.json')
receipt.parent.mkdir(parents=True, exist_ok=True)
write_session_record(receipt,
dict(status='UNCONFIRMED',safe_to_continue=False,task_scopes=handoffs,
retained_mappings=[dict(display=str(getattr(item,'display_path','')),
mentor=str(getattr(item,'mentor_path',''))) for item in owned.values()]))
self._lease_action(release)
self._close_when_idle()
event.ignore()
@@ -2213,20 +2249,9 @@ class MainWindow(QWidget):
active = active or bool(pool is not None and pool.activeThreadCount())
except RuntimeError:
pass # Qt may already have deleted a closed dialog's pool.
if (self._lease_pending or active) and time.monotonic()-getattr(self,'_close_started',time.monotonic()) < 30:
if self._lease_pending or active:
QTimer.singleShot(20, self._close_when_idle)
return
if active:
for pool in (self.lifecycle_pool, self.lease_pool):
pool.setParent(None)
_shutdown_pools.add(pool)
for owner in owners:
pool = getattr(owner, 'pool', None)
if pool is not None:
try:
pool.setParent(None)
_shutdown_pools.add(pool)
except RuntimeError: pass
self.close_wait_timer.stop()
self._close_ready = True
self.close()
@@ -59,7 +59,7 @@ class ManagementDialog(QDialog):
evidence_dir=self.template.evidence_dir.parent/'reimport-recovery', **kwargs)
self.busy = False
self.executed = False
self.context = BatchTaskContext(str(self.template.library))
self.context = BatchTaskContext(str(self.template.library), native_policy='external')
self.stop = self.context.stop
self.ui_job = None
self.admit = None
@@ -177,7 +177,7 @@ class ManagementDialog(QDialog):
if self.admit is not None and not self.admit(tuple(requests)):
self.status_label.setText('相同任务已提交,未重复执行。')
return
self.context = BatchTaskContext(str(self.template.library), targets=tuple(r.name for r in requests))
self.context = BatchTaskContext(str(self.template.library), targets=tuple(r.name for r in requests), native_policy='external')
context = self.context
self.stop = context.stop
self.busy = True; self.executed = True
+2 -2
View File
@@ -889,7 +889,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
timing.begin('content_check')
try:
from .operation_queue import native_channel
with native_channel():
with native_channel(read_only=True):
exit_code, stdout, stderr, timed_out = runner(command, request.timeout_seconds, Path(request.lmc_file).parent)
finally:
timing.end('content_check')
@@ -1358,7 +1358,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
if progress:
_notify_progress(progress, {'stage': 'library_flag_probe', 'block': request.block_name, **record})
try:
with native_channel():
with native_channel(read_only=True):
probe = read_library_flag_with_retry(
flag_reader or read_library_flag_if_available, Path(request.lmc_file),
request.block_name, on_attempt=record_attempt)
+22 -1
View File
@@ -115,7 +115,28 @@ def test_reader_never_creates_a_desktop_or_process(monkeypatch, tmp_path, reader
import win32service
import win32process
import cbb_refresh_verify.mentor_adapter as module
from pywinauto import Desktop
import sys
import pythoncom
if 'pywinauto' not in sys.modules:
# Match the real worker's STA apartment. pywinauto's first import probes
# MTA and may report its fallback; validate that exact diagnostic only.
import warnings
pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
try:
with warnings.catch_warnings(record=True) as diagnostics:
warnings.simplefilter('always')
from pywinauto import Desktop
for diagnostic in diagnostics:
if diagnostic.category is UserWarning and str(diagnostic.message) in (
'Revert to STA COM threading mode', 'Apply externally defined coinit_flags: 2'):
assert sys.coinit_flags == pythoncom.COINIT_APARTMENTTHREADED
else:
warnings.warn_explicit(diagnostic.message, diagnostic.category,
diagnostic.filename, diagnostic.lineno)
finally:
pythoncom.CoUninitialize()
else:
from pywinauto import Desktop
monkeypatch.setattr(Desktop, 'windows', lambda *_: [])
closed = []
+281
View File
@@ -0,0 +1,281 @@
"""Actual process shutdown and captured native ownership regressions."""
from pathlib import Path
from threading import Event
from types import SimpleNamespace
import json
import os
import subprocess
import sys
import time
import pytest
from test_issue15_ui_integration import window_for, load
def test_close_retains_captured_mapping_while_native_supervisor_is_active(qtbot,tmp_path,monkeypatch):
from cbb_refresh_verify.operation_queue import native_channel
from cbb_refresh_verify.network_path import LibraryPathLease
from test_network_path import FakeWNet
window=window_for(qtbot,tmp_path,monkeypatch)
load(window,qtbot)
backend=FakeWNet()
lease=LibraryPathLease.acquire(r'\\server\share\A\library.lmc',backend=backend)
window.library_lease=lease; window.mentor_lmc_path=lease.mentor_path
entered,release=Event(),Event()
def action():
with native_channel(): entered.set(); release.wait(5)
job=window._new_job('verify',library=lease.mentor_path)
window._start(action,lambda _:None,job=job)
qtbot.waitUntil(entered.is_set)
try:
window.close(); qtbot.wait(150)
assert not lease._closed, 'active native owner lost its captured mapping'
assert lease._retained_for_recovery
finally:
release.set(); qtbot.waitUntil(lambda:not window.isVisible())
lease.release_after_recovery({'status':'RELEASE_CONFIRMED','library':str(lease.mentor_path)})
lease.close()
def test_old_release_proof_cleans_its_lease_without_updating_new_operation(qtbot,tmp_path,monkeypatch):
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
monkeypatch.setattr(ResidualReleaseDialog,'refresh',lambda self:None)
cleaned=[]
monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases',
lambda proof,**kw:cleaned.append((proof,kw)))
window=window_for(qtbot,tmp_path,monkeypatch); load(window,qtbot)
first,second=Event(),Event()
window._start(lambda:first.wait(5),lambda _:None,queued=False)
window.check_library_release(); panel=window.release_dialog
first.set(); qtbot.waitUntil(lambda:not window.busy)
window._start(lambda:second.wait(5),lambda _:None,queued=False)
window.status_label.setText('NEW OPERATION STATUS')
pending={'library':Path('a.lmc'),'names':['A'],'maintenance':False,'released':False}
window._resume_batch=pending
try:
panel.released.emit({'status':'RELEASE_CONFIRMED','library':'a.lmc','evidence_path':'old-proof.json'})
qtbot.waitUntil(lambda:bool(cleaned)); qtbot.waitUntil(lambda:window._lease_pending==0)
assert window.status_label.text()=='NEW OPERATION STATUS'
assert pending['released'] is False
assert cleaned[0][0]['evidence_path']=='old-proof.json'
finally:
second.set(); qtbot.waitUntil(lambda:not window.busy); panel.close()
def test_shutdown_role_rejects_unregistered_ui_write_before_its_body(qtbot,tmp_path,monkeypatch):
from cbb_refresh_verify.operation_queue import native_phase
window=window_for(qtbot,tmp_path,monkeypatch); load(window,qtbot)
effects=[]
def action():
with native_phase(): effects.append('write')
window._start(action,lambda _:None)
qtbot.waitUntil(lambda:not window.busy)
assert effects==[]
assert 'NATIVE_ROLE_REQUIRED' in window.status_label.text()
def test_readonly_native_phase_process_really_exits_while_observer_remains_pending(tmp_path):
script=tmp_path/'readonly_app.py'
script.write_text('''from threading import Event
from pathlib import Path
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QTimer
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.operation_queue import native_phase
app=QApplication([])
MainWindow._show_unfinished_management=lambda self:None
w=MainWindow(reader=lambda _:None,data_root=Path(sys.argv[1])); w.show()
def action():
with native_phase(read_only=True):
print('READONLY_PHASE_ACTIVE',flush=True)
Event().wait(90)
w._start(action,lambda _:None)
QTimer.singleShot(250,w.close)
app.exec()
print('APP_RETURNED',flush=True)
''',encoding='utf-8')
process=subprocess.Popen([sys.executable,'-X','utf8',str(script),str(tmp_path/'data')],
stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,encoding='utf-8',
env=dict(os.environ,QT_QPA_PLATFORM='offscreen'))
try:
stdout,stderr=process.communicate(timeout=8)
assert process.returncode==0,stderr
assert 'READONLY_PHASE_ACTIVE' in stdout and 'APP_RETURNED' in stdout
finally:
if process.poll() is None: process.kill(); process.communicate()
def test_main_process_exit_hands_off_exact_worker_with_sidecar_and_writer_barrier(tmp_path):
import win32api,win32con,win32event
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
from cbb_refresh_verify.operation_queue import native_channel
library=tmp_path/'lib.lmc'; library.write_bytes(b'isolated metadata')
child_script=tmp_path/'protected_child.py'
child_script.write_text('''import sys,time,json,os
from pathlib import Path
from cbb_refresh_verify.desktop_worker import worker_native_channel
root=Path(sys.argv[1])
while not (root/'gate').exists(): time.sleep(.01)
session=json.loads((root/'session.json').read_text('utf-8'))
with worker_native_channel(lease_handle=session['worker_lease_handle']):
(root/'ready').write_text(str(os.getpid()))
while not (root/'finish').exists(): time.sleep(.02)
(root/'done').touch()
''',encoding='utf-8')
script=tmp_path/'owner_app.py'
script.write_text('''import sys,time,subprocess,win32api,win32con
from pathlib import Path
from threading import Event
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QTimer
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.operation_queue import native_channel
from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease
from cbb_refresh_verify.session_record import register_worker_session
root=Path(sys.argv[1]); library=root/'lib.lmc'
app=QApplication([]); MainWindow._show_unfinished_management=lambda self:None
w=MainWindow(reader=lambda _:None,data_root=root/'data'); w.show()
def action():
with LibraryLock(library,checkpoint=False), native_channel():
child=subprocess.Popen([sys.executable,'-X','utf8',str(root/'protected_child.py'),str(root)],
stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
handle=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,child.pid)
try: lease=duplicate_worker_lease(library,handle)
finally: handle.Close()
register_worker_session(child,dict(library=str(library),block='A',operation='verify',
operation_id='protected-child',session_record_path=str(root/'session.json'),
worker_lease_handle=lease,cancel_signal=str(root/'cancel')))
(root/'gate').touch()
Event().wait(90)
job=w._new_job('verify',library=library,targets=('A',))
w._start(action,lambda _:None,job=job)
def close_ready():
if (root/'ready').exists():
print('PROTECTED_WRITER_ACTIVE',flush=True); w.close()
else: QTimer.singleShot(20,close_ready)
QTimer.singleShot(20,close_ready)
app.exec(); print('APP_RETURNED',flush=True)
''',encoding='utf-8')
started=time.monotonic()
process=subprocess.Popen([sys.executable,'-X','utf8',str(script),str(tmp_path)],
stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,encoding='utf-8',
env=dict(os.environ,QT_QPA_PLATFORM='offscreen'))
try:
stdout,stderr=process.communicate(timeout=36)
assert process.returncode==0,stderr
assert 'PROTECTED_WRITER_ACTIVE' in stdout and 'APP_RETURNED' in stdout
assert time.monotonic()-started<30
record=json.loads((tmp_path/'session.json').read_text('utf-8'))
pid=int((tmp_path/'ready').read_text())
assert record['processes'][0]['pid']==pid and record['worker_lease_handle']
receipt=json.loads(next((tmp_path/'data'/'shutdown').glob('*.json')).read_text('utf-8'))
assert receipt['safe_to_continue'] is False and receipt['status']=='UNCONFIRMED'
bindings=[binding for scope in receipt['task_scopes'] for binding in scope['worker_sessions']]
assert any(binding['operation_id']=='protected-child' and binding['path']==str(tmp_path/'session.json')
and binding['worker_protection']=='duplicated_lease' for binding in bindings)
assert record['task_operation_id'] in {scope['operation_id'] for scope in receipt['task_scopes']}
handle=win32api.OpenProcess(win32con.SYNCHRONIZE,False,pid)
try: assert win32event.WaitForSingleObject(handle,0)==win32con.WAIT_TIMEOUT
finally: handle.Close()
with pytest.raises(LibraryBusy):
with LibraryLock(library,checkpoint=False): pass
with pytest.raises(RuntimeError,match='NATIVE_WRITER_ACTIVE'):
with native_channel(): pass
finally:
(tmp_path/'finish').touch()
if process.poll() is None: process.kill(); process.communicate()
deadline=time.monotonic()+5
while (tmp_path/'ready').exists() and not (tmp_path/'done').exists() and time.monotonic()<deadline:
time.sleep(.02)
assert (tmp_path/'done').exists()
with LibraryLock(library,checkpoint=False), native_channel(): pass
def test_shutdown_during_registration_returns_waiter_and_never_opens_late_gate(tmp_path,monkeypatch):
from threading import Thread
import win32api,win32con
from cbb_refresh_verify import session_record
from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease
from cbb_refresh_verify.operation_queue import BatchTaskContext,TaskHandoff,native_channel
library=tmp_path/'lib.lmc'; library.write_bytes(b'isolated')
context=BatchTaskContext(str(library),native_policy='external')
entered,resume,returned=Event(),Event(),Event(); gates=[]; child=[]
original=session_record.write_session_record
def stalled(path,value):
entered.set(); resume.wait(5); return original(path,value)
monkeypatch.setattr(session_record,'write_session_record',stalled)
def action():
with LibraryLock(library,checkpoint=False), native_channel():
process=subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
child.append(process)
handle=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,process.pid)
try: lease=duplicate_worker_lease(library,handle)
finally: handle.Close()
session_record.register_worker_session(process,dict(library=str(library),operation='verify',
operation_id='registration',session_record_path=str(tmp_path/'session.json'),worker_lease_handle=lease))
gates.append('late native dispatch')
def run():
try: context.run(action)
except TaskHandoff: pass
finally: returned.set()
thread=Thread(target=run); thread.start(); assert entered.wait(2)
try:
state=context.shutdown()
assert state['safe_to_continue'] is False and returned.wait(1)
resume.set()
deadline=time.monotonic()+3
while context.snapshot()['pending'] and time.monotonic()<deadline: time.sleep(.01)
assert not context.snapshot()['pending']
assert gates==[]
finally:
resume.set(); thread.join(3)
for process in child: process.communicate('\n',timeout=3)
def test_readonly_observer_cannot_enter_a_mutating_phase(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext,native_phase,native_channel
effects=[]
def action():
with native_phase(read_only=True):
with native_channel(): effects.append('write')
with pytest.raises(RuntimeError,match='NATIVE_ROLE_REQUIRED'):
BatchTaskContext(str(tmp_path/'lib.lmc'),native_policy='external').run(action)
assert effects==[]
def test_readonly_role_rejects_mutating_worker_at_common_registration(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext,native_phase
from cbb_refresh_verify.session_record import register_worker_session
process=subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
effects=[]
def action():
with native_phase(read_only=True):
register_worker_session(process,dict(library=str(tmp_path/'lib.lmc'),operation='remove',
operation_id='forbidden',session_record_path=str(tmp_path/'session.json'),worker_lease_handle=1))
effects.append('write gate')
try:
with pytest.raises(RuntimeError,match='NATIVE_ROLE_REQUIRED'):
BatchTaskContext(str(tmp_path/'lib.lmc'),native_policy='external').run(action)
assert effects==[]
finally: process.communicate('\n',timeout=3)
def test_shutdown_does_not_abandon_a_legacy_inprocess_mutation(tmp_path):
from threading import Thread
from cbb_refresh_verify.operation_queue import BatchTaskContext,native_phase
context=BatchTaskContext(str(tmp_path/'lib.lmc'))
entered,release,returned=Event(),Event(),Event()
def action():
with native_phase(): entered.set(); release.wait(3)
def run():
try: context.run(action)
finally: returned.set()
thread=Thread(target=run); thread.start(); assert entered.wait(1)
try:
state=context.shutdown()
assert state['pending_native'] and not state['shutdown_handoff']
assert state['safe_to_continue'] is False and not returned.wait(.1)
finally: release.set(); thread.join(3)
assert returned.is_set()
+3 -2
View File
@@ -559,7 +559,8 @@ def test_queue_close_has_one_deadline_and_reports_pending_scope(tmp_path):
thread.join(2)
def test_real_management_transaction_does_not_handoff_between_remove_and_add(management_setup, monkeypatch):
@pytest.mark.parametrize('native_policy', ['legacy', 'external'])
def test_real_management_transaction_does_not_handoff_between_remove_and_add(management_setup, monkeypatch, native_policy):
from dataclasses import replace
from types import SimpleNamespace
from test_management import execute
@@ -576,7 +577,7 @@ def test_real_management_transaction_does_not_handoff_between_remove_and_add(man
time.sleep(.08) # Let the real admission monitor observe the active native phase.
return result
adapter.remove = slow_remove # Native external boundary; transaction stays real.
context = BatchTaskContext(library=str(request.library), targets=('Block',))
context = BatchTaskContext(library=str(request.library), targets=('Block',), native_policy=native_policy)
result = NativeQueue().run(lambda: execute(replace(request, operation='update',
source=source, auto_verify=False), adapter), context=context, timeout_seconds=10)
assert result.status == 'UPLOADED'