fix: reconcile cancelled sessions and resume queued native work
This commit is contained in:
1 parent
845ae58690
commit
7cc65f78da
10 files changed
+637
-18
No files matched your search
@@ -230,7 +230,7 @@ def _run_process(command, timeout_seconds, cwd, *, evidence_root=None):
|
||||
return run_content_check(command, timeout_seconds, cwd, evidence_root=evidence_root)
|
||||
|
||||
|
||||
def _stop_worker(process, *, timeout=5):
|
||||
def _stop_worker(process, *, timeout=5, wait_for_native_write=True):
|
||||
"""Request stop without interrupting a possibly unreturned native call."""
|
||||
try:
|
||||
if hasattr(process, 'request_stop'):
|
||||
@@ -243,7 +243,7 @@ def _stop_worker(process, *, timeout=5):
|
||||
process.wait(timeout=timeout)
|
||||
except Exception:
|
||||
pending = getattr(process, 'native_write_pending', lambda: False)()
|
||||
if pending is not False and not getattr(process, '_stop_wait_complete', False):
|
||||
if wait_for_native_write and pending is not False and not getattr(process, '_stop_wait_complete', False):
|
||||
from .defaults import NATIVE_WRITE_STOP_WAIT_SECONDS
|
||||
try:
|
||||
process.wait(timeout=max(0, NATIVE_WRITE_STOP_WAIT_SECONDS - timeout))
|
||||
@@ -1070,7 +1070,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
'已请求停止,等待当前原生操作返回')
|
||||
_notify_progress(progress, {'stage': 'save_stop_requested', 'block': request.block_name,
|
||||
'detail': detail})
|
||||
_stop_worker(process)
|
||||
_stop_worker(process, wait_for_native_write=not request.diagnostic_only)
|
||||
return finish(VerifyResult('CANCELLED', stderr='已取消当前校验。',
|
||||
safe_to_continue=False, error_stage=phase.value))
|
||||
if events.exists():
|
||||
@@ -1237,7 +1237,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
target_pid=target_pid,
|
||||
observation=last_post_verify_observation,
|
||||
)
|
||||
_stop_worker(process)
|
||||
_stop_worker(process, wait_for_native_write=not request.diagnostic_only)
|
||||
if dispatch_failure is not None:
|
||||
status, detail = dispatch_failure
|
||||
if progress:
|
||||
@@ -1260,7 +1260,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
result = VerifyResult(**outcome)
|
||||
except Exception as error:
|
||||
if 'process' in locals() and process.poll() is None:
|
||||
_stop_worker(process)
|
||||
_stop_worker(process, wait_for_native_write=not request.diagnostic_only)
|
||||
launch_release = getattr(error, 'worker_launch_release', None) if 'process' not in locals() else None
|
||||
if not worker_launch_attempted:
|
||||
launch_release = {'process_created': False, 'worker_exited': True,
|
||||
@@ -1279,6 +1279,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
finally:
|
||||
close_errors = []
|
||||
worker_exited = False
|
||||
observer_started = False
|
||||
automation_release = {'safe_to_continue': False}
|
||||
if 'process' not in locals() or process.poll() is not None or getattr(process, 'session_record_path', None):
|
||||
# A gated worker owns its writer barrier and inherited sidecar.
|
||||
@@ -1293,18 +1294,17 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
try:
|
||||
worker_exited = process.poll() is not None
|
||||
if not worker_exited:
|
||||
worker_exited = _stop_worker(process)
|
||||
worker_exited = _stop_worker(process, wait_for_native_write=not request.diagnostic_only)
|
||||
if hasattr(process, 'automation_release_state'):
|
||||
automation_release = process.automation_release_state()
|
||||
else:
|
||||
automation_release = {'safe_to_continue': worker_exited, 'remaining_pids': []}
|
||||
if worker_exited:
|
||||
process.close()
|
||||
elif getattr(process, 'session_record_path', None):
|
||||
if not worker_exited and getattr(process, 'session_record_path', None):
|
||||
from .session_record import handoff_session
|
||||
handoff_session(process.session_record_path, evidence_root=root,
|
||||
process=process, progress=lambda state: _notify_progress(progress,
|
||||
{'stage': 'session_observation', 'block': request.block_name, **state}))
|
||||
observer_started = True
|
||||
_notify_progress(progress, {'stage': 'session_handoff', 'block': request.block_name,
|
||||
'detail': '已转后台观察,可查看状态'})
|
||||
from .operation_queue import hold_native_handoff
|
||||
@@ -1353,6 +1353,25 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
'automation_release': automation_release, 'worker_exited': worker_exited}
|
||||
release_result['remaining_pids'] = sorted(set(release_result.get('remaining_pids', []))
|
||||
| set(automation_release.get('remaining_pids', [])))
|
||||
if worker_exited:
|
||||
try:
|
||||
if (release_result.get('safe_to_continue') is not True
|
||||
and getattr(process, 'session_record_path', None)):
|
||||
if not observer_started:
|
||||
from .session_record import handoff_session
|
||||
# Retain the exact worker handle until registered
|
||||
# native clients exit and the observer obtains proof.
|
||||
handoff_session(process.session_record_path, evidence_root=root,
|
||||
process=process, progress=lambda state: _notify_progress(progress,
|
||||
{'stage': 'session_observation', 'block': request.block_name, **state}))
|
||||
observer_started = True
|
||||
_notify_progress(progress, {'stage': 'session_handoff', 'block': request.block_name,
|
||||
'detail': '工作进程已退出,原生会话继续后台观察'})
|
||||
else:
|
||||
process.close()
|
||||
except Exception as error:
|
||||
release_result = {**release_result, 'safe_to_continue': False,
|
||||
'close_errors': [*close_errors, str(error)]}
|
||||
try:
|
||||
from .operation_lifecycle import classify_save_state
|
||||
save_lines = ((root/'save.log').read_text('utf-8', errors='replace').splitlines()
|
||||
|
||||
Reference in new issue
Block a user