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

+34 -8
View File
@@ -27,7 +27,8 @@ class _StopSignal:
self._event = Event()
def set(self):
self._event.set()
with _tasks_guard:
self._event.set()
def is_set(self):
return self._event.is_set()
@@ -120,10 +121,35 @@ 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'):
and not (job.get('operation') == 'catalog' and job.get('worker_role') == 'catalog_readonly')):
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):
"""Yield parent admission to controls; the gated worker retains write exclusion.
@@ -233,19 +259,19 @@ class BatchTaskContext:
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.
External workers commit their kernel start event under this same fence.
Registration and all filesystem preparation remain outside the fence.
Legacy in-process transactions cannot opt in.
"""
self._closed.set()
self.cancel()
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()]
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')
all(s.get('worker_protection') in ('duplicated_lease', 'worker_owned_lease', 'inspection_only', 'catalog_readonly')
for s in call.worker_sessions))
if not call.native_depth or readonly or external:
call.shutdown_handoff = call.abandoned = True