fix: cancel superseded release probes without stopping the batch
This commit is contained in:
1 parent
b9d52e84cd
commit
0c02ff6966
2 files changed
+52
-10
No files matched your search
@@ -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()
|
||||
|
||||
|
||||
|
||||
@@ -145,6 +145,7 @@ def test_natural_native_exit_gets_fresh_release_and_admits_waiting_scope_without
|
||||
assert future.result(8)=='fresh admission'
|
||||
thread.join(3)
|
||||
assert not thread.is_alive() and closed==[0]
|
||||
assert not stop.is_set(), 'retiring the observer must not cancel its caller'
|
||||
observations=[json.loads(p.read_text()) for p in evidence.glob('observation-*.json')]
|
||||
assert any(o.get('status')=='RELEASED' and o['release_proof']['proof_block_name']=='A' for o in observations)
|
||||
assert original.read_bytes()==before
|
||||
@@ -152,3 +153,24 @@ def test_natural_native_exit_gets_fresh_release_and_admits_waiting_scope_without
|
||||
gate.touch();native.wait(5);stop.set()
|
||||
if thread:thread.join(3)
|
||||
cleanup(lease)
|
||||
|
||||
|
||||
def test_stopped_observer_does_not_start_a_second_library_probe(tmp_path, monkeypatch):
|
||||
from test_session_record import record
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
with subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)']) as native:
|
||||
identity = session_record.process_identity(native.pid)
|
||||
native.terminate()
|
||||
native.wait(5)
|
||||
path = record(tmp_path, [dict(role='native', **identity)])
|
||||
stop = Event()
|
||||
stop.set()
|
||||
probes = []
|
||||
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library',
|
||||
lambda *args, **kwargs: probes.append(args))
|
||||
|
||||
session_record.observe_retained_session(path, evidence_root=tmp_path, stop=stop)
|
||||
|
||||
assert probes == []
|
||||
Reference in new issue
Block a user