fix: reconcile cancelled sessions and resume queued native work

This commit is contained in:
阿白叔 committed 2026-11-17 08:37:23 +08:00
1 parent 845ae58690
commit 7cc65f78da
10 files changed
+637 -18

No files matched your search

+11 -4
View File
@@ -365,14 +365,16 @@ def _pending_prompt(windows, *, closing=False, frame=None, session=None, clicked
return False return False
def cancelled_startup_step(job, pcb_pid, state, *, closed_frame, deadline, post, emit): def cancelled_startup_step(job, pcb_pid, state, *, closed_frame, deadline, post, emit,
read_windows=None, identity_check=None):
"""Finish only the owned no-write diagnostic startup; never establish PASS. """Finish only the owned no-write diagnostic startup; never establish PASS.
Cancellation already ended supervisor polling. Cleanup is its sole prompt Cancellation already ended supervisor polling. Cleanup is its sole prompt
actor and uses the caller's original deadline and single-close state. actor and uses the caller's original deadline and single-close state.
""" """
from types import SimpleNamespace from types import SimpleNamespace
windows = _windows(pcb_pid) read_windows = read_windows or (lambda: _windows(pcb_pid))
windows = read_windows()
if any(window['pid'] != pcb_pid for window in windows): if any(window['pid'] != pcb_pid for window in windows):
raise RuntimeError('STARTUP_PROCESS_UNCONFIRMED') raise RuntimeError('STARTUP_PROCESS_UNCONFIRMED')
current = _target_frame(windows, Path(job['pcb']).stem, require_ready=False) current = _target_frame(windows, Path(job['pcb']).stem, require_ready=False)
@@ -387,7 +389,11 @@ def cancelled_startup_step(job, pcb_pid, state, *, closed_frame, deadline, post,
if closed_frame is not None and frame['hwnd'] != closed_frame: if closed_frame is not None and frame['hwnd'] != closed_frame:
raise RuntimeError('STARTUP_DOCUMENT_CHANGED') raise RuntimeError('STARTUP_DOCUMENT_CHANGED')
if old is None: if old is None:
if _load_evidence(job, pcb_pid, job['diagnostic_log_baseline'], require_autosave=False) is None: # Explicit legacy cleanup supplies already checked registration. It is
# revalidated immediately before every action, never used as open proof.
confirmed = (identity_check is not None or
_load_evidence(job, pcb_pid, job['diagnostic_log_baseline'], require_autosave=False) is not None)
if not confirmed:
return closed_frame, False return closed_frame, False
state['frame'] = frame state['frame'] = frame
# Validate every window before allowing a known No. An unknown sibling # Validate every window before allowing a known No. An unknown sibling
@@ -402,7 +408,8 @@ def cancelled_startup_step(job, pcb_pid, state, *, closed_frame, deadline, post,
raise raise
actions = [] actions = []
def act(hwnd, message): def act(hwnd, message):
if _windows(pcb_pid) != windows or time.monotonic() >= deadline: if (read_windows() != windows or identity_check is not None and not identity_check()
or time.monotonic() >= deadline):
raise RuntimeError('STARTUP_ACTION_UNCONFIRMED: native state or deadline changed') raise RuntimeError('STARTUP_ACTION_UNCONFIRMED: native state or deadline changed')
post(hwnd, message) post(hwnd, message)
actions.append((hwnd, message)) actions.append((hwnd, message))
+9 -2
View File
@@ -390,6 +390,13 @@ class NativeQueue:
def run(self, action, *, stop=None, waiting=None, ready=lambda: True, wait_state=None, def run(self, action, *, stop=None, waiting=None, ready=lambda: True, wait_state=None,
context=None, timeout_seconds=10): context=None, timeout_seconds=10):
def available_now():
if not ready():
return False
if context is None:
return True # Explicit recovery controls must still reach their retained clients.
from .session_record import _live_library_available
return all(_live_library_available(path) for path in (context.library, *context.aliases))
ticket = [object(), False] ticket = [object(), False]
acquired, notified = False, False acquired, notified = False, False
previous_state = None previous_state = None
@@ -401,7 +408,7 @@ class NativeQueue:
self.pending.append(ticket) self.pending.append(ticket)
try: try:
while not acquired: while not acquired:
available = ready() # Call only this ticket's predicate, outside the queue lock. available = available_now() # Cached predicates only, outside the queue lock.
with self.condition: with self.condition:
if (self.closed or stop is not None and stop.is_set() if (self.closed or stop is not None and stop.is_set()
or context is not None and (context.stop.is_set() or context.stop_after_current.is_set())): or context is not None and (context.stop.is_set() or context.stop_after_current.is_set())):
@@ -412,7 +419,7 @@ class NativeQueue:
self.active = acquired = True self.active = acquired = True
else: else:
self.condition.wait(.1) self.condition.wait(.1)
if acquired and not ready(): if acquired and not available_now():
with self.condition: with self.condition:
ticket[1] = self.active = acquired = False ticket[1] = self.active = acquired = False
self.condition.notify_all() self.condition.notify_all()
+160
View File
@@ -570,6 +570,161 @@ def _dismiss_startup_tip(row, handle, windows, *, incomplete=None, dismissed=(),
return tip['hwnd'] return tip['hwnd']
def _cancelled_diagnostic_record(row):
"""Existing complete registration can authorize cleanup, never validation."""
import stat
import pywintypes, win32con, win32event
from .session_record import _session, IDENTITY_KEYS, active_write_records
from .failed_session import _command_args
if row.get('scope_confirmed') is not True or row.get('name', '').casefold() != 'expeditionpcb.exe':
return None
def same(a, b):
return os.path.normcase(os.path.abspath(str(a))) == os.path.normcase(os.path.abspath(str(b)))
def read(path, *, lines=False):
if path.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
raise ValueError('STARTUP_RECORD_REPARSE_POINT')
with path.open('rb') as stream:
data = stream.read(1024*1024+1)
if len(data) > 1024*1024:
raise ValueError('STARTUP_RECORD_TOO_LARGE')
return [json.loads(line) for line in data.splitlines()] if lines else json.loads(data)
matches = []
for record in row.get('session_records', ()):
try:
path = Path(record['path']); root = path.parent
session, state, identities = _session(path, root)
job, result = read(root/'request.json'), read(root/'result.json')
operation = session['operation_id']
scope = session.get('resource_scope', {})
if (record.get('operation_id') != operation or job.get('operation_id') != operation
or job.get('diagnostic_only') is not True or job.get('interactive_input') is not False
or job.get('operation') != 'verify' or session.get('operation') != 'verify'
or not all(same(value, row['library']) for value in (job['library'], job['lmc_file'], session['library']))
or not same(job['root'], root) or not same(job['session_record_path'], path)
or job.get('desktop') != session.get('desktop') or not job['desktop'].startswith('CbbVerify_')
or scope.get('scope_state') != 'DECLARED' or scope.get('operation_id') != operation
or not same(scope.get('library', ''), row['library'])
or scope.get('projects') != [job['project']]
or not Path(job['pcb']).is_relative_to(Path(job['project']).parent)
or not same(job['cancel_signal'], session['cancel_signal'])
or Path(job['cancel_signal']).parent != root or not Path(job['cancel_signal']).is_file()
or result.get('status') != 'CANCELLED' or result.get('error_stage') != 'PCB_STARTING'
or result.get('write_outcome', {}).get('save_state') != 'UNKNOWN'
or state.get('phase') != 'UNCONFIRMED' or (root/'save.log').exists()):
continue
native = [p for p in identities if p.get('role') == 'native' and p['pid'] == row['pid']]
workers = [p for p in identities if p.get('role') in ('worker', 'helper')]
if (len(native) != 1 or not workers
or any(native[0][k] != row.get(k) for k in IDENTITY_KEYS)
or any(p.get('role') not in ('worker','helper','native','supervisor') for p in identities)):
continue
for worker in workers:
try:
with _process_handle(worker['pid']) as held:
if win32event.WaitForSingleObject(held, 0) != win32con.WAIT_OBJECT_0:
raise ValueError('STARTUP_WORKER_STILL_ACTIVE')
except (OSError, pywintypes.error) as error:
if getattr(error, 'winerror', None) != 87:
raise
args = _command_args(row['command'])
if ('-reusablecircuit' not in [a.casefold() for a in args]
or not any(same(a, job['pcb']) for a in args[1:])):
continue
events = read(root/'events.jsonl', lines=True)
if (not any(e.get('stage') == 'pcb_worker_started' and e.get('pcb_pid') == row['pid'] for e in events)
or not any(e.get('stage') == 'pcb_baseline' and row['pid'] not in e.get('pids', []) for e in events)
or any(str(e.get('stage', '')).startswith(('save', 'integration', 'validate', 'failure_close'))
or e.get('stage') == 'diagnostic_complete' for e in events)):
continue
closes = [e for e in events if e.get('stage') in ('startup_close_requested', 'diagnostic_wm_close')]
if len(closes) > 1 or any(e.get('pcb_pid') != row['pid'] or not e.get('hwnd') for e in closes):
continue
if any(r.get('operation_id') != operation for r in active_write_records(row['library'])):
continue
matches.append(dict(job=job, record_path=str(path), operation_id=operation,
closed_frame=closes[0]['hwnd'] if closes else None))
except (OSError, pywintypes.error, ValueError, KeyError, TypeError):
continue
return matches[0] if len(matches) == 1 else None
def _legacy_diagnostic_windows(row):
"""Adapt the existing complete private-desktop snapshot for diagnostic policy."""
import win32con, win32gui
snapshot = _process_windows({row['pid']})
if _window_error(snapshot, row['pid']):
raise ValueError('STARTUP_WINDOW_UNCONFIRMED')
result = []
for window in snapshot.get(row['pid'], []):
if window.get('desktop') != row['cleanup_desktop']:
raise ValueError('STARTUP_DESKTOP_UNCONFIRMED')
benign = (not window['title'] and window['class'] in ('UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator'))
benign = benign or (window['class'] == 'Internet Explorer_Hidden' and not window['title']
and not window['children'] and window.get('rect', [1,1,2,2])[:2] == window.get('rect', [1,1,2,2])[2:])
if benign:
continue
with _window_desktop(window['desktop']):
children = [dict(c, parent=win32gui.GetParent(c['hwnd']), visible=True) for c in window['children']]
result.append(dict(hwnd=window['hwnd'], pid=row['pid'], **{'class':window['class']},
text=window['title'], enabled=window['enabled'], children=children,
owner=int(win32gui.GetWindow(window['hwnd'], win32con.GW_OWNER) or 0)))
return result
def _close_registered_startup(row, handle, proof, *, stop=None):
import pywintypes, win32con, win32event
from .diagnostic_open import cancelled_startup_step
from .operation_queue import check_active
job = proof['job']; bound = dict(row, cleanup_desktop=job['desktop'])
deadline = time.monotonic()+3
def active_owner():
check_active()
if stop is not None and stop.is_set():
return False
owner = _force_ownership(handle,row['pid'])
return (owner.get('force_user_confirmed') is True
and win32event.WaitForSingleObject(handle,0) == win32con.WAIT_TIMEOUT
and _handle_identity(handle) == {k:row.get(k) for k in ('created_handle','image')}
and all(owner.get(k) == row.get(k) for k in ('owner_sid','session_id')))
def confirmed():
if not active_owner():return False
fresh = next((p for p in query_process_snapshot() if p['pid'] == row['pid']), None)
return (fresh is not None and all(fresh.get(k) == row.get(k) for k in ('created','command','executable','name'))
and _cancelled_diagnostic_record(row) == proof)
def post(hwnd, message):
if not active_owner() or time.monotonic() >= deadline:
raise ValueError('STARTUP_ACTION_UNCONFIRMED')
_post_window_message({'desktop':job['desktop']}, hwnd, row['pid'], message)
state = {'clicked':set()}; closed = proof['closed_frame']
try:
while time.monotonic() < deadline:
if win32event.WaitForSingleObject(handle, 0) == win32con.WAIT_OBJECT_0:
return {'release_status':'RELEASED','detail':'已正常结束已取消的诊断启动'}
if not active_owner():
break
snapshot = _process_windows({row['pid']})
# Only this fully proved no-write operation may bypass its historical
# UNCONFIRMED label; other operations were vetoed by the proof above.
tip_row = {k:v for k,v in row.items() if k not in ('runtime_phase','library')}
if closed is not None:
tip_row['startup_frame'] = closed
has_tip = any(w['title'] == 'Tip of the Day' for w in snapshot.get(row['pid'],[]))
if has_tip and confirmed() and _dismiss_startup_tip(tip_row,handle,snapshot.get(row['pid'],[]),
incomplete=_window_error(snapshot,row['pid']),deadline=deadline,stop=stop) is not None:
while time.monotonic() < deadline:
if win32event.WaitForSingleObject(handle,100) == win32con.WAIT_OBJECT_0:
return {'release_status':'RELEASED','detail':'已关闭启动提示并正常退出'}
if stop is not None and stop.is_set():break
break # Never stack another Close after a legacy Tip acknowledgement.
closed,_ = cancelled_startup_step(job,row['pid'],state,closed_frame=closed,deadline=deadline,
post=post,emit=lambda **event:None,read_windows=lambda:_legacy_diagnostic_windows(bound),
identity_check=confirmed)
time.sleep(.1)
except (OSError, pywintypes.error, ValueError, RuntimeError) as error:
return {'release_status':'WAITING','detail':str(error)}
return {'release_status':'WAITING','detail':'已请求取消启动清理,等待原生退出后重新检查'}
def _request_close(row, *, stop=None): def _request_close(row, *, stop=None):
import win32con, win32event import win32con, win32event
with _process_handle(row['pid']) as handle: with _process_handle(row['pid']) as handle:
@@ -584,6 +739,8 @@ def _request_close(row, *, stop=None):
return {'release_status': 'WAITING', 'detail': '该会话窗口读取不完整,请重新检查:'+error} return {'release_status': 'WAITING', 'detail': '该会话窗口读取不完整,请重新检查:'+error}
if stop is not None and stop.is_set(): if stop is not None and stop.is_set():
return {'release_status': 'CANCELLED', 'detail': '已停止释放'} return {'release_status': 'CANCELLED', 'detail': '已停止释放'}
if proof := _cancelled_diagnostic_record(row):
return _close_registered_startup(row, handle, proof, stop=stop)
if _dismiss_startup_tip(row, handle, windows, stop=stop) is not None: if _dismiss_startup_tip(row, handle, windows, stop=stop) is not None:
# Legacy startup cleanup may already have sent WM_CLOSE. Only wait # Legacy startup cleanup may already have sent WM_CLOSE. Only wait
# for it here; never stack another close behind this acknowledgement. # for it here; never stack another close behind this acknowledgement.
@@ -795,6 +952,9 @@ def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False, ca
if not save(): if not save():
return result return result
try: try:
if not force and not cancel_native:
row['session_records'] = [{k:r.get(k) for k in ('path','operation_id')}
for r in plan.get('runtime_sessions', []) if r.get('path')]
outcome = (_request_native_cancel(row, stop=stop) if cancel_native else outcome = (_request_native_cancel(row, stop=stop) if cancel_native else
(_force_close if force else _request_close)(row, stop=stop)) (_force_close if force else _request_close)(row, stop=stop))
except Exception as error: except Exception as error:
+25 -2
View File
@@ -22,13 +22,30 @@ def _library_key(library):
return os.path.normcase(os.path.normpath(str(library))) return os.path.normcase(os.path.normpath(str(library)))
def library_available(library): def library_available(library, *, live_only=False):
"""Cached queue predicate: no file, process or network queries.""" """Cached queue predicate: no file, process or network queries."""
if live_only:
return _live_library_available(library)
key = _library_key(library) key = _library_key(library)
with _observer_guard: with _observer_guard:
return not any(key in aliases for aliases in _retained_libraries.values()) return not any(key in aliases for aliases in _retained_libraries.values())
def _live_library_available(library):
"""Ignore stale retention, but wait for an actual registered observer."""
key = _library_key(library)
with _observer_guard:
for path, aliases in _retained_libraries.items():
observer = _observers.get(path)
if key not in aliases or observer is None or not observer[0].is_alive():
continue
state = _retained_results.get(path, {})
if (state.get('observer_initializing') is True or state.get('status') == 'ACTIVE'
or state.get('owned_worker_alive') is True):
return False
return True
def retained_session_snapshot(library): def retained_session_snapshot(library):
key = _library_key(library) key = _library_key(library)
with _observer_guard: with _observer_guard:
@@ -396,6 +413,10 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
result = {**result, 'status': 'RELEASED', 'identity_changed': True, 'release_proof': proof} result = {**result, 'status': 'RELEASED', 'identity_changed': True, 'release_proof': proof}
except Exception as error: except Exception as error:
result['detail'] += f';资源释放待确认:{error}' result['detail'] += f';资源释放待确认:{error}'
try:
result['owned_worker_alive'] = process is not None and process.poll() is None
except Exception:
result['owned_worker_alive'] = False
try: try:
write_session_record(observation, {**result, 'operation_id': result['operation_id'] or 'unknown'}) write_session_record(observation, {**result, 'operation_id': result['operation_id'] or 'unknown'})
except (OSError, ValueError): except (OSError, ValueError):
@@ -432,12 +453,14 @@ def handoff_session(path, *, evidence_root, process=None, progress=None, stop=No
_retained_libraries[key] = aliases _retained_libraries[key] = aliases
_retained_results[key] = {'status': 'UNCONFIRMED', 'phase': 'UNCONFIRMED', _retained_results[key] = {'status': 'UNCONFIRMED', 'phase': 'UNCONFIRMED',
'library': str(library) if library else None, 'block': session.get('block'), 'library': str(library) if library else None, 'block': session.get('block'),
'detail': '正在核对残留会话', 'operation_id': session.get('operation_id')} 'detail': '正在核对残留会话', 'operation_id': session.get('operation_id'),
'observer_initializing': True}
def notify(value): def notify(value):
with _observer_guard: with _observer_guard:
if _retained_libraries.get(key) is aliases: if _retained_libraries.get(key) is aliases:
previous = _retained_results[key] previous = _retained_results[key]
_retained_results[key] = {**previous, **value, _retained_results[key] = {**previous, **value,
'observer_initializing': False,
'library': value.get('library') or previous['library'], 'library': value.get('library') or previous['library'],
'operation_id': value.get('operation_id') or previous['operation_id'], 'operation_id': value.get('operation_id') or previous['operation_id'],
'block': value.get('block') or previous['block']} 'block': value.get('block') or previous['block']}
+1 -1
View File
@@ -136,7 +136,7 @@ class IcdbCheckTask(QObject):
return None, str(error) return None, str(error)
def bounded_write(): def bounded_write():
try: try:
return BatchTaskContext(str(self.requests[0].lmc_file)).run(write) return BatchTaskContext(str(self.requests[0].lmc_file)).run(write, metadata_only=True)
except Exception as error: except Exception as error:
return None, str(error) return None, str(error)
self.worker = ServiceWorker(bounded_write) self.worker = ServiceWorker(bounded_write)
+28 -9
View File
@@ -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) 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.""" """Request stop without interrupting a possibly unreturned native call."""
try: try:
if hasattr(process, 'request_stop'): if hasattr(process, 'request_stop'):
@@ -243,7 +243,7 @@ def _stop_worker(process, *, timeout=5):
process.wait(timeout=timeout) process.wait(timeout=timeout)
except Exception: except Exception:
pending = getattr(process, 'native_write_pending', lambda: False)() 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 from .defaults import NATIVE_WRITE_STOP_WAIT_SECONDS
try: try:
process.wait(timeout=max(0, NATIVE_WRITE_STOP_WAIT_SECONDS - timeout)) 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, _notify_progress(progress, {'stage': 'save_stop_requested', 'block': request.block_name,
'detail': detail}) 'detail': detail})
_stop_worker(process) _stop_worker(process, wait_for_native_write=not request.diagnostic_only)
return finish(VerifyResult('CANCELLED', stderr='已取消当前校验。', return finish(VerifyResult('CANCELLED', stderr='已取消当前校验。',
safe_to_continue=False, error_stage=phase.value)) safe_to_continue=False, error_stage=phase.value))
if events.exists(): if events.exists():
@@ -1237,7 +1237,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
target_pid=target_pid, target_pid=target_pid,
observation=last_post_verify_observation, 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: if dispatch_failure is not None:
status, detail = dispatch_failure status, detail = dispatch_failure
if progress: if progress:
@@ -1260,7 +1260,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
result = VerifyResult(**outcome) result = VerifyResult(**outcome)
except Exception as error: except Exception as error:
if 'process' in locals() and process.poll() is None: 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 launch_release = getattr(error, 'worker_launch_release', None) if 'process' not in locals() else None
if not worker_launch_attempted: if not worker_launch_attempted:
launch_release = {'process_created': False, 'worker_exited': True, 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: finally:
close_errors = [] close_errors = []
worker_exited = False worker_exited = False
observer_started = False
automation_release = {'safe_to_continue': 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): 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. # 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: try:
worker_exited = process.poll() is not None worker_exited = process.poll() is not None
if not worker_exited: 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'): if hasattr(process, 'automation_release_state'):
automation_release = process.automation_release_state() automation_release = process.automation_release_state()
else: else:
automation_release = {'safe_to_continue': worker_exited, 'remaining_pids': []} automation_release = {'safe_to_continue': worker_exited, 'remaining_pids': []}
if worker_exited: if not worker_exited and getattr(process, 'session_record_path', None):
process.close()
elif getattr(process, 'session_record_path', None):
from .session_record import handoff_session from .session_record import handoff_session
handoff_session(process.session_record_path, evidence_root=root, handoff_session(process.session_record_path, evidence_root=root,
process=process, progress=lambda state: _notify_progress(progress, process=process, progress=lambda state: _notify_progress(progress,
{'stage': 'session_observation', 'block': request.block_name, **state})) {'stage': 'session_observation', 'block': request.block_name, **state}))
observer_started = True
_notify_progress(progress, {'stage': 'session_handoff', 'block': request.block_name, _notify_progress(progress, {'stage': 'session_handoff', 'block': request.block_name,
'detail': '已转后台观察,可查看状态'}) 'detail': '已转后台观察,可查看状态'})
from .operation_queue import hold_native_handoff 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} 'automation_release': automation_release, 'worker_exited': worker_exited}
release_result['remaining_pids'] = sorted(set(release_result.get('remaining_pids', [])) release_result['remaining_pids'] = sorted(set(release_result.get('remaining_pids', []))
| set(automation_release.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: try:
from .operation_lifecycle import classify_save_state from .operation_lifecycle import classify_save_state
save_lines = ((root/'save.log').read_text('utf-8', errors='replace').splitlines() save_lines = ((root/'save.log').read_text('utf-8', errors='replace').splitlines()
+34
View File
@@ -0,0 +1,34 @@
from threading import Event
import pytest
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue, TaskHandoff, hold_native_handoff
from cbb_refresh_verify.ui.icdb_check_task import IcdbCheckTask
from cbb_refresh_verify.verify_runner import VerifyResult
from test_verify_runner import fixture, request
def test_cancelled_diagnostic_report_survives_ongoing_handoff_finalization(qtbot,tmp_path):
fixture(tmp_path)
req=request(tmp_path,diagnostic_only=True)
context=BatchTaskContext(str(req.lmc_file),native_policy='external')
entered,finish=Event(),Event()
def finalize():
try:
hold_native_handoff(None,tmp_path/'session.json')
except TaskHandoff:
entered.set()
assert finish.wait(5)
try:
with pytest.raises(TaskHandoff):context.run(finalize)
assert entered.is_set()
task=IcdbCheckTask([req],NativeQueue(),tmp_path/'reports')
task.active=True
task.results=[VerifyResult('CANCELLED',safe_to_continue=False,stderr='已转后台观察')]
task._report()
qtbot.waitUntil(lambda:not task.active,timeout=3000)
assert task.report is not None and task.report.exists(),task.report_error
assert not task.report_error and task.results[0].status=='CANCELLED'
finally:
finish.set()
context.close(timeout_seconds=2)
+129
View File
@@ -0,0 +1,129 @@
import json
from pathlib import Path
from threading import Event
import pytest
from cbb_refresh_verify import desktop_worker, session_record, verify_runner as runner
from cbb_refresh_verify.dialog_supervisor import DialogPollResult
from test_verify_runner import fixture, request, PORTS_OK
def worker_fixture(tmp_path, monkeypatch, *, exited=False):
fixture(tmp_path)
state = dict(waits=[], closed=False, observers=[], polling=False, polls=0)
stop = Event()
class Worker:
desktop=None; pid=700; companion_pids=[]; returncode=1 if exited else None
def __init__(self, command, name):
self.name=name
path=Path(command[-1]); state.update(path=path,job=json.loads(path.read_text()),worker=self)
self.session_record_path=path.with_name('session.json')
session_record.write_session_record(self.session_record_path, dict(
operation_id=state['job']['operation_id'],library=state['job']['library'],
block=state['job']['block']))
path.with_name('events.jsonl').write_text(json.dumps(dict(stage='pcb_worker_started',pcb_pid=901))+'\n')
path.with_name('outcome.json').write_text(json.dumps(dict(status='CANCELLED',safe_to_continue=False)))
def poll(self):return self.returncode
def request_stop(self):
assert not state['polling']
Path(state['job']['cancel_signal']).touch()
def native_write_pending(self):return True
def wait(self,timeout=5):
state['waits'].append(timeout)
if timeout <= 5:raise TimeoutError('native startup still closing')
self.returncode=1;return 1
def automation_release_state(self):return {'safe_to_continue':self.poll() is not None,'remaining_pids':[]}
def close(self):state['closed']=True
class Supervisor:
def poll(self,*args):
assert not state['observers']
state['polling']=True;state['polls']+=1;stop.set();state['polling']=False
return DialogPollResult()
monkeypatch.setattr(desktop_worker,'DesktopWorker',Worker)
monkeypatch.setattr(runner,'_cleanup_session_processes',lambda *a,**kw:
{'safe_to_continue':False,'remaining_pids':[901]})
monkeypatch.setattr(session_record,'handoff_session',lambda path,**kw:
state['observers'].append((path,kw,state['closed'])))
# Keep the task runner deterministic while exercising real supervisor ownership decisions.
monkeypatch.setattr('cbb_refresh_verify.operation_queue.hold_native_handoff',lambda *a:None)
return state,stop,lambda diagnostic:runner.run_verify(request(tmp_path,diagnostic_only=diagnostic),
port_check=PORTS_OK,stop=stop,dialog_supervisor_factory=lambda **kw:Supervisor())
@pytest.mark.parametrize('diagnostic',[True,False])
def test_diagnostic_cancel_yields_after_five_seconds_but_write_wait_is_unchanged(tmp_path,monkeypatch,diagnostic):
state,stop,run=worker_fixture(tmp_path,monkeypatch)
assert run(diagnostic).status=='CANCELLED'
assert state['waits']==([5] if diagnostic else [5,115])
assert state['observers'] and state['observers'][0][1]['process'] is state['worker']
assert not state['observers'][0][2] and not state['closed']
assert state['polls']==1
def test_exited_worker_with_live_native_clients_keeps_ownership_for_observer(tmp_path,monkeypatch):
state,stop,run=worker_fixture(tmp_path,monkeypatch,exited=True)
result=run(True)
assert result.status=='CANCELLED' and not result.safe_to_continue
assert len(state['observers'])==1, 'worker exit must not orphan the still-live registered native client'
path,options,already_closed=state['observers'][0]
assert path==state['worker'].session_record_path
assert options['process'] is state['worker'] and not already_closed and not state['closed']
original=json.loads(state['path'].with_name('result.json').read_text())
assert original['status']=='CANCELLED' and not original['safe_to_continue']
def test_failed_observer_registration_preserves_original_cancelled_result_and_handle(tmp_path,monkeypatch):
state,stop,run=worker_fixture(tmp_path,monkeypatch,exited=True)
def fail(*args,**kwargs):raise OSError('observation journal unavailable')
monkeypatch.setattr(session_record,'handoff_session',fail)
result=run(True)
assert result.status=='CANCELLED' and not result.safe_to_continue
assert not state['closed']
def test_natural_native_exit_gets_fresh_release_and_admits_waiting_scope_without_click(tmp_path,monkeypatch):
import subprocess,sys,time
from concurrent.futures import ThreadPoolExecutor
from types import SimpleNamespace
from cbb_refresh_verify import library_lock
from cbb_refresh_verify.operation_queue import NativeQueue,BatchTaskContext
from test_issue15_resource_release import library_file,cleanup
library=library_file(tmp_path)
gate=tmp_path/'native-exit'
native=subprocess.Popen([sys.executable,'-c',
'from pathlib import Path; import time,sys\nwhile not Path(sys.argv[1]).exists(): time.sleep(.02)',str(gate)])
evidence=tmp_path/'evidence';evidence.mkdir();path=evidence/'session.json'
session_record.write_session_record(path,dict(operation_id='cancelled-owned',operation='verify',
library=str(library),block='A',processes=[dict(role='native',**session_record.process_identity(native.pid))]))
original=evidence/'result.json';original.write_text('{"status":"CANCELLED","safe_to_continue":false}')
before=original.read_bytes()
with library_lock.LibraryLock(library,block_name='A',safe_to_release=lambda:False) as lease:pass
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners',lambda *a,**kw:[])
active=Event();stop=Event();closed=[]
owner=SimpleNamespace(pid=700,poll=lambda:0,close=lambda:closed.append(native.poll()))
thread=None
try:
session_record.handoff_session(path,evidence_root=evidence,process=owner,stop=stop,
progress=lambda state:active.set() if state['status']=='ACTIVE' else None)
assert active.wait(3) and not closed
key=str(path.absolute()).casefold()
thread=session_record._observers[key][0]
assert not session_record.library_available(library)
queue=NativeQueue()
def admitted():
with library_lock.LibraryLock(library,checkpoint=False):return 'fresh admission'
with ThreadPoolExecutor(1) as pool:
future=pool.submit(queue.run,admitted,context=BatchTaskContext(str(library)),stop=stop)
time.sleep(.15);assert not future.done()
gate.touch();native.wait(5)
assert future.result(8)=='fresh admission'
thread.join(3)
assert not thread.is_alive() and closed==[0]
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
finally:
gate.touch();native.wait(5);stop.set()
if thread:thread.join(3)
cleanup(lease)
+90
View File
@@ -0,0 +1,90 @@
from concurrent.futures import ThreadPoolExecutor
from threading import Event
from types import SimpleNamespace
import pytest
from cbb_refresh_verify import session_record as sessions
from cbb_refresh_verify.operation_queue import BatchTaskContext,NativeQueue
def observed(tmp_path,monkeypatch,*,status='ACTIVE',initializing=False,registered=True,alive=True,worker_alive=False):
library=tmp_path/'lib.lmc';key='record'
states={key:dict(status=status,observer_initializing=initializing,owned_worker_alive=worker_alive)}
monkeypatch.setattr(sessions,'_retained_libraries',{key:{sessions._library_key(library)}})
monkeypatch.setattr(sessions,'_retained_results',states)
monkeypatch.setattr(sessions,'_observers',{key:(SimpleNamespace(is_alive=lambda:alive),None)} if registered else {})
return library,states[key]
@pytest.mark.parametrize('initializing',[False,True])
def test_same_library_waits_for_actual_observer_and_other_library_can_run(tmp_path,monkeypatch,initializing):
library,state=observed(tmp_path,monkeypatch,status='UNCONFIRMED' if initializing else 'ACTIVE',initializing=initializing)
queue=NativeQueue();waiting=Event();entered=Event();stop=Event()
# Queue predicates may not perform filesystem/process probes.
monkeypatch.setattr(sessions,'query_process_snapshot',lambda *a,**kw:pytest.fail('queue probed native clients'),raising=False)
context=BatchTaskContext(str(library),native_policy='external')
with ThreadPoolExecutor(2) as pool:
future=pool.submit(queue.run,lambda:entered.set() or 'resumed',context=context,stop=stop,waiting=waiting.set)
try:
assert waiting.wait(.5) and not entered.is_set()
other=pool.submit(queue.run,lambda:'unrelated',context=BatchTaskContext(str(tmp_path/'other.lmc')))
assert other.result(1)=='unrelated'
state.update(status='RELEASED',observer_initializing=False)
assert future.result(2)=='resumed'
finally:
stop.set();queue.wake()
@pytest.mark.parametrize('case',['orphaned','dead_thread','unconfirmed','released'])
def test_stale_or_nonlive_cache_does_not_prevent_fresh_scoped_admission(tmp_path,monkeypatch,case):
library,state=observed(tmp_path,monkeypatch,status='RELEASED' if case=='released' else 'UNCONFIRMED',
registered=case!='orphaned',alive=case!='dead_thread')
assert not sessions.library_available(library) # Historical cache is deliberately stale.
assert NativeQueue().run(lambda:'fresh admission',context=BatchTaskContext(str(library)))=='fresh admission'
def test_live_owned_worker_still_blocks_when_its_journal_cannot_be_read(tmp_path,monkeypatch):
library,state=observed(tmp_path,monkeypatch,status='UNCONFIRMED',worker_alive=True)
queue=NativeQueue();waiting=Event();stop=Event();entered=Event()
with ThreadPoolExecutor(1) as pool:
future=pool.submit(queue.run,lambda:entered.set(),context=BatchTaskContext(str(library)),
stop=stop,waiting=waiting.set)
try:
assert waiting.wait(.5) and not entered.is_set()
state['owned_worker_alive']=False
future.result(2)
finally:
stop.set();queue.wake()
def test_actual_observer_initialization_closes_queue_race_then_unconfirmed_cache_releases_it(tmp_path,monkeypatch):
library=tmp_path/'lib.lmc';path=tmp_path/'session.json'
sessions.write_session_record(path,dict(operation_id='initializing',library=str(library)))
entered,finish,waiting,stop=Event(),Event(),Event(),Event()
def first_scan(*args,**kwargs):
entered.set();assert finish.wait(3)
return dict(status='UNCONFIRMED',operation_id='initializing',library=str(library),
detail='No live client; fresh admission must recheck resource proof')
monkeypatch.setattr(sessions,'_reconcile',first_scan)
queue=NativeQueue();thread=None
try:
sessions.handoff_session(path,evidence_root=tmp_path,observe_only=True,stop=stop)
assert entered.wait(2)
thread=sessions._observers[str(path.absolute()).casefold()][0]
assert not sessions.library_available(library,live_only=True)
with ThreadPoolExecutor(1) as pool:
future=pool.submit(queue.run,lambda:'fresh scoped admission',context=BatchTaskContext(str(library)),
waiting=waiting.set,stop=stop)
try:
assert waiting.wait(1) and not future.done()
finish.set()
assert future.result(2)=='fresh scoped admission'
finally:
stop.set();queue.wake()
thread.join(2)
assert not sessions.library_available(library) # Unproved historical state remains visible.
assert sessions.library_available(library,live_only=True)
finally:
finish.set();stop.set()
if thread:thread.join(2)
+150
View File
@@ -0,0 +1,150 @@
from contextlib import nullcontext
import json
from pathlib import Path
import pytest
from cbb_refresh_verify import diagnostic_open, residual_sessions as sessions, session_record
from test_cancelled_loaded_startup import loaded_fixture
def registered_fixture(tmp_path,monkeypatch,*,closed=False,obstacle=''):
import win32con,win32event
job,records,messages,state=loaded_fixture(tmp_path,monkeypatch)
state.update(tip=False,closing=closed)
job.update(operation_id='verify-old',operation='verify',library=job['lmc_file'],block='IP101GR',
session_record_path=str(tmp_path/'session.json'),interactive_input=False)
job.pop('diagnostic_log_baseline') # Old builds never persisted this; do not invent it.
identity=dict(pid=12,created_handle='native-created',image=records[0][1],owner_sid='current',session_id=1)
worker=dict(pid=11,created_handle='worker-created',image='C:/App/CbbRefreshVerify.exe',owner_sid='current',session_id=1)
session_record.write_session_record(tmp_path/'session.json',dict(operation_id='verify-old',operation='verify',
library=job['library'],block=job['block'],desktop=job['desktop'],cancel_signal=job['cancel_signal'],
processes=[dict(role='worker',**worker)],resource_scope=dict(scope_state='DECLARED',
library=job['library'],operation_id='verify-old',projects=[job['project']])))
session_record.write_session_record(tmp_path/'worker-state.json',dict(operation_id='verify-old',
phase='UNCONFIRMED',processes=[dict(role='native',**identity)]))
result=dict(status='CANCELLED',error_stage='PCB_STARTING',safe_to_continue=False,
write_outcome=dict(save_state='UNKNOWN'))
events=[dict(stage='pcb_baseline',pids=[]),dict(stage='pcb_worker_started',pcb_pid=12,command=records[0][2])]
if closed:events.append(dict(stage='startup_close_requested',pcb_pid=12,hwnd=100))
if obstacle=='ordinary':job['diagnostic_only']=False
if obstacle=='interactive':job['interactive_input']=True
if obstacle=='other_project':job['project']=str(tmp_path/'Other.prj')
if obstacle=='wrong_operation':job['operation_id']='other'
if obstacle=='missing_cancel':Path(job['cancel_signal']).unlink()
if obstacle=='save':(tmp_path/'save.log').write_text('SAVE_ENTERED')
if obstacle=='save_event':events.append(dict(stage='save_start'))
if obstacle=='not_cancelled':result['status']='FAIL'
if obstacle=='wrong_stage':result['error_stage']='SAVING'
(tmp_path/'request.json').write_text(json.dumps(job))
(tmp_path/'result.json').write_text(json.dumps(result))
(tmp_path/'events.jsonl').write_text(''.join(json.dumps(e)+'\n' for e in events))
row=dict(pid=12,name='ExpeditionPCB.exe',command=records[0][2],created='native-created',
executable=identity['image'],library=job['library'],scope_confirmed=True,runtime_phase='UNCONFIRMED',
session_records=[dict(path=str(tmp_path/'session.json'),operation_id='verify-old')],**{k:v for k,v in identity.items() if k!='pid'})
if obstacle=='foreign_identity':row['created_handle']='replacement'
if obstacle=='unscoped':row['scope_confirmed']=False
monkeypatch.setattr(sessions,'_process_handle',lambda pid,**kw:nullcontext(pid))
monkeypatch.setattr(sessions,'_handle_identity',lambda handle:{k:v for k,v in (worker if handle==11 else identity).items() if k in ('created_handle','image')})
monkeypatch.setattr(sessions,'_force_ownership',lambda h,p:{'owner_sid':'current','session_id':1,'force_user_confirmed':True})
def wait(handle,ms):
state['ticks']+=ms/1000
return win32con.WAIT_OBJECT_0 if handle==11 and obstacle!='live_worker' or handle==12 and not records else win32con.WAIT_TIMEOUT
monkeypatch.setattr(win32event,'WaitForSingleObject',wait)
monkeypatch.setattr(sessions,'time',diagnostic_open.time)
monkeypatch.setattr(sessions,'query_process_snapshot',lambda:[row])
monkeypatch.setattr(sessions,'_legacy_diagnostic_windows',lambda r:diagnostic_open._windows(r['pid']),raising=False)
monkeypatch.setattr(sessions,'_window_desktop',lambda *a,**kw:nullcontext())
monkeypatch.setattr(sessions,'_post_window_message',lambda w,h,p,m:__import__('win32gui').PostMessage(h,m,0,0))
monkeypatch.setattr(session_record,'active_write_records',lambda library:
[dict(operation_id='other-write' if obstacle=='other_write' else 'verify-old',phase='UNCONFIRMED')])
monkeypatch.setattr(diagnostic_open,'_load_evidence',lambda *a,**kw:
pytest.fail('legacy cleanup may not manufacture missing prelaunch log evidence'))
return job,row,records,messages,state
@pytest.mark.parametrize('closed',[False,True])
def test_normal_release_finishes_exact_registered_cancelled_startup_without_save_or_second_close(tmp_path,monkeypatch,closed):
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch,closed=closed)
original=(tmp_path/'result.json').read_bytes()
result=sessions._request_close(row)
assert result['release_status']=='RELEASED'
assert messages==([(31,245)] if closed else [(31,245),(100,16)])
assert not records and (tmp_path/'result.json').read_bytes()==original
@pytest.mark.parametrize('obstacle',['ordinary','interactive','other_project','wrong_operation','missing_cancel',
'save','save_event','not_cancelled','wrong_stage','foreign_identity','unscoped','live_worker','other_write'])
def test_registered_startup_release_rejects_partial_foreign_or_writing_evidence(tmp_path,monkeypatch,obstacle):
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch,obstacle=obstacle)
assert sessions._request_close(row)['release_status'] in ('WAITING','IDENTITY_CHANGED')
assert not messages and records
@pytest.mark.parametrize('error_code',[87,5])
def test_registered_worker_exit_requires_kernel_exit_or_missing_pid_not_access_denial(tmp_path,monkeypatch,error_code):
import pywintypes
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch)
original=sessions._process_handle
def open_process(pid,**kwargs):
if pid==11:raise pywintypes.error(error_code,'OpenProcess','worker probe')
return original(pid,**kwargs)
monkeypatch.setattr(sessions,'_process_handle',open_process)
result=sessions._request_close(row)
assert result['release_status']==('RELEASED' if error_code==87 else 'WAITING')
assert messages==([(31,245),(100,16)] if error_code==87 else [])
def test_completed_identity_snapshots_fit_existing_release_budget_without_duplicate_polling(tmp_path,monkeypatch):
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch)
def snapshot():
state['ticks']+=.4
return [row]
monkeypatch.setattr(sessions,'query_process_snapshot',snapshot)
assert sessions._request_close(row)['release_status']=='RELEASED'
assert messages==[(31,245),(100,16)] and state['ticks']<3
@pytest.mark.parametrize('change',['result','stop','identity'])
def test_legacy_no_rechecks_registered_state_after_fresh_windows(tmp_path,monkeypatch,change):
from threading import Event
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch)
stop=Event();reads=[]
original=sessions._legacy_diagnostic_windows
def windows(bound):
value=original(bound);reads.append(1)
if len(reads)==2:
if change=='result':(tmp_path/'result.json').write_text('{"status":"PASS"}')
if change=='stop':stop.set()
if change=='identity':row['created_handle']='replacement'
return value
monkeypatch.setattr(sessions,'_legacy_diagnostic_windows',windows)
assert sessions._request_close(row,stop=stop)['release_status']=='WAITING'
assert records and not messages
@pytest.mark.parametrize('frame_handle',[100,999])
def test_normal_release_clears_registered_late_tip_despite_historical_unknown_phase(tmp_path,monkeypatch,frame_handle):
import win32con,win32gui
from copy import deepcopy
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch,closed=True)
state['tip']=True
windows=[dict(hwnd=frame_handle,**{'class':'Afx:main'},title='Expedition PCB',enabled=False,
desktop=job['desktop'],children=[],documents=[]),
dict(hwnd=200,**{'class':'#32770'},title='Tip of the Day',enabled=True,
desktop=job['desktop'],documents=[],children=[dict(hwnd=201,**{'class':'Button'},id=1,text='OK',enabled=True)])]
monkeypatch.setattr(sessions,'_process_windows',lambda pids:{12:deepcopy(windows)})
monkeypatch.setattr(win32gui,'GetWindow',lambda h,k:frame_handle if h==200 and k==win32con.GW_OWNER else 0)
def post(hwnd,message,*args):messages.append((hwnd,message));records.clear()
monkeypatch.setattr(win32gui,'PostMessage',post)
result=sessions._request_close(row)
assert result['release_status']==('RELEASED' if frame_handle==100 else 'WAITING')
assert messages==([(201,245)] if frame_handle==100 else [])
def test_registered_cleanup_cannot_act_from_another_windows_user_session(tmp_path,monkeypatch):
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch)
monkeypatch.setattr(sessions,'_force_ownership',lambda h,p:
dict(owner_sid='current',session_id=1,force_user_confirmed=False))
assert sessions._request_close(row)['release_status']=='WAITING'
assert not messages and records