fix: hand off UI shutdown without releasing native ownership
This commit is contained in:
1 parent
cd7a5e0ca1
commit
dbc685b9a5
10 files changed
+454
-52
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user