fix: cancel superseded release probes without stopping the batch

This commit is contained in:
阿白叔 committed 2026-11-25 16:15:26 +08:00
1 parent b9d52e84cd
commit 0c02ff6966
2 files changed
+52 -10

No files matched your search

+30 -10
View File
@@ -404,14 +404,17 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
while True:
observation_only = read_only()
try:
result = _reconcile(Path(path), Path(evidence_root), handles, stack, release=not observation_only)
result = _reconcile(Path(path), Path(evidence_root), handles, stack,
release=not observation_only and not stop.is_set(), stop=stop)
except Exception as error:
result = _result('UNCONFIRMED', str(error))
if (not observation_only and process is not None and process.poll() is not None
if (not observation_only and not stop.is_set()
and process is not None and process.poll() is not None
and getattr(process, 'cbb_library', None)):
try:
from .library_lock import release_retained_library
proof = release_retained_library(process.cbb_library, Path(path).parent/'release-proof')
proof = release_retained_library(process.cbb_library, Path(path).parent/'release-proof',
stop=stop)
if proof.get('status') == 'RELEASE_CONFIRMED':
result = _result('RELEASED', '会话记录不可读;由原进程句柄及独立资源证明确认释放',
session={'library': str(process.cbb_library)}, proof=proof)
@@ -423,13 +426,15 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
if resume_cancelled_startup(path, stop=stop) or finish_owned_idle_session(path, stop=stop):
# Cleanup is not release proof. Re-read all held identities
# and independently prove the resource release as usual.
result = _reconcile(Path(path), Path(evidence_root), handles, stack)
result = _reconcile(Path(path), Path(evidence_root), handles, stack,
release=not stop.is_set(), stop=stop)
except Exception:
logging.getLogger(__name__).warning('Retained startup cleanup unavailable', exc_info=True)
if result['status'] == 'IDENTITY_CHANGED' and not observation_only:
if result['status'] == 'IDENTITY_CHANGED' and not observation_only and not stop.is_set():
try:
from .library_lock import release_retained_library
proof = release_retained_library(result['library'], Path(path).parent/'release-proof')
proof = release_retained_library(result['library'], Path(path).parent/'release-proof',
stop=stop)
if proof.get('status') == 'RELEASE_CONFIRMED':
result = {**result, 'status': 'RELEASED', 'identity_changed': True, 'release_proof': proof}
except Exception as error:
@@ -460,8 +465,23 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
def handoff_session(path, *, evidence_root, process=None, progress=None, stop=None, observe_only=False):
"""Retain exact handles in memory until the observer independently releases."""
if stop is None:
stop = threading.Event()
retired = threading.Event()
class ObservationStop:
def is_set(self):
return retired.is_set() or (stop is not None and stop.is_set())
def wait(self, seconds):
if stop is None:
return retired.wait(seconds)
import time
deadline = time.monotonic() + seconds
while not self.is_set():
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
retired.wait(min(.1, remaining))
return True
key = str(Path(path).absolute()).casefold()
try:
session = read_session_record(path, evidence_root=evidence_root)
@@ -504,7 +524,7 @@ def handoff_session(path, *, evidence_root, process=None, progress=None, stop=No
result = None
try:
result = observe_retained_session(path, evidence_root=evidence_root, progress=notify,
stop=stop, process=process, observe_only=observation_only)
stop=ObservationStop(), process=process, observe_only=observation_only)
if process is not None and process.poll() is not None:
process.close()
return result
@@ -518,7 +538,7 @@ def handoff_session(path, *, evidence_root, process=None, progress=None, stop=No
_retained_libraries.pop(key, None)
_retained_results.pop(key, None)
thread = threading.Thread(target=observe, name='retained-native-session', daemon=True)
_observers[key] = (thread, process, stop)
_observers[key] = (thread, process, retired)
thread.start()