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

+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']]