fix: preserve async writer ownership and bounded handoff

This commit is contained in:
阿白叔 committed 2026-11-16 19:51:21 +08:00
1 parent ddff6bd857
commit 8374cabe5c
12 files changed
+692 -47

No files matched your search

+8 -8
View File
@@ -67,6 +67,7 @@ class _Call:
abandoned: bool = False
native_depth: int = 0
native_handoff: str = ''
handoff_cancelled: bool = False
phase_started: float = field(default_factory=time.monotonic)
result: object = None
last_result: object = None
@@ -101,19 +102,18 @@ def remember_result(result):
def hold_native_handoff(process, session_record_path):
"""Keep this thread's mutex while a recorded native client is unreturned.
"""Yield parent admission to controls; the gated worker retains write exclusion.
The caller registers the observer first. Only UI/scheduling is handed off;
this thread and its native channel remain alive until that exact handle exits.
The caller registers the exact worker observer first. Its writer barrier and
inherited sidecar survive this supervisor, including while a user answers Save.
"""
call = getattr(_local, 'call', None)
if call is not None:
with _tasks_guard:
if not call.native_depth:
raise RuntimeError('Native handoff requires an owned native phase')
call.handoff_cancelled = call.is_set()
call.native_handoff = str(session_record_path)
while process.poll() is None:
Event().wait(.1)
call.abandoned = True
raise TaskHandoff(call.context, cancelled=call.handoff_cancelled, result=call.last_result)
@contextmanager
@@ -252,7 +252,7 @@ class BatchTaskContext:
while not call.done.wait(.02):
with _tasks_guard:
if call.native_handoff:
cancelled = call.is_set()
cancelled = call.handoff_cancelled
call.abandoned = True
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)
elapsed = time.monotonic()-call.phase_started