fix: prove prewrite cancellation and bound recovery metadata lifetime

This commit is contained in:
阿白叔 committed 2026-11-17 12:45:59 +08:00
1 parent 32dcd80d1e
commit 906cfc06be
14 files changed
+659 -60

No files matched your search

+24 -4
View File
@@ -68,6 +68,7 @@ class _Call:
done: Event = field(default_factory=Event)
abandoned: bool = False
native_depth: int = 0
commit_depth: int = 0
readonly_depth: int = 0
supervised_depth: int = 0
worker_sessions: list = field(default_factory=list)
@@ -175,6 +176,25 @@ def hold_native_handoff(process, session_record_path):
raise TaskHandoff(call.context, cancelled=call.handoff_cancelled, result=call.last_result)
@contextmanager
def local_commit():
"""Finish an admitted filesystem rename/rollback without native ownership claims."""
call = getattr(_local, 'call', None)
with _tasks_guard:
check_active()
if call is not None:
if call.metadata_only or call.readonly_depth:
raise RuntimeError('Read-only metadata cannot commit filesystem changes')
call.commit_depth += 1
try:
yield
finally:
if call is not None:
with _tasks_guard:
call.commit_depth -= 1
call.phase_started = time.monotonic()
@contextmanager
def native_phase(*, read_only=False, supervised=False):
"""Never abandon a transaction between native writes (including Remove/Add)."""
@@ -266,7 +286,7 @@ class BatchTaskContext:
'CANCELLED' if self.stop.is_set() else
'HANDED_OFF' if any(c.abandoned for c in calls) else
'RUNNING' if pending else 'IDLE', pending=pending,
pending_native=any(c.native_depth for c in calls))
pending_native=any(c.native_depth or c.commit_depth for c in calls))
def shutdown(self):
"""Yield UI supervision, never a native worker's write/lease ownership.
@@ -285,7 +305,7 @@ class BatchTaskContext:
call.supervised_depth == call.native_depth and
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:
if not call.commit_depth and (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),
@@ -319,7 +339,7 @@ class BatchTaskContext:
call.done.wait(max(0, deadline-time.monotonic()))
with _tasks_guard:
for call in calls:
if not call.done.is_set() and not call.native_depth:
if not call.done.is_set() and not call.native_depth and not call.commit_depth:
call.abandoned = True
return self.snapshot()
@@ -357,7 +377,7 @@ class BatchTaskContext:
call.abandoned = True
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)
elapsed = time.monotonic()-call.phase_started
if not call.native_depth and (call.abandoned or elapsed >= timeout_seconds):
if not call.native_depth and not call.commit_depth and (call.abandoned or elapsed >= timeout_seconds):
cancelled = call.is_set()
call.abandoned = True
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)