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

+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):
import subprocess, sys
from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native, library_lock, session_record
request, project = project_fixture(tmp_path)
@@ -90,13 +91,17 @@ def test_versions_launcher_rechecks_parent_retained_lease_and_persists_before_ga
calls = []
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'))
child = subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
def spawn(command, **kw):
job = json.loads(Path(command[-1]).read_text())
assert calls == ['recheck'] and not Path(job['start_gate']).exists()
calls.append('spawn')
return SimpleNamespace(pid=123, poll=lambda: None)
return child
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())
assert Path(record['start_gate']).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):
import win32api
from threading import Event, Thread
from test_native_backup import project_fixture
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
request, _ = project_fixture(tmp_path)
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)
entered, release = Event(), Event()
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.write_text('''import sys,time,json,os
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])
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']):
def action(path):
(root/'ready').write_text(str(os.getpid()))
while not (root/'finish').exists(): time.sleep(.02)
run_native_worker(action,root/'request.json')
(root/'done').touch()
''',encoding='utf-8')
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 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.operation_queue import native_channel,commit_worker_start
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'
@@ -139,15 +138,17 @@ 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():
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)],
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()
register_worker_session(child,{**record,'worker_lease_handle':lease})
commit_worker_start(child,record)
Event().wait(90)
job=w._new_job('verify',library=library,targets=('A',))
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'))
pid=int((tmp_path/'ready').read_text())
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'))
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']]
+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):
import subprocess, sys
from cbb_refresh_verify import native_backup as native
request, project = project_fixture(tmp_path)
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:
dict(pid=pid, created='fixture', executable='python.exe', owner='fixture'))
calls = []
monkeypatch.setattr(native.subprocess, 'Popen', lambda command, **kw:
(calls.append(command) or SimpleNamespace(pid=123, poll=lambda: None)))
session = native.launch_native_backup(request)
child = subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
monkeypatch.setattr(native.subprocess, 'Popen', lambda command, **kw: (calls.append(command) or child))
try:
session = native.launch_native_backup(request)
finally:
child.communicate('\n',timeout=5)
record = json.loads(session.job.read_text('utf-8'))
assert record['project'] == str(project)
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('mapped', [False, True])
@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.mentor_adapter as mentor
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(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'])
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),
'supervisor_pid': __import__('os').getpid()}, tmp_path)
assert result['success'] and result['safe_to_continue']