fix: fence final worker admission against UI shutdown

This commit is contained in:
阿白叔 committed 2026-11-17 00:00:50 +08:00
1 parent dbc685b9a5
commit eabf61c73b
13 files changed
+356 -46

No files matched your search

+5 -2
View File
@@ -100,9 +100,12 @@ def main(argv: Sequence[str] | None = None) -> int:
from .native_backup import run_native_backup_worker from .native_backup import run_native_backup_worker
return run_native_backup_worker(args.native_backup_worker) return run_native_backup_worker(args.native_backup_worker)
if args.management_worker: if args.management_worker:
from .native_management import management_worker_main import json
from .native_management import catalog_worker_main, management_worker_main
from .desktop_worker import run_native_worker from .desktop_worker import run_native_worker
return run_native_worker(management_worker_main, args.management_worker) job = json.loads(args.management_worker.read_text(encoding='utf-8'))
action = catalog_worker_main if job.get('worker_role') == 'catalog_readonly' else management_worker_main
return run_native_worker(action, args.management_worker)
if args.repair_worker: if args.repair_worker:
from .native_repair_ui import repair_worker_main from .native_repair_ui import repair_worker_main
from .desktop_worker import run_native_worker from .desktop_worker import run_native_worker
+24 -2
View File
@@ -197,14 +197,36 @@ def run_native_worker(action, request_path):
raise RuntimeError('WORKER_CANCELLED: 未取得启动许可。') raise RuntimeError('WORKER_CANCELLED: 未取得启动许可。')
threading.Event().wait(.05) threading.Event().wait(.05)
lease_handle = None lease_handle = None
record = None
if job.get('session_record_path'): if job.get('session_record_path'):
from .session_record import read_session_record from .session_record import read_session_record, process_identity, IDENTITY_KEYS
path = Path(job['session_record_path']) path = Path(job['session_record_path'])
record = read_session_record(path, evidence_root=path.parent) record = read_session_record(path, evidence_root=path.parent)
identity = process_identity(os.getpid())
if (record['operation_id'] != job['operation_id'] if (record['operation_id'] != job['operation_id']
or not any(p['pid'] == os.getpid() for p in record.get('processes', ()) )): or any(record.get(key) != job.get(key) for key in (
'operation', 'library', 'block', 'desktop', 'worker_role', 'start_gate_protocol', 'start_gate'))
or not any(p.get('role') == 'worker' and all(p.get(k) == identity[k] for k in IDENTITY_KEYS)
for p in record.get('processes', ()))):
raise RuntimeError('WORKER_ADMISSION_IDENTITY_CHANGED') raise RuntimeError('WORKER_ADMISSION_IDENTITY_CHANGED')
lease_handle = record.get('worker_lease_handle') lease_handle = record.get('worker_lease_handle')
if job.get('start_gate_protocol') == 'event-v1':
import win32api
handle = record.get('worker_start_handle') if record else None
if not handle:
raise RuntimeError('WORKER_ADMISSION_UNCONFIRMED: 缺少启动事件。')
try:
deadline = time.monotonic()+30
while win32event.WaitForSingleObject(int(handle), 50) != win32con.WAIT_OBJECT_0:
if (job.get('cancel_signal') and Path(job['cancel_signal']).exists()
or time.monotonic() >= deadline):
raise RuntimeError('WORKER_CANCELLED: 启动事件未提交。')
finally:
win32api.CloseHandle(int(handle))
if job.get('worker_role') == 'catalog_readonly':
from .native_management import catalog_worker_main
if action is not catalog_worker_main or job.get('operation') != 'catalog':
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
inspection = (action.__module__ == 'cbb_refresh_verify.native_repair_ui' inspection = (action.__module__ == 'cbb_refresh_verify.native_repair_ui'
and action.__name__ == 'repair_worker_main' and job.get('inspection_only') is True) and action.__name__ == 'repair_worker_main' and job.get('inspection_only') is True)
with worker_native_channel(lease_handle=lease_handle, inspection_only=inspection): with worker_native_channel(lease_handle=lease_handle, inspection_only=inspection):
+3 -2
View File
@@ -90,7 +90,7 @@ class NativeBackupSession:
def launch_native_backup(request, *, mode='versions', target_database=None): def launch_native_backup(request, *, mode='versions', target_database=None):
from .library_lock import recheck_previous_release from .library_lock import recheck_previous_release
from .session_record import register_worker_session from .session_record import register_worker_session
from .operation_queue import check_active, native_channel, task_stop from .operation_queue import check_active, native_channel, task_stop, commit_worker_start
project, database = native_backup_target(request, mode=mode, target_database=target_database) project, database = native_backup_target(request, mode=mode, target_database=target_database)
native_backup_executable(request.mentor_bin) native_backup_executable(request.mentor_bin)
root = Path(request.evidence_dir).resolve()/('native-backup-'+uuid4().hex[:12]) root = Path(request.evidence_dir).resolve()/('native-backup-'+uuid4().hex[:12])
@@ -108,6 +108,7 @@ def launch_native_backup(request, *, mode='versions', target_database=None):
'mentor_bin': request.mentor_bin, 'automatic_repair': False, 'mentor_bin': request.mentor_bin, 'automatic_repair': False,
'operation': 'native_backup', 'operation_id': root.name, 'operation': 'native_backup', 'operation_id': root.name,
'session_record_path': str(root/'session.json'), 'start_gate': str(root/'start-worker.signal'), 'session_record_path': str(root/'session.json'), 'start_gate': str(root/'start-worker.signal'),
'start_gate_protocol': 'event-v1',
'cancel_signal': str(root/'cancel-worker.signal'), 'supervisor_pid': os.getpid(), 'desktop': None} 'cancel_signal': str(root/'cancel-worker.signal'), 'supervisor_pid': os.getpid(), 'desktop': None}
job.write_text(json.dumps(record, ensure_ascii=False), 'utf-8') job.write_text(json.dumps(record, ensure_ascii=False), 'utf-8')
command = ([sys.executable] if getattr(sys, 'frozen', False) else command = ([sys.executable] if getattr(sys, 'frozen', False) else
@@ -118,7 +119,7 @@ def launch_native_backup(request, *, mode='versions', target_database=None):
register_worker_session(process, record) register_worker_session(process, record)
with native_channel(): with native_channel():
check_active() check_active()
Path(record['start_gate']).touch() commit_worker_start(process, record)
except BaseException: except BaseException:
Path(record['cancel_signal']).touch() # The unadmitted child exits without native launch. Path(record['cancel_signal']).touch() # The unadmitted child exits without native launch.
raise raise
+24 -6
View File
@@ -53,9 +53,10 @@ def removal_output_ok(code, output, name, partition):
+ r"' from partition '" + re.escape(partition) + r"'\.", output) is not None) + r"' from partition '" + re.escape(partition) + r"'\.", output) is not None)
def catalog_dialog_action(title, text): def catalog_dialog_action(title, text, *, read_only=False):
value = (title + '\n' + text).casefold() value = (title + '\n' + text).casefold()
if ('this central library must be updated' in value or 'select flow type' in value): if ('this central library must be updated' in value or 'select flow type' in value
or read_only and ('save' in value or 'upgrade' in value)):
return 'cancel' return 'cancel'
return 'observe' return 'observe'
@@ -222,6 +223,7 @@ class NativeManagementAdapter:
from .session_record import process_identity from .session_record import process_identity
job = {**job, 'cancel_signal': str(evidence/'cancel-management.signal'), job = {**job, 'cancel_signal': str(evidence/'cancel-management.signal'),
'start_gate': str(evidence/'start-management.signal'), 'start_gate': str(evidence/'start-management.signal'),
'start_gate_protocol': 'event-v1',
'session_record_path': str(evidence/'session.json'), 'operation_id': uuid4().hex, 'session_record_path': str(evidence/'session.json'), 'operation_id': uuid4().hex,
'supervisor_pid': os.getpid(), 'supervisor_identity': process_identity(os.getpid()), 'supervisor_pid': os.getpid(), 'supervisor_identity': process_identity(os.getpid()),
'block': job.get('name')} 'block': job.get('name')}
@@ -242,7 +244,8 @@ class NativeManagementAdapter:
worker.bind_cancellation(job['cancel_signal']) worker.bind_cancellation(job['cancel_signal'])
if hasattr(worker, 'bind_session'): if hasattr(worker, 'bind_session'):
worker.bind_session(job) worker.bind_session(job)
Path(job['start_gate']).touch() from .operation_queue import commit_worker_start
commit_worker_start(worker, job)
while worker.poll() is None and time.monotonic() < deadline: while worker.poll() is None and time.monotonic() < deadline:
if self.stop is not None and self.stop.is_set(): if self.stop is not None and self.stop.is_set():
Path(job['cancel_signal']).touch() Path(job['cancel_signal']).touch()
@@ -275,7 +278,8 @@ class NativeManagementAdapter:
worker.close() worker.close()
def catalog(self, library, evidence): def catalog(self, library, evidence):
result = self._worker({'operation': 'catalog', 'library': str(absolute_preserving_drive(library))}, evidence) result = self._worker({'operation': 'catalog', 'worker_role': 'catalog_readonly',
'library': str(absolute_preserving_drive(library))}, evidence)
if not result.get('success') or not result.get('safe_to_continue'): if not result.get('success') or not result.get('safe_to_continue'):
raise NativeOperationError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'), raise NativeOperationError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'),
safe_to_continue=result.get('safe_to_continue') is True) safe_to_continue=result.get('safe_to_continue') is True)
@@ -1069,6 +1073,8 @@ def _catalog_native(job, root):
def _catalog_com(job, root): def _catalog_com(job, root):
if job.get('worker_role') == 'catalog_readonly' and job.get('operation') != 'catalog':
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
import pythoncom, win32com.client, win32con, win32gui, win32process import pythoncom, win32com.client, win32con, win32gui, win32process
from .mentor_adapter import _process_details from .mentor_adapter import _process_details
try: try:
@@ -1102,7 +1108,7 @@ def _catalog_com(job, root):
win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None) win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None)
title = win32gui.GetWindowText(hwnd) title = win32gui.GetWindowText(hwnd)
text = '\n'.join(win32gui.GetWindowText(child) for child in children) text = '\n'.join(win32gui.GetWindowText(child) for child in children)
if catalog_dialog_action(title, text) == 'cancel': if catalog_dialog_action(title, text, read_only=job.get('worker_role') == 'catalog_readonly') == 'cancel':
if hwnd not in observed: if hwnd not in observed:
observed.append(hwnd) observed.append(hwnd)
refused.append({'title': title, 'text': text}) refused.append({'title': title, 'text': text})
@@ -1236,11 +1242,23 @@ def _catalog_com(job, root):
return result return result
def management_worker_main(request_path): def catalog_worker_main(request_path):
"""A catalog-only entry: never allow this read role to select a write branch."""
job = json.loads(Path(request_path).read_text(encoding='utf-8'))
if job.get('operation') != 'catalog' or job.get('worker_role') != 'catalog_readonly':
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
return management_worker_main(request_path, catalog_readonly=True)
def management_worker_main(request_path, *, catalog_readonly=False):
"""Internal worker. Desktop identity and capability gates are rechecked here.""" """Internal worker. Desktop identity and capability gates are rechecked here."""
request_path = Path(request_path) request_path = Path(request_path)
root = request_path.parent root = request_path.parent
job = json.loads(request_path.read_text(encoding='utf-8')) job = json.loads(request_path.read_text(encoding='utf-8'))
if job.get('worker_role') == 'catalog_readonly' and not catalog_readonly:
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
if catalog_readonly and (job.get('operation') != 'catalog' or job.get('worker_role') != 'catalog_readonly'):
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
result = {'success': False, 'safe_to_continue': True} result = {'success': False, 'safe_to_continue': True}
try: try:
_, desktop = _current_desktop() _, desktop = _current_desktop()
+8 -4
View File
@@ -275,6 +275,7 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr
'operation': 'inspect' if inspection_only else 'repair', 'operation': 'inspect' if inspection_only else 'repair',
'block': (maintenance_context or {}).get('block'), 'block': (maintenance_context or {}).get('block'),
'session_record_path': str(root/'session.json'), 'start_gate': str(root/'start-repair.signal')} 'session_record_path': str(root/'session.json'), 'start_gate': str(root/'start-repair.signal')}
job['start_gate_protocol'] = 'event-v1'
job_path.write_text(json.dumps(job, ensure_ascii=False), encoding='utf-8') job_path.write_text(json.dumps(job, ensure_ascii=False), encoding='utf-8')
command = ([sys.executable] if getattr(sys, 'frozen', False) else command = ([sys.executable] if getattr(sys, 'frozen', False) else
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)] [sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)]
@@ -286,9 +287,7 @@ def _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress,
from .desktop_worker import DesktopWorker from .desktop_worker import DesktopWorker
worker = DesktopWorker(command, desktop) worker = DesktopWorker(command, desktop)
worker.bind_cancellation(cancel) worker.bind_cancellation(cancel)
if hasattr(worker, 'bind_session'): from .operation_queue import check_active, native_channel, commit_worker_start
worker.bind_session(job)
from .operation_queue import check_active, native_channel
native = native_channel(inspection_only=job.get('inspection_only') is True) native = native_channel(inspection_only=job.get('inspection_only') is True)
entered = False entered = False
# Worker admission includes filesystem/ownership checks before the native # Worker admission includes filesystem/ownership checks before the native
@@ -297,10 +296,12 @@ def _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress,
event_count = 0 event_count = 0
cancelled = False cancelled = False
try: try:
if hasattr(worker, 'bind_session'):
worker.bind_session(job)
check_active() check_active()
native.__enter__() native.__enter__()
entered = True entered = True
Path(job['start_gate']).touch() commit_worker_start(worker, job)
while worker.poll() is None and time.monotonic()<deadline: while worker.poll() is None and time.monotonic()<deadline:
if not cancelled and stop is not None and stop.is_set(): if not cancelled and stop is not None and stop.is_set():
worker.request_stop() worker.request_stop()
@@ -331,6 +332,9 @@ def _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress,
if worker.returncode != 0 and result.get('status') in ('REPAIRED', 'NOT_IN_USE', 'IN_USE'): if worker.returncode != 0 and result.get('status') in ('REPAIRED', 'NOT_IN_USE', 'IN_USE'):
result['status'] = 'UNCONFIRMED' result['status'] = 'UNCONFIRMED'
return {**result, 'evidence_path': str(root)} return {**result, 'evidence_path': str(root)}
except BaseException:
worker.request_stop()
raise
finally: finally:
if entered: if entered:
native.__exit__(None, None, None) native.__exit__(None, None, None)
+34 -8
View File
@@ -27,7 +27,8 @@ class _StopSignal:
self._event = Event() self._event = Event()
def set(self): def set(self):
self._event.set() with _tasks_guard:
self._event.set()
def is_set(self): def is_set(self):
return self._event.is_set() return self._event.is_set()
@@ -120,10 +121,35 @@ def check_worker_admission(job):
check_active() check_active()
call = getattr(_local, 'call', None) call = getattr(_local, 'call', None)
if (call is not None and call.readonly_depth and job.get('inspection_only') is not True if (call is not None and call.readonly_depth and job.get('inspection_only') is not True
and job.get('operation') != 'catalog'): and not (job.get('operation') == 'catalog' and job.get('worker_role') == 'catalog_readonly')):
raise RuntimeError('NATIVE_ROLE_REQUIRED: 只读观察不能启动原生写入进程。') raise RuntimeError('NATIVE_ROLE_REQUIRED: 只读观察不能启动原生写入进程。')
def commit_worker_start(worker, job):
"""Prepare evidence outside the shutdown fence; only SetEvent grants admission."""
from pathlib import Path
import win32event
try:
check_worker_admission(job)
event = getattr(worker, '_start_event', None)
context = current_context()
if context is not None and context.native_policy == 'external' and event is None:
raise RuntimeError('WORKER_ADMISSION_UNCONFIRMED: 缺少已登记的启动事件。')
if event is not None and worker._start_binding != (
str(job['session_record_path']), job['operation_id'], job['start_gate']):
raise RuntimeError('WORKER_ADMISSION_IDENTITY_CHANGED')
Path(job['start_gate']).touch()
with _tasks_guard:
check_worker_admission(job)
if event is not None:
win32event.SetEvent(event)
except BaseException:
# A stalled evidence volume cannot delay shutdown or grant admission.
if job.get('cancel_signal'):
Path(job['cancel_signal']).touch()
raise
def hold_native_handoff(process, session_record_path): def hold_native_handoff(process, session_record_path):
"""Yield parent admission to controls; the gated worker retains write exclusion. """Yield parent admission to controls; the gated worker retains write exclusion.
@@ -233,19 +259,19 @@ class BatchTaskContext:
def shutdown(self): def shutdown(self):
"""Yield UI supervision, never a native worker's write/lease ownership. """Yield UI supervision, never a native worker's write/lease ownership.
External-role code only dispatches through register_worker_session. An External workers commit their kernel start event under this same fence.
empty binding list means pre-dispatch preparation; its late gate is Registration and all filesystem preparation remain outside the fence.
cancelled by that hook. Legacy in-process transactions cannot opt in. Legacy in-process transactions cannot opt in.
""" """
self._closed.set()
self.cancel()
with _tasks_guard: with _tasks_guard:
self._closed.set()
self.cancel()
calls = [c for c in _calls.values() if c.context is self and not c.done.is_set()] calls = [c for c in _calls.values() if c.context is self and not c.done.is_set()]
for call in calls: for call in calls:
readonly = call.native_depth and call.readonly_depth == call.native_depth readonly = call.native_depth and call.readonly_depth == call.native_depth
external = (self.native_policy == 'external' and external = (self.native_policy == 'external' and
call.supervised_depth == call.native_depth and call.supervised_depth == call.native_depth and
all(s.get('worker_protection') in ('duplicated_lease', 'worker_owned_lease', 'inspection_only') all(s.get('worker_protection') in ('duplicated_lease', 'worker_owned_lease', 'inspection_only', 'catalog_readonly')
for s in call.worker_sessions)) for s in call.worker_sessions))
if not call.native_depth or readonly or external: if not call.native_depth or readonly or external:
call.shutdown_handoff = call.abandoned = True call.shutdown_handoff = call.abandoned = True
+18 -2
View File
@@ -133,17 +133,33 @@ def register_worker_session(worker, job):
context = current_context() context = current_context()
protection = ('duplicated_lease' if job.get('worker_lease_handle') else protection = ('duplicated_lease' if job.get('worker_lease_handle') else
'worker_owned_lease' if job.get('operation') == 'native_backup' else 'worker_owned_lease' if job.get('operation') == 'native_backup' else
'inspection_only' if job.get('inspection_only') is True else None) 'inspection_only' if job.get('inspection_only') is True else
'catalog_readonly' if job.get('worker_role') == 'catalog_readonly'
and job.get('operation') == 'catalog' else None)
if context is not None and context.native_policy == 'external': if context is not None and context.native_policy == 'external':
if protection is None or worker.pid == os.getpid(): if protection is None or worker.pid == os.getpid():
raise RuntimeError('WORKER_OWNERSHIP_REQUIRED: 原生写入未登记独立进程和租约。') raise RuntimeError('WORKER_OWNERSHIP_REQUIRED: 原生写入未登记独立进程和租约。')
from .icdb_ownership import session_resource_scope from .icdb_ownership import session_resource_scope
record = ({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')} 'operation_id', 'operation', 'library', 'block', 'desktop', 'cancel_signal', 'worker_lease_handle',
'worker_role', 'start_gate_protocol', 'start_gate')}
| {'processes': [{'role': 'worker', **process_identity(worker.pid)}], | {'processes': [{'role': 'worker', **process_identity(worker.pid)}],
'lease_state': 'ACTIVE', 'resource_scope': session_resource_scope(job), 'lease_state': 'ACTIVE', 'resource_scope': session_resource_scope(job),
'worker_protection': protection, 'task_operation_id': context.operation_id if context else None, 'worker_protection': protection, 'task_operation_id': context.operation_id if context else None,
'blocked_resources': [], 'data_recovery_required': None}) 'blocked_resources': [], 'data_recovery_required': None})
if job.get('start_gate_protocol') == 'event-v1':
import win32api, win32con, win32event
event = win32event.CreateEvent(None, True, False, None)
process = getattr(worker, 'handle', None) or getattr(worker, '_handle', None)
if process is None:
raise RuntimeError('WORKER_ADMISSION_UNCONFIRMED: 缺少工作进程句柄。')
# Use the launch handle, never a reopened PID. The child's event survives
# supervisor exit even before Python startup or journal reads finish.
remote = win32api.DuplicateHandle(win32api.GetCurrentProcess(), event,
process, 0, False, win32con.DUPLICATE_SAME_ACCESS)
record['worker_start_handle'] = remote.Detach()
worker._start_event = event
worker._start_binding = (str(path), job['operation_id'], job['start_gate'])
write_session_record(path, record) write_session_record(path, record)
bind_supervised_worker(path, record) bind_supervised_worker(path, record)
worker.session_record_path = path worker.session_record_path = path
+3 -2
View File
@@ -933,7 +933,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
'cancel_signal': str(root/'cancel-worker.signal'), 'cancel_signal': str(root/'cancel-worker.signal'),
'start_gate': str(root/'start-worker.signal')} 'start_gate': str(root/'start-worker.signal')}
job.update(operation_id=root.name, operation='verify', library=str(request.lmc_file), job.update(operation_id=root.name, operation='verify', library=str(request.lmc_file),
block=request.block_name, session_record_path=str(root/'session.json')) block=request.block_name, session_record_path=str(root/'session.json'), start_gate_protocol='event-v1')
from .session_record import process_identity from .session_record import process_identity
job['supervisor_identity'] = process_identity(os.getpid()) job['supervisor_identity'] = process_identity(os.getpid())
consent = None consent = None
@@ -1023,7 +1023,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
except RuntimeError as error: except RuntimeError as error:
return finish(VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False, return finish(VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False,
error_category='NATIVE_CHANNEL_BUSY')) error_category='NATIVE_CHANNEL_BUSY'))
Path(job['start_gate']).touch() from .operation_queue import commit_worker_start
commit_worker_start(process, job)
while process.poll() is None: while process.poll() is None:
if stop is not None and stop.is_set(): if stop is not None and stop.is_set():
save_markers = timing._save_markers save_markers = timing._save_markers
+10 -3
View File
@@ -83,6 +83,7 @@ def test_manual_repair_selects_diagnosed_bound_database_without_layout_or_health
def test_versions_launcher_rechecks_parent_retained_lease_and_persists_before_gate(tmp_path, monkeypatch): def test_versions_launcher_rechecks_parent_retained_lease_and_persists_before_gate(tmp_path, monkeypatch):
import subprocess, sys
from test_native_backup import project_fixture from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native, library_lock, session_record from cbb_refresh_verify import native_backup as native, library_lock, session_record
request, project = project_fixture(tmp_path) request, project = project_fixture(tmp_path)
@@ -90,13 +91,17 @@ def test_versions_launcher_rechecks_parent_retained_lease_and_persists_before_ga
calls = [] calls = []
monkeypatch.setattr(library_lock, 'recheck_previous_release', lambda *a, **k: calls.append('recheck')) monkeypatch.setattr(library_lock, 'recheck_previous_release', lambda *a, **k: calls.append('recheck'))
monkeypatch.setattr(session_record, 'process_identity', lambda pid: dict(pid=pid, created='now', executable='python', owner='test')) monkeypatch.setattr(session_record, 'process_identity', lambda pid: dict(pid=pid, created='now', executable='python', owner='test'))
child = subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
def spawn(command, **kw): def spawn(command, **kw):
job = json.loads(Path(command[-1]).read_text()) job = json.loads(Path(command[-1]).read_text())
assert calls == ['recheck'] and not Path(job['start_gate']).exists() assert calls == ['recheck'] and not Path(job['start_gate']).exists()
calls.append('spawn') calls.append('spawn')
return SimpleNamespace(pid=123, poll=lambda: None) return child
monkeypatch.setattr(native.subprocess, 'Popen', spawn) monkeypatch.setattr(native.subprocess, 'Popen', spawn)
session = native.launch_native_backup(request, mode='repair') try:
session = native.launch_native_backup(request, mode='repair')
finally:
child.communicate('\n',timeout=5)
record = json.loads(session.job.read_text()) record = json.loads(session.job.read_text())
assert Path(record['start_gate']).is_file() assert Path(record['start_gate']).is_file()
assert Path(record['session_record_path']).is_file() assert Path(record['session_record_path']).is_file()
@@ -213,6 +218,7 @@ def test_independent_backup_job_registration_retains_descendants_after_caller_ha
def test_common_admission_refusal_publishes_bound_receipt_and_ui_can_reopen(qtbot, tmp_path, monkeypatch): def test_common_admission_refusal_publishes_bound_receipt_and_ui_can_reopen(qtbot, tmp_path, monkeypatch):
import win32api
from threading import Event, Thread from threading import Event, Thread
from test_native_backup import project_fixture from test_native_backup import project_fixture
from test_ui_filters import load_window from test_ui_filters import load_window
@@ -220,7 +226,8 @@ def test_common_admission_refusal_publishes_bound_receipt_and_ui_can_reopen(qtbo
from cbb_refresh_verify.application import main from cbb_refresh_verify.application import main
request, _ = project_fixture(tmp_path) request, _ = project_fixture(tmp_path)
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe') monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe')
monkeypatch.setattr(native.subprocess, 'Popen', lambda *a, **k: SimpleNamespace(pid=os.getpid(), poll=lambda: None)) monkeypatch.setattr(native.subprocess, 'Popen', lambda *a, **k:
SimpleNamespace(pid=os.getpid(), handle=win32api.GetCurrentProcess(), poll=lambda: None))
session = native.launch_native_backup(request) session = native.launch_native_backup(request)
entered, release = Event(), Event() entered, release = Event(), Event()
monkeypatch.setattr(desktop_worker, '_automation_clients', []) monkeypatch.setattr(desktop_worker, '_automation_clients', [])
+12 -10
View File
@@ -115,23 +115,22 @@ def test_main_process_exit_hands_off_exact_worker_with_sidecar_and_writer_barrie
child_script=tmp_path/'protected_child.py' child_script=tmp_path/'protected_child.py'
child_script.write_text('''import sys,time,json,os child_script.write_text('''import sys,time,json,os
from pathlib import Path from pathlib import Path
from cbb_refresh_verify.desktop_worker import worker_native_channel from cbb_refresh_verify.desktop_worker import run_native_worker
root=Path(sys.argv[1]) root=Path(sys.argv[1])
while not (root/'gate').exists(): time.sleep(.01) def action(path):
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())) (root/'ready').write_text(str(os.getpid()))
while not (root/'finish').exists(): time.sleep(.02) while not (root/'finish').exists(): time.sleep(.02)
run_native_worker(action,root/'request.json')
(root/'done').touch() (root/'done').touch()
''',encoding='utf-8') ''',encoding='utf-8')
script=tmp_path/'owner_app.py' script=tmp_path/'owner_app.py'
script.write_text('''import sys,time,subprocess,win32api,win32con script.write_text('''import sys,time,subprocess,win32api,win32con,json
from pathlib import Path from pathlib import Path
from threading import Event from threading import Event
from PySide6.QtWidgets import QApplication from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QTimer from PySide6.QtCore import QTimer
from cbb_refresh_verify.ui.main_window import MainWindow from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.operation_queue import native_channel from cbb_refresh_verify.operation_queue import native_channel,commit_worker_start
from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease
from cbb_refresh_verify.session_record import register_worker_session from cbb_refresh_verify.session_record import register_worker_session
root=Path(sys.argv[1]); library=root/'lib.lmc' root=Path(sys.argv[1]); library=root/'lib.lmc'
@@ -139,15 +138,17 @@ app=QApplication([]); MainWindow._show_unfinished_management=lambda self:None
w=MainWindow(reader=lambda _:None,data_root=root/'data'); w.show() w=MainWindow(reader=lambda _:None,data_root=root/'data'); w.show()
def action(): def action():
with LibraryLock(library,checkpoint=False), native_channel(): with LibraryLock(library,checkpoint=False), native_channel():
record=dict(library=str(library),block='A',operation='verify',
operation_id='protected-child',session_record_path=str(root/'session.json'),
cancel_signal=str(root/'cancel'),start_gate=str(root/'gate'),start_gate_protocol='event-v1')
(root/'request.json').write_text(json.dumps(record))
child=subprocess.Popen([sys.executable,'-X','utf8',str(root/'protected_child.py'),str(root)], child=subprocess.Popen([sys.executable,'-X','utf8',str(root/'protected_child.py'),str(root)],
stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
handle=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,child.pid) handle=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,child.pid)
try: lease=duplicate_worker_lease(library,handle) try: lease=duplicate_worker_lease(library,handle)
finally: handle.Close() finally: handle.Close()
register_worker_session(child,dict(library=str(library),block='A',operation='verify', register_worker_session(child,{**record,'worker_lease_handle':lease})
operation_id='protected-child',session_record_path=str(root/'session.json'), commit_worker_start(child,record)
worker_lease_handle=lease,cancel_signal=str(root/'cancel')))
(root/'gate').touch()
Event().wait(90) Event().wait(90)
job=w._new_job('verify',library=library,targets=('A',)) job=w._new_job('verify',library=library,targets=('A',))
w._start(action,lambda _:None,job=job) w._start(action,lambda _:None,job=job)
@@ -170,6 +171,7 @@ app.exec(); print('APP_RETURNED',flush=True)
record=json.loads((tmp_path/'session.json').read_text('utf-8')) record=json.loads((tmp_path/'session.json').read_text('utf-8'))
pid=int((tmp_path/'ready').read_text()) pid=int((tmp_path/'ready').read_text())
assert record['processes'][0]['pid']==pid and record['worker_lease_handle'] assert record['processes'][0]['pid']==pid and record['worker_lease_handle']
assert record['start_gate_protocol']=='event-v1' and record['worker_start_handle']
receipt=json.loads(next((tmp_path/'data'/'shutdown').glob('*.json')).read_text('utf-8')) 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' 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']] bindings=[binding for scope in receipt['task_scopes'] for binding in scope['worker_sessions']]
+205
View File
@@ -0,0 +1,205 @@
"""Real producer/binding/worker admission, using only harmless Python children."""
import json
import os
from pathlib import Path
import subprocess
import sys
from threading import Event
import time
import pytest
from test_issue15_ui_integration import window_for, load
@pytest.fixture
def transport(tmp_path):
from cbb_refresh_verify.desktop_worker import DesktopWorker
script = tmp_path/'harmless_worker.py'
script.write_text('''import json,sys,time
from pathlib import Path
from cbb_refresh_verify.desktop_worker import run_native_worker
from cbb_refresh_verify import native_management as management
path=Path(sys.argv[1]); job=json.loads(path.read_text('utf-8')); root=path.parent
def harmless(path):
(root/'handler-entered').touch()
while (root/'hold-handler').exists() and not (root/'finish').exists(): time.sleep(.01)
(root/'result.json').write_text(json.dumps(dict(success=True,safe_to_continue=True,
library=job['library'],flow=1,entries=[dict(name='A',partition='Logical-Physical',engineering_identity={})])))
return 0
if job['operation']=='catalog':
management._current_desktop=lambda:(None,job['desktop'])
def catalog(job,root):
harmless(path)
return json.loads((root/'result.json').read_text())
management._catalog_native=catalog
from cbb_refresh_verify.application import main
execute=lambda:main(['--management-worker',str(path)])
else: execute=lambda:run_native_worker(harmless,path)
try: execute()
except Exception as error: (root/'rejected').write_text(str(error),encoding='utf-8')
''', encoding='utf-8')
workers = []
spawn = subprocess.Popen
class Transport:
bind_cancellation = DesktopWorker.bind_cancellation
bind_session = DesktopWorker.bind_session
request_stop = DesktopWorker.request_stop
desktop = None
def __init__(self, command, *args, **kwargs):
self.path = Path(command[-1])
self.process = spawn([sys.executable, str(script), str(self.path)])
self.pid, self.handle = self.process.pid, self.process._handle
workers.append(self)
def poll(self): return self.process.poll()
def wait(self, timeout=5): return self.process.wait(timeout)
def close(self): self.process.wait(timeout=5)
yield Transport, workers
for worker in workers:
worker.path.with_name('finish').touch()
if worker.poll() is None:
Path(json.loads(worker.path.read_text('utf-8'))['cancel_signal']).touch()
worker.wait(6)
@pytest.mark.parametrize('entry', ['initial', 'verify', 'icdb'])
def test_real_catalog_adapter_enters_initial_and_execution_reads(qtbot, tmp_path, monkeypatch, transport, entry):
from cbb_refresh_verify.native_management import NativeManagementAdapter
from cbb_refresh_verify.operation_queue import BatchTaskContext
Transport, workers = transport
adapter = NativeManagementAdapter(worker_factory=Transport)
window = window_for(qtbot, tmp_path/'ui', monkeypatch,
catalog_reader=adapter.catalog, status_refresher=lambda names, _: {name:'PENDING' for name in names})
library = tmp_path/'library.lmc'; library.write_bytes(b'isolated')
if entry == 'initial':
load(window, qtbot, str(library))
qtbot.waitUntil(lambda: not any(j.kind=='catalog' and j.active for j in window._jobs.values()), timeout=6000)
assert window._records_by_name['A'].catalog_source == 'both', window.status_label.text()
else:
context = BatchTaskContext(str(library), native_policy='external')
reader = window._execution_status_reader(entry, None)
assert context.run(lambda: reader(('A', 'Missing'), library)) == {'A':'PENDING','Missing':'NOT_REGISTERED'}
assert len(workers) == 1
record = json.loads(workers[0].path.with_name('session.json').read_text('utf-8'))
assert record['worker_protection'] == 'catalog_readonly'
assert record['worker_lease_handle'] is None
@pytest.mark.parametrize('pause', ['after_bind', 'final_preparation'])
def test_shutdown_wins_after_real_binding_and_returns_ui_waiter(qtbot, tmp_path, monkeypatch, transport, pause):
from cbb_refresh_verify.native_management import NativeManagementAdapter
from cbb_refresh_verify.library_lock import LibraryLock
Transport, workers = transport
entered, resume = Event(), Event()
original_bind, original_touch = Transport.bind_session, Path.touch
if pause == 'after_bind':
def bind(self, job):
original_bind(self, job); entered.set(); assert resume.wait(6)
Transport.bind_session = bind
else:
def touch(path, *args, **kwargs):
if path.name == 'start-management.signal':
entered.set(); assert resume.wait(6)
return original_touch(path, *args, **kwargs)
monkeypatch.setattr(Path, 'touch', touch)
library = tmp_path/'lib.lmc'; library.write_bytes(b'isolated')
adapter = NativeManagementAdapter(worker_factory=Transport)
window = window_for(qtbot, tmp_path/'ui', monkeypatch)
job = window._new_job('verify', library=library)
def action():
with LibraryLock(library, checkpoint=False):
return adapter._worker(dict(operation='remove',library=str(library)),tmp_path/'operation')
window._start(action, lambda _:None, job=job)
qtbot.waitUntil(entered.is_set, timeout=4000)
try:
state = job.context.shutdown()
assert state['shutdown_handoff']
qtbot.waitUntil(lambda: not window.busy, timeout=1000)
resume.set()
qtbot.waitUntil(lambda: not job.context.snapshot()['pending'], timeout=4000)
qtbot.waitUntil(lambda: workers[0].poll() is not None, timeout=4000)
assert not (tmp_path/'operation'/'handler-entered').exists()
finally:
resume.set()
@pytest.mark.parametrize('route', ['verify', 'backup', 'repair'])
@pytest.mark.parametrize('pause', ['after_bind', 'final_preparation'])
def test_other_real_producers_share_final_admission_fence(qtbot,tmp_path,monkeypatch,transport,route,pause):
from cbb_refresh_verify import desktop_worker, native_backup, native_repair, session_record
from cbb_refresh_verify.library_lock import LibraryLock
from cbb_refresh_verify.verify_runner import run_verify
from test_verify_runner import fixture, request, PORTS_OK
from test_native_repair import target_fixture
Transport, workers = transport
entered, resume = Event(), Event()
original_bind, original_register, original_touch = Transport.bind_session, session_record.register_worker_session, Path.touch
if pause == 'after_bind':
def bind(self, job):
original_bind(self, job); entered.set(); assert resume.wait(8)
Transport.bind_session = bind
if route == 'backup':
def register(worker,job):
original_register(worker,job); entered.set(); assert resume.wait(8)
monkeypatch.setattr(session_record,'register_worker_session',register)
else:
def touch(path,*args,**kwargs):
if path.name in ('start-worker.signal','start-repair.signal'):
entered.set(); assert resume.wait(8)
return original_touch(path,*args,**kwargs)
monkeypatch.setattr(Path,'touch',touch)
monkeypatch.setattr(desktop_worker,'DesktopWorker',Transport)
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners',lambda *_:[])
if route == 'repair':
req,_,target = target_fixture(tmp_path)
def action():
with LibraryLock(req.lmc_file,checkpoint=False):
return native_repair.run_native_repair(target,tmp_path/'repair',maintenance_context={'library':str(req.lmc_file)})
elif route == 'backup':
from test_native_backup import project_fixture
req,_ = project_fixture(tmp_path)
monkeypatch.setattr(native_backup,'native_backup_executable',lambda *_:tmp_path/'unused.exe')
monkeypatch.setattr(native_backup.subprocess,'Popen',Transport)
action=lambda:native_backup.launch_native_backup(req)
else:
fixture(tmp_path); req=request(tmp_path)
action=lambda:run_verify(req,port_check=PORTS_OK)
window=window_for(qtbot,tmp_path/'ui',monkeypatch)
job=window._new_job('verify',library=req.lmc_file)
window._start(action,lambda _:None,job=job)
try:
qtbot.waitUntil(entered.is_set,timeout=6000)
assert job.context.shutdown()['shutdown_handoff']
qtbot.waitUntil(lambda:not window.busy,timeout=1500)
resume.set()
qtbot.waitUntil(lambda:not job.context.snapshot()['pending'],timeout=6000)
qtbot.waitUntil(lambda:workers[0].poll() is not None,timeout=4000)
assert not workers[0].path.with_name('handler-entered').exists()
finally:
resume.set()
def test_catalog_read_role_requires_the_actual_readonly_worker_action(tmp_path):
from cbb_refresh_verify.desktop_worker import run_native_worker
from cbb_refresh_verify.native_management import catalog_worker_main,management_worker_main,_catalog_com,catalog_dialog_action
from cbb_refresh_verify.session_record import register_worker_session
from types import SimpleNamespace
path=tmp_path/'request.json'
job=dict(operation='catalog',worker_role='catalog_readonly',operation_id='read',
library=str(tmp_path/'lib.lmc'),session_record_path=str(tmp_path/'session.json'))
path.write_text(json.dumps(job))
register_worker_session(SimpleNamespace(pid=os.getpid()),job)
effects=[]
def impostor(path): effects.append('write')
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
run_native_worker(impostor,path)
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
management_worker_main(path)
job['operation']='add';path.write_text(json.dumps(job))
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
catalog_worker_main(path)
with pytest.raises(RuntimeError,match='WORKER_READONLY_ROLE_MISMATCH'):
_catalog_com(job,tmp_path)
assert effects==[]
assert catalog_dialog_action('Library Manager','Would you like to save?',read_only=True)=='cancel'
assert catalog_dialog_action('Upgrade library','Continue?',read_only=True)=='cancel'
+7 -3
View File
@@ -22,6 +22,7 @@ def project_fixture(tmp_path):
def test_backup_entry_can_open_project_without_layout_data(tmp_path, monkeypatch): def test_backup_entry_can_open_project_without_layout_data(tmp_path, monkeypatch):
import subprocess, sys
from cbb_refresh_verify import native_backup as native from cbb_refresh_verify import native_backup as native
request, project = project_fixture(tmp_path) request, project = project_fixture(tmp_path)
executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch() executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch()
@@ -30,9 +31,12 @@ def test_backup_entry_can_open_project_without_layout_data(tmp_path, monkeypatch
monkeypatch.setattr(session_record, 'process_identity', lambda pid: monkeypatch.setattr(session_record, 'process_identity', lambda pid:
dict(pid=pid, created='fixture', executable='python.exe', owner='fixture')) dict(pid=pid, created='fixture', executable='python.exe', owner='fixture'))
calls = [] calls = []
monkeypatch.setattr(native.subprocess, 'Popen', lambda command, **kw: child = subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
(calls.append(command) or SimpleNamespace(pid=123, poll=lambda: None))) monkeypatch.setattr(native.subprocess, 'Popen', lambda command, **kw: (calls.append(command) or child))
session = native.launch_native_backup(request) try:
session = native.launch_native_backup(request)
finally:
child.communicate('\n',timeout=5)
record = json.loads(session.job.read_text('utf-8')) record = json.loads(session.job.read_text('utf-8'))
assert record['project'] == str(project) assert record['project'] == str(project)
assert calls[0][-2] == '--native-backup-worker' assert calls[0][-2] == '--native-backup-worker'
+3 -2
View File
@@ -355,7 +355,8 @@ def test_probe_requires_owned_new_native_library_path(tmp_path):
@pytest.mark.parametrize('records', [[], [('Actual', 'Logical-Physical')]]) @pytest.mark.parametrize('records', [[], [('Actual', 'Logical-Physical')]])
@pytest.mark.parametrize('mapped', [False, True]) @pytest.mark.parametrize('mapped', [False, True])
@pytest.mark.parametrize('flow', [1, 2]) @pytest.mark.parametrize('flow', [1, 2])
def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records, mapped, flow): @pytest.mark.parametrize('role', [None, 'catalog_readonly'])
def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records, mapped, flow, role):
import cbb_refresh_verify.native_management as native import cbb_refresh_verify.native_management as native
import cbb_refresh_verify.mentor_adapter as mentor import cbb_refresh_verify.mentor_adapter as mentor
import win32com.client import win32com.client
@@ -388,7 +389,7 @@ def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path,
monkeypatch.setattr(native, '_current_desktop', lambda: (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test')) monkeypatch.setattr(native, '_current_desktop', lambda: (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test'))
monkeypatch.setattr(mentor, '_process_details', lambda _: [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if state['running'] else []) monkeypatch.setattr(mentor, '_process_details', lambda _: [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if state['running'] else [])
monkeypatch.setattr(native, '_wait_native_exit', lambda _: not state['running']) monkeypatch.setattr(native, '_wait_native_exit', lambda _: not state['running'])
result = native._catalog_native({'operation': 'catalog', 'library': str(library_path), result = native._catalog_native({'operation': 'catalog', 'worker_role': role, 'library': str(library_path),
'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path), 'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path),
'supervisor_pid': __import__('os').getpid()}, tmp_path) 'supervisor_pid': __import__('os').getpid()}, tmp_path)
assert result['success'] and result['safe_to_continue'] assert result['success'] and result['safe_to_continue']