fix: clear verified queue blockers and exit idle orphan workers

This commit is contained in:
阿白叔 committed 2026-11-09 10:11:36 +08:00
1 parent 96e179ba65
commit ca39fb3517
16 files changed
+537 -70

No files matched your search

@@ -0,0 +1,53 @@
{
"date": "2026-11-09",
"review_url": "https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/pulls/13#issuecomment-319",
"base_commit": "96e179ba6529eefd1401a012155cd7b7cbc0ffc3",
"environment": {
"python": "3.12.10",
"windows": "10.0.26200"
},
"full_suite": {
"tests": 1732,
"failures": 0,
"errors": 0,
"skipped": 0,
"duration_seconds": 501.166,
"xml": "F:\\Sync Tool Skyworth\\Work Maps\\cbb-refresh-verify-795\\diagnostics\\cbb-refresh-verify-795-review-stack-fixes-full.xml",
"xml_sha256": "578dd4b01db5c18ce657430ce3b2cd2dba8516d8191bdd31bf4e0df78187b3ba",
"warnings": [
"Existing pywinauto Revert to STA COM threading mode warning"
]
},
"focused_suite": {
"passed": 79
},
"compileall": "passed",
"diff_check": "passed",
"independent_review": "No remaining important findings in the scoped diff; durable completion feedback addressed",
"tested_files_sha256": {
"src/cbb_refresh_verify/desktop_worker.py": "173a951d510e78e088c439768ecfe31ab198723e7717b314b4a74a9d590a1fe0",
"src/cbb_refresh_verify/library_lock.py": "391fb2f0b25f400676ba230472fb3bbd4a0d994ed5a8dfee46f8f32c7f198219",
"src/cbb_refresh_verify/mentor_adapter.py": "1fc8c98d5508120e03bf001a42dd3e0da7b165eb18c90f1aed83147d17db8638",
"src/cbb_refresh_verify/native_management.py": "96c2fe613a657d110302c8f7cc033b91ef256e168036f4794eb39be475e69de8",
"src/cbb_refresh_verify/residual_sessions.py": "a19622be3e959a7e64bf9de953450203499fb82618da8537307e5f61ef4d793b",
"src/cbb_refresh_verify/session_record.py": "c4c5771d11d7469980c02f2fb264b26e5ee26634a37daae008dbb65097ab4e0c",
"src/cbb_refresh_verify/ui/main_window.py": "e46096ce784933f3ab47e4c316f3c84e3e5cf07242489323f6234b61aeb22f80",
"src/cbb_refresh_verify/ui/residual_release_dialog.py": "ec7566bac6d92845f98933bffd4c06f31eeec42946e69699aff94e31f9a8baef",
"tests/test_incident_safety.py": "52f3ccb04db8971bf38628c2bd334d4fd79795e97de6538f6f66cb35f7123ece",
"tests/test_integration.py": "9e9d30692607c889e1e3744a63c79c68e3af7d4c86804e0bcebe48e68765086c",
"tests/test_management_supervision.py": "107196ce8a00dd76e7e3e83fb47ae2e67c857fa234492ba593bf71a7d7e1c5e5",
"tests/test_residual_release_dialog.py": "7d2bf95afc2f9112b9433c7d2fefde3b35b5e85a70a6bd506471c039e2505509",
"tests/test_review_319.py": "831fd34d426abb4436b9132bfb7ed754994155a34ac60a59378dee0964b54d28"
},
"not_verified": [
"Frozen execution of this revision",
"Real Mentor native writes/save/close during supervisor loss",
"Windows 10",
"Shared network/cross-machine release",
"ICDB field recovery"
],
"automatic_icdb_repair": false,
"candidate_rebuilt": false,
"merged": false,
"issue_closed": false
}
@@ -0,0 +1,33 @@
# PR #13 review 319 validation
Review: https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/pulls/13#issuecomment-319
Baseline: `96e179ba6529eefd1401a012155cd7b7cbc0ffc3`.
## Changes
- Successful explicit resource proof acknowledges the retained queue entries captured before proof. The alias-set identity is a generation token: unrelated libraries and newer handoffs remain retained. Scoped proof acknowledges only the matching block.
- Final observer outcomes remain available in the residual dialog after its observer ends. The existing refresh/confirm actions provide a running-session retry path. Successful release records a matching operation's RELEASED observation, so history refresh does not resurrect it as unconfirmed. Journal failure is reported separately from the established release fact.
- Discovery observes process identities only. It never opens production databases exclusively, deletes coordination checkpoints, closes clients, or replays operations. Startup observers use the same read-only mode; only explicit release or already-authorized task reconciliation can establish resource release.
- Supervisor loss sets cancellation independently of evidence-write success. An idle worker exits with code 75 only when its matching journal and synchronized in-memory phase both confirm no pending native write and every registered helper has exited. Missing/unknown/stale-active state retains the worker. The watchdog rechecks returned calls and cannot authorize a subsequent native write/helper. Native children are never terminated by this exit; worker exit is not database-release or Save-success evidence.
- Worker phase persistence and watchdog journal reads share a gate to avoid a Windows sharing violation between reading the JSON and atomically replacing it. The dead `_terminate_pid` function is removed; cleanup tests observe actual process-termination boundaries.
## Verification
Commands run through the existing test wrapper with a dedicated mutex namespace and Qt offscreen; no user/native Mentor process is part of these fixtures.
```
python -u -X utf8 diagnostics/run_review_fixes.py "F:/Sync Tool Skyworth/Work Maps/cbb-refresh-verify-795-review-stack"
python -X utf8 -m compileall -q src tests
git diff --check
```
The focused session/watchdog/management/dialog/lease regression run passed 79 tests. The initial regressions were observed failing before the corresponding fixes: explicit proof left a library unavailable, history scan attempted release, orphan idle workers stayed alive, residual refresh omitted runtime records, and successful release reappeared as unconfirmed. Real harmless Windows subprocess fixtures cover PRECALL/POSTCALL, NATIVE_CALL/CLOSING, unknown/missing/stale journals, failed cancel/evidence writes, and rechecking after return.
Full-suite result is recorded in `2026-11-09-review-319-validation.json`. Independent review found no remaining P1/P2 in this scoped diff; its observation about durable completion was addressed and regression-tested.
## Limits
This is source/PR validation. The earlier `0.2.4b2-iconfix-44bb734` executable has not been rebuilt with these changes and must not be used as evidence for this revision. Its icon assets are preserved.
Not validated here: frozen execution of this revision, real Mentor Save/Close or library writes during supervisor loss, Windows 10, shared/network or cross-machine release. ICDB automatic Repair remains disabled. No field-recovery acceptance, merge, formal publication or issue closure is implied.
@@ -0,0 +1,32 @@
# PR #13 comment 319: residual lifecycle fixes
**Goal:** Fix the P2 and three P3 findings against head `96e179b` without interrupting an in-flight native writer.
**Scope:** https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/pulls/13#issuecomment-319
**Constraints:** Python 3.12/PySide6/pywin32, no new dependencies; existing isolated worktree and icon retained; no automatic ICDB Repair, native process termination, merge, release or issue closure. Real Mentor and shared-network acceptance remains outstanding.
## 1. Release proof and queue state
- [x] Reproduce an ended UNCONFIRMED observer leaving its library unavailable after explicit successful release.
- [x] Snapshot retained entries before proof; acknowledge only those entries, for the matching library, while proof handles are still held. Failed proof and newer handoffs remain blocked. Test independent libraries and queue resumption.
- [x] Retain final observer states for the existing residual panel, including unreadable journals; expose fresh recheck through that panel.
## 2. Passive discovery
- [x] Test that discovery of an exited session never calls release proof or modifies library checkpoints.
- [x] Separate process observation from explicit reconciliation; startup observation stays read-only, reports ACTIVE/UNCONFIRMED and cannot claim RELEASED without proof.
- [x] Refresh journal/status information from the running residual dialog, keeping all I/O off the Qt thread.
## 3. Supervisor loss
- [x] Use harmless real subprocesses to test PRECALL/POSTCALL exit, NATIVE_CALL/CLOSING retention, missing/unreadable journals and failed cancellation/journal writes.
- [x] Synchronize phase transitions and helper registration with watchdog shutdown. Only matching durable and in-memory non-pending state allows worker-only exit; preserve active helpers and unknown states. Prevent subsequent calls after supervisor loss.
- [x] Recheck after retained calls return; do not kill a native child or interpret worker exit as release proof.
## 4. Dead code and validation
- [x] Delete unused `_terminate_pid`; replace obsolete test hooks with assertions at actual process termination boundaries.
- [x] Run focused regressions, full Python suite, compileall and diff checks. Record results and field-validation limits, then commit scoped changes. Do not rebuild or publish a candidate without documenting its source and validation.
Execution: implement inline in the existing isolated worktree; red/green tests per task. The user has authorized these fixes, so no further plan confirmation is needed.
+56 -20
View File
@@ -1,6 +1,8 @@
"""Launch automation on a private Windows desktop, never the input desktop.""" """Launch automation on a private Windows desktop, never the input desktop."""
import subprocess import subprocess
import os import os
import logging
from contextlib import contextmanager
from pathlib import Path from pathlib import Path
import threading import threading
import win32con import win32con
@@ -13,6 +15,39 @@ import win32service
_automation_clients = [] _automation_clients = []
_automation_clients_lock = threading.Lock() _automation_clients_lock = threading.Lock()
_supervisor_lost = threading.Event() _supervisor_lost = threading.Event()
_native_phases = {}
def _phase_pending(phase):
if phase not in ('PRECALL', 'POSTCALL', 'RELEASE_CHECK', 'FINISHED', 'NATIVE_CALL', 'CLOSING'):
return None
from .operation_lifecycle import stop_disposition
return stop_disposition(call_started=phase != 'PRECALL',
call_returned=phase in ('POSTCALL', 'RELEASE_CHECK', 'FINISHED'),
identity_confirmed=True) == 'WAIT_FOR_RETURN'
@contextmanager
def worker_phase(operation_id, phase):
with _automation_clients_lock:
if _supervisor_lost.is_set() and phase in ('NATIVE_CALL', 'CLOSING'):
raise RuntimeError('WORKER_CANCELLED: 监督器已退出,未提交新的原生调用')
_native_phases[operation_id] = phase
yield
def _record_write_pending(record_path, operation_id=None):
if record_path is None:
return None
from .session_record import read_session_record
path = Path(record_path).with_name('worker-state.json')
try:
state = read_session_record(path, evidence_root=path.parent)
if operation_id is not None and state['operation_id'] != operation_id:
return None
return _phase_pending(state.get('phase'))
except (OSError, ValueError):
return None
def launch_save_helper(command, *, start_gate=None, identity_path=None, operation_id=None, **kwargs): def launch_save_helper(command, *, start_gate=None, identity_path=None, operation_id=None, **kwargs):
@@ -58,13 +93,29 @@ def start_supervisor_watchdog(supervisor_pid, *, cancel_path=None, stop_clients=
def watch(): def watch():
try: try:
win32event.WaitForSingleObject(parent, win32event.INFINITE) win32event.WaitForSingleObject(parent, win32event.INFINITE)
_supervisor_lost.set() with _automation_clients_lock:
_supervisor_lost.set()
if cancel_path is not None: if cancel_path is not None:
Path(cancel_path).touch() try:
Path(cancel_path).touch()
except OSError:
logging.getLogger(__name__).warning('Unable to record orphan cancellation', exc_info=True)
if session_record_path is not None: if session_record_path is not None:
from .session_record import write_session_record from .session_record import write_session_record
write_session_record(Path(session_record_path).with_name('supervisor-lost.json'), try:
{'operation_id': operation_id, 'phase': 'SUPERVISOR_LOST'}) write_session_record(Path(session_record_path).with_name('supervisor-lost.json'),
{'operation_id': operation_id, 'phase': 'SUPERVISOR_LOST'})
except (OSError, ValueError):
logging.getLogger(__name__).warning('Unable to record supervisor loss', exc_info=True)
while True:
with _automation_clients_lock:
pending = _record_write_pending(session_record_path, operation_id)
# The same gate blocks new calls/helpers after loss. A stale
# POSTCALL on disk cannot override an active in-memory call.
if (pending is False and _phase_pending(_native_phases.get(operation_id)) is False
and all(helper.poll() is not None for helper in _automation_clients)):
os._exit(75) # This Python worker only; native children remain untouched.
threading.Event().wait(.2)
finally: finally:
parent.Close() parent.Close()
@@ -232,22 +283,7 @@ class DesktopWorker:
register_worker_session(self, job) register_worker_session(self, job)
def native_write_pending(self): def native_write_pending(self):
if not getattr(self, 'session_record_path', None): return _record_write_pending(getattr(self, 'session_record_path', None))
return None
from .session_record import read_session_record
path = self.session_record_path.with_name('worker-state.json')
try:
state = read_session_record(path, evidence_root=path.parent)
from .operation_lifecycle import stop_disposition
phase = state.get('phase')
started = (False if phase == 'PRECALL' else True if phase in
('NATIVE_CALL', 'CLOSING', 'POSTCALL', 'RELEASE_CHECK', 'FINISHED') else None)
returned = (False if phase in ('PRECALL', 'NATIVE_CALL', 'CLOSING') else True if phase in
('POSTCALL', 'RELEASE_CHECK', 'FINISHED') else None)
return stop_disposition(call_started=started, call_returned=returned,
identity_confirmed=True) in ('WAIT_FOR_RETURN', 'OBSERVE_UNCONFIRMED')
except (OSError, ValueError):
return None
def request_stop(self): def request_stop(self):
if not hasattr(self, 'cancel_path'): if not hasattr(self, 'cancel_path'):
+3
View File
@@ -528,6 +528,8 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
""" """
import win32con, win32event, win32file import win32con, win32event, win32file
library = Path(library).resolve(strict=True) library = Path(library).resolve(strict=True)
from .session_record import retained_session_snapshot, acknowledge_library_release
retained = retained_session_snapshot(library)
evidence = Path(evidence_dir).resolve() evidence = Path(evidence_dir).resolve()
if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence): if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence):
raise ValueError('释放检查证据必须位于目标库之外。') raise ValueError('释放检查证据必须位于目标库之外。')
@@ -584,6 +586,7 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
if owner: if owner:
owner['handle'].Close() owner['handle'].Close()
del _owners[lease.key] del _owners[lease.key]
acknowledge_library_release(record, retained, block_name=scoped)
return record return record
finally: finally:
if external is not None: if external is not None:
+2 -13
View File
@@ -928,19 +928,6 @@ def execute_job(job, emit):
lm_session.close() lm_session.close()
def _terminate_pid(pid, *, timeout=20):
"""Terminate a validated automation helper PID, never its native children."""
try:
subprocess.run(
[str(Path(os.environ.get('WINDIR', r'C:\Windows'))/'System32'/'taskkill.exe'), '/PID', str(int(pid)), '/F'],
capture_output=True,
timeout=timeout,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0),
)
except Exception:
pass
def cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid=None, *, def cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid=None, *,
expected_pcb=None, baseline_pids=(), emit=None): expected_pcb=None, baseline_pids=(), emit=None):
"""Observe native process release without interrupting database writes. """Observe native process release without interrupting database writes.
@@ -1025,6 +1012,8 @@ def worker_main(request_path):
try: try:
if job.get('supervisor_pid'): if job.get('supervisor_pid'):
from .session_record import update_worker_state
update_worker_state(job, 'PRECALL')
from .desktop_worker import start_supervisor_watchdog from .desktop_worker import start_supervisor_watchdog
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job.get('cancel_signal'), stop_clients=True, start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job.get('cancel_signal'), stop_clients=True,
supervisor_identity=job.get('supervisor_identity'), supervisor_identity=job.get('supervisor_identity'),
@@ -1224,6 +1224,8 @@ def management_worker_main(request_path):
raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。') raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。')
_require_management_continuation(job, root) _require_management_continuation(job, root)
if job.get('cancel_signal'): if job.get('cancel_signal'):
from .session_record import update_worker_state
update_worker_state(job, 'PRECALL')
from .desktop_worker import start_supervisor_watchdog from .desktop_worker import start_supervisor_watchdog
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'], start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'],
supervisor_identity=job.get('supervisor_identity'), supervisor_identity=job.get('supervisor_identity'),
@@ -512,6 +512,10 @@ def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False, ca
or Path(proof.get('library', '')).resolve() != library): or Path(proof.get('library', '')).resolve() != library):
raise ValueError('释放证明与当前库不一致') raise ValueError('释放证明与当前库不一致')
result['status'] = 'RELEASE_CONFIRMED' result['status'] = 'RELEASE_CONFIRMED'
from .session_record import record_session_release
errors = record_session_release(proof, plan.get('runtime_sessions', []))
if errors:
result['evidence_error'] = ';'.join(errors)
except BaseException as error: except BaseException as error:
result.update(status='PARTIAL', detail=f'{type(error).__name__}: {error}') result.update(status='PARTIAL', detail=f'{type(error).__name__}: {error}')
if not isinstance(error, Exception): if not isinstance(error, Exception):
+120 -30
View File
@@ -15,6 +15,7 @@ IDENTITY_KEYS = ('pid', 'created_handle', 'image', 'owner_sid', 'session_id')
_observers = {} _observers = {}
_observer_guard = threading.Lock() _observer_guard = threading.Lock()
_retained_libraries = {} _retained_libraries = {}
_retained_results = {}
def _library_key(library): def _library_key(library):
@@ -28,6 +29,61 @@ def library_available(library):
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 retained_session_snapshot(library):
key = _library_key(library)
with _observer_guard:
return {path: (aliases, _retained_results.get(path, {}).get('block'))
for path, aliases in _retained_libraries.items() if key in aliases}
def acknowledge_library_release(proof, snapshot, *, block_name=None):
"""Called under fresh proof handles; never clear a newer handoff."""
if proof.get('status') != 'RELEASE_CONFIRMED':
return
key = _library_key(proof['library'])
with _observer_guard:
records = [dict(_retained_results.get(path, {}), path=path)
for path, (aliases, block) in snapshot.items()
if key in aliases and _retained_libraries.get(path) is aliases
and (block_name is None or block == block_name)]
record_session_release(proof, records)
with _observer_guard:
for path, (aliases, block) in snapshot.items():
if (key in aliases and _retained_libraries.get(path) is aliases
and (block_name is None or block == block_name)):
_retained_libraries.pop(path, None)
_retained_results.pop(path, None)
def record_session_release(proof, records):
"""Persist a past proof only for the pre-proof operation, never replay it."""
errors = []
if proof.get('status') != 'RELEASE_CONFIRMED':
return errors
library = Path(proof['library']).resolve()
for record in records:
try:
path = Path(record['path'])
session = read_session_record(path, evidence_root=path.parent)
if (session['operation_id'] != record.get('operation_id')
or Path(session['library']).resolve() != library):
continue
write_session_record(path.with_name('observation-release-'+uuid4().hex+'.json'), {
'operation_id': session['operation_id'], 'library': str(library),
'status': 'RELEASED', 'release_proof': proof})
except (OSError, ValueError, KeyError) as error:
errors.append(str(error))
logging.getLogger(__name__).warning('Unable to record session release', exc_info=True)
return errors
def retained_session_states(library):
key = _library_key(library)
with _observer_guard:
return [dict(_retained_results[path], path=path) for path, aliases in _retained_libraries.items()
if key in aliases and path in _retained_results]
def register_worker_session(worker, job): def register_worker_session(worker, job):
"""The launcher must persist identity before opening the worker's gate.""" """The launcher must persist identity before opening the worker's gate."""
path = Path(job['session_record_path']) path = Path(job['session_record_path'])
@@ -43,26 +99,28 @@ def update_worker_state(job, phase, *, identities=(), write_outcome=None, requir
if required: if required:
raise ValueError('原生写入缺少会话记录位置,未提交新的调用') raise ValueError('原生写入缺少会话记录位置,未提交新的调用')
return False return False
path = Path(job['session_record_path']).with_name('worker-state.json') from .desktop_worker import worker_phase
try: with worker_phase(job['operation_id'], phase):
state = ({'operation_id': job['operation_id'], 'processes': []} if not path.exists() path = Path(job['session_record_path']).with_name('worker-state.json')
else read_session_record(path, evidence_root=path.parent)) try:
if state['operation_id'] != job['operation_id']: state = ({'operation_id': job['operation_id'], 'processes': []} if not path.exists()
raise ValueError('工作状态操作身份不一致') else read_session_record(path, evidence_root=path.parent))
for role, pid in identities: if state['operation_id'] != job['operation_id']:
if pid and not any(p['pid'] == pid for p in state['processes']): raise ValueError('工作状态操作身份不一致')
state['processes'].append({'role': role, **process_identity(pid)}) for role, pid in identities:
state['phase'] = phase if pid and not any(p['pid'] == pid for p in state['processes']):
if write_outcome is not None: state['processes'].append({'role': role, **process_identity(pid)})
state['write_outcome'] = write_outcome state['phase'] = phase
write_session_record(path, state) if write_outcome is not None:
return True state['write_outcome'] = write_outcome
except Exception: write_session_record(path, state)
if required: return True
raise except Exception:
# An unavailable journal cannot interrupt an already executing COM call. if required:
logging.getLogger(__name__).warning('Worker session journal unavailable', exc_info=True) raise
return False # An unavailable journal cannot interrupt an already executing COM call.
logging.getLogger(__name__).warning('Worker session journal unavailable', exc_info=True)
return False
def process_identity(pid): def process_identity(pid):
@@ -135,11 +193,12 @@ def _session(path, root):
def _result(status, detail='', *, session=None, state=None, proof=None): def _result(status, detail='', *, session=None, state=None, proof=None):
return {'status': status, 'detail': detail, 'library': (session or {}).get('library'), return {'status': status, 'detail': detail, 'library': (session or {}).get('library'),
'block': (session or {}).get('block'),
'operation_id': (session or {}).get('operation_id'), 'operation_id': (session or {}).get('operation_id'),
'phase': (state or {}).get('phase', 'UNCONFIRMED'), 'release_proof': proof} 'phase': (state or {}).get('phase', 'UNCONFIRMED'), 'release_proof': proof}
def _reconcile(path, root, handles, stack): def _reconcile(path, root, handles, stack, *, release=True):
import pywintypes, win32api, win32con, win32event import pywintypes, win32api, win32con, win32event
from .residual_sessions import _handle_identity, _force_ownership from .residual_sessions import _handle_identity, _force_ownership
session, state, processes = _session(path, root) session, state, processes = _session(path, root)
@@ -176,6 +235,8 @@ def _reconcile(path, root, handles, stack):
active = True active = True
if active: if active:
return _result('ACTIVE', '原生调用尚未确认返回', session=session, state=state) return _result('ACTIVE', '原生调用尚未确认返回', session=session, state=state)
if not release:
return _result('UNCONFIRMED', '记录中的进程已退出,需确认库占用已释放', session=session, state=state)
from .library_lock import release_retained_library from .library_lock import release_retained_library
try: try:
proof = release_retained_library(session['library'], Path(path).parent/'release-proof', proof = release_retained_library(session['library'], Path(path).parent/'release-proof',
@@ -197,17 +258,28 @@ def reconcile_session_record(path, *, evidence_root):
return _result('UNCONFIRMED', str(error)) return _result('UNCONFIRMED', str(error))
def observe_retained_session(path, *, evidence_root, stop=None, progress=None, process=None): def inspect_session_record(path, *, evidence_root):
"""Observe process identities without touching the production library."""
with ExitStack() as stack:
try:
return _reconcile(Path(path), Path(evidence_root), {}, stack, release=False)
except Exception as error:
return _result('UNCONFIRMED', str(error))
def observe_retained_session(path, *, evidence_root, stop=None, progress=None, process=None,
observe_only=False):
stop = stop if stop is not None else threading.Event() stop = stop if stop is not None else threading.Event()
observation = Path(path).with_name('observation-' + uuid4().hex + '.json') observation = Path(path).with_name('observation-' + uuid4().hex + '.json')
with ExitStack() as stack: with ExitStack() as stack:
handles = {} handles = {}
while True: while True:
try: try:
result = _reconcile(Path(path), Path(evidence_root), handles, stack) result = _reconcile(Path(path), Path(evidence_root), handles, stack, release=not observe_only)
except Exception as error: except Exception as error:
result = _result('UNCONFIRMED', str(error)) result = _result('UNCONFIRMED', str(error))
if process is not None and process.poll() is not None and getattr(process, 'cbb_library', None): if (not observe_only and process is not None and process.poll() is not None
and getattr(process, 'cbb_library', None)):
try: try:
from .library_lock import release_retained_library 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')
@@ -216,7 +288,7 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
session={'library': str(process.cbb_library)}, proof=proof) session={'library': str(process.cbb_library)}, proof=proof)
except Exception: except Exception:
pass pass
if result['status'] == 'IDENTITY_CHANGED': if result['status'] == 'IDENTITY_CHANGED' and not observe_only:
try: try:
from .library_lock import release_retained_library 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')
@@ -233,7 +305,8 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
progress(result) progress(result)
except Exception: except Exception:
logging.getLogger(__name__).warning('Observation callback failed', exc_info=True) logging.getLogger(__name__).warning('Observation callback failed', exc_info=True)
if result['status'] == 'RELEASED' or stop.is_set(): if (result['status'] == 'RELEASED' or stop.is_set()
or observe_only and result['status'] != 'ACTIVE'):
return result return result
if not handles and process is not None and process.poll() is None: if not handles and process is not None and process.poll() is None:
stop.wait(1) stop.wait(1)
@@ -243,24 +316,38 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
stop.wait(5 if result['status'] in ('UNCONFIRMED', 'IDENTITY_CHANGED') else 1) stop.wait(5 if result['status'] in ('UNCONFIRMED', 'IDENTITY_CHANGED') else 1)
def handoff_session(path, *, evidence_root, process=None, progress=None, stop=None): 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.""" """Retain exact handles in memory until the observer independently releases."""
key = str(Path(path).absolute()).casefold() key = str(Path(path).absolute()).casefold()
try: try:
session = read_session_record(path, evidence_root=evidence_root) session = read_session_record(path, evidence_root=evidence_root)
library = session['library'] library = session['library']
except (OSError, ValueError, KeyError): except (OSError, ValueError, KeyError):
session = {}
library = getattr(process, 'cbb_library', None) library = getattr(process, 'cbb_library', None)
aliases = {_library_key(library), _library_key(Path(library).resolve())} if library else set() aliases = {_library_key(library), _library_key(Path(library).resolve())} if library else set()
with _observer_guard: with _observer_guard:
if key in _observers: if key in _observers:
return return
_retained_libraries[key] = aliases _retained_libraries[key] = aliases
_retained_results[key] = {'status': 'UNCONFIRMED', 'phase': 'UNCONFIRMED',
'library': str(library) if library else None, 'block': session.get('block'),
'detail': '正在核对残留会话', 'operation_id': session.get('operation_id')}
def notify(value):
with _observer_guard:
if _retained_libraries.get(key) is aliases:
previous = _retained_results[key]
_retained_results[key] = {**previous, **value,
'library': value.get('library') or previous['library'],
'operation_id': value.get('operation_id') or previous['operation_id'],
'block': value.get('block') or previous['block']}
if progress is not None:
progress(value)
def observe(): def observe():
result = None result = None
try: try:
result = observe_retained_session(path, evidence_root=evidence_root, progress=progress, result = observe_retained_session(path, evidence_root=evidence_root, progress=notify,
stop=stop, process=process) stop=stop, process=process, observe_only=observe_only)
if process is not None and process.poll() is not None: if process is not None and process.poll() is not None:
process.close() process.close()
return result return result
@@ -269,6 +356,7 @@ def handoff_session(path, *, evidence_root, process=None, progress=None, stop=No
_observers.pop(key, None) _observers.pop(key, None)
if result and result['status'] == 'RELEASED': if result and result['status'] == 'RELEASED':
_retained_libraries.pop(key, None) _retained_libraries.pop(key, None)
_retained_results.pop(key, None)
thread = threading.Thread(target=observe, name='retained-native-session', daemon=True) thread = threading.Thread(target=observe, name='retained-native-session', daemon=True)
_observers[key] = (thread, process) _observers[key] = (thread, process)
thread.start() thread.start()
@@ -308,7 +396,9 @@ def discover_sessions(evidence_root, *, library=None):
continue continue
if library is not None and _library_key(value.get('library', '')) != _library_key(library): if library is not None and _library_key(value.get('library', '')) != _library_key(library):
continue continue
value = {**value, 'path': str(path), **reconcile_session_record(path, evidence_root=root)} observed = inspect_session_record(path, evidence_root=root)
value = {**value, **observed, 'library': value.get('library'),
'block': value.get('block'), 'operation_id': value['operation_id'], 'path': str(path)}
except (OSError, ValueError): except (OSError, ValueError):
continue continue
found.append(value) found.append(value)
+1
View File
@@ -457,6 +457,7 @@ class MainWindow(QWidget):
for session in sessions: for session in sessions:
if session['status'] == 'ACTIVE': if session['status'] == 'ACTIVE':
handoff_session(session['path'], evidence_root=root, handoff_session(session['path'], evidence_root=root,
observe_only=True,
progress=lambda value: notify({'stage': 'session_observation', **value})) progress=lambda value: notify({'stage': 'session_observation', **value}))
return unfinished, sessions return unfinished, sessions
worker = ServiceWorker(inspect) worker = ServiceWorker(inspect)
@@ -30,6 +30,9 @@ class ResidualReleaseDialog(QDialog):
self.message = QLabel('正在检查当前库关联的会话…') self.message = QLabel('正在检查当前库关联的会话…')
self.message.setWordWrap(True) self.message.setWordWrap(True)
layout.addWidget(self.message) layout.addWidget(self.message)
self.runtime_label = QLabel()
self.runtime_label.setWordWrap(True)
layout.addWidget(self.runtime_label)
self.table = QTableWidget(0, 6) self.table = QTableWidget(0, 6)
self.table.setHorizontalHeaderLabels(['工程', '程序', 'PID', '状态', '窗口位置', '程序路径']) self.table.setHorizontalHeaderLabels(['工程', '程序', 'PID', '状态', '窗口位置', '程序路径'])
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
@@ -155,6 +158,7 @@ class ResidualReleaseDialog(QDialog):
or any(r.get('release_status') != 'RELEASED' for r in result['sessions'])): or any(r.get('release_status') != 'RELEASED' for r in result['sessions'])):
return False return False
self.plan = None self.plan = None
self.runtime_label.clear()
self.released.emit(proof) self.released.emit(proof)
return True return True
@@ -191,10 +195,19 @@ class ResidualReleaseDialog(QDialog):
elif not plan['sessions']: elif not plan['sessions']:
text += '确认后将复查库占用状态。' text += '确认后将复查库占用状态。'
self.message.setText(text) self.message.setText(text)
states = {'ACTIVE': '进行中', 'UNCONFIRMED': '待核对', 'IDENTITY_CHANGED': '待核对'}
self.runtime_label.setText('\n'.join(
f'{states.get(row["status"], "待核对")} · {row.get("block") or "当前库"} · '
f'{row.get("detail") or "操作状态需重新核对"}' for row in plan.get('runtime_sessions', [])))
def inspect(): def inspect():
from cbb_refresh_verify.network_path import select_library_file from cbb_refresh_verify.network_path import select_library_file
from cbb_refresh_verify.session_record import discover_sessions, retained_session_states
library = self.library if self.library.suffix.casefold() == '.lmc' else select_library_file(self.library) library = self.library if self.library.suffix.casefold() == '.lmc' else select_library_file(self.library)
return inspect_residual_sessions(library) plan = inspect_residual_sessions(library)
states = {row['path']: row for row in retained_session_states(library)}
states.update({str(Path(row['path']).absolute()).casefold(): row for row in
discover_sessions(self.evidence_dir.parent, library=library)})
return {**plan, 'runtime_sessions': list(states.values())}
self._start(inspect, ready) self._start(inspect, ready)
def force_release(self): def force_release(self):
+3 -1
View File
@@ -57,7 +57,9 @@ def test_unsafe_result_stops_batch_even_when_reported_pass():
def test_cleanup_does_not_force_terminate_native_database_owner(monkeypatch): def test_cleanup_does_not_force_terminate_native_database_owner(monkeypatch):
killed = [] killed = []
monkeypatch.setattr(adapter, '_terminate_pid', lambda pid, **k: killed.append(pid)) import win32api
monkeypatch.setattr(adapter.subprocess, 'run', lambda *args, **kwargs: killed.append(args))
monkeypatch.setattr(win32api, 'TerminateProcess', lambda *args: killed.append(args))
monkeypatch.setattr(adapter, '_process_details', lambda name: monkeypatch.setattr(adapter, '_process_details', lambda name:
[(22, 'ExpeditionPCB.exe', 'pcb', 11)] if name == 'ExpeditionPCB.exe' else []) [(22, 'ExpeditionPCB.exe', 'pcb', 11)] if name == 'ExpeditionPCB.exe' else [])
events = [] events = []
+4 -4
View File
@@ -96,8 +96,8 @@ def test_cleanup_verify_processes_confirms_absent_worker_parent_and_lm(monkeypat
from cbb_refresh_verify import mentor_adapter from cbb_refresh_verify import mentor_adapter
terminated = [] terminated = []
monkeypatch.setattr(mentor_adapter, '_terminate_pid', monkeypatch.setattr(mentor_adapter.subprocess, 'run',
lambda pid, **_kwargs: terminated.append(int(pid))) lambda *args, **kwargs: terminated.append(args))
monkeypatch.setattr(mentor_adapter, '_process_details', lambda _name: ()) monkeypatch.setattr(mentor_adapter, '_process_details', lambda _name: ())
result = cleanup_verify_processes(101, 202, 303, emit=lambda **event: None) result = cleanup_verify_processes(101, 202, 303, emit=lambda **event: None)
assert terminated == [] assert terminated == []
@@ -120,8 +120,8 @@ def test_cleanup_verify_processes_discovers_unbound_target_pcb_processes(monkeyp
(999, r'C:\Mentor\wg\ExpeditionPCB.exe', command, 0), (999, r'C:\Mentor\wg\ExpeditionPCB.exe', command, 0),
], ],
) )
monkeypatch.setattr(mentor_adapter, '_terminate_pid', monkeypatch.setattr(mentor_adapter.subprocess, 'run',
lambda pid, **_kwargs: terminated.append(int(pid))) lambda *args, **kwargs: terminated.append(args))
result = mentor_adapter.cleanup_verify_processes( result = mentor_adapter.cleanup_verify_processes(
101, 101,
+2
View File
@@ -79,6 +79,8 @@ def test_real_management_worker_parent_loss_waits_for_native_boundary(tmp_path):
'from cbb_refresh_verify import native_management as n\n' 'from cbb_refresh_verify import native_management as n\n'
'n._current_desktop=lambda:(None,"CbbManagement_test")\n' 'n._current_desktop=lambda:(None,"CbbManagement_test")\n'
'def native_call(job,root):\n' 'def native_call(job,root):\n'
' from cbb_refresh_verify.session_record import update_worker_state\n'
' update_worker_state(job,"NATIVE_CALL")\n'
f' Path({str(started)!r}).touch()\n' f' Path({str(started)!r}).touch()\n'
' time.sleep(1.5)\n' ' time.sleep(1.5)\n'
' try: n._require_management_continuation(job,root)\n' ' try: n._require_management_continuation(job,root)\n'
+2 -1
View File
@@ -56,7 +56,8 @@ def test_confirm_releases_full_plan_and_only_emits_actual_library_proof(qtbot, t
qtbot.waitUntil(lambda: not dialog.busy) qtbot.waitUntil(lambda: not dialog.busy)
qtbot.mouseClick(dialog.confirm_button, Qt.LeftButton) qtbot.mouseClick(dialog.confirm_button, Qt.LeftButton)
qtbot.waitUntil(lambda: not dialog.busy) qtbot.waitUntil(lambda: not dialog.busy)
assert calls == [plan] and released == [proof] assert len(calls) == 1 and calls[0]['sessions'] == plan['sessions']
assert calls[0]['library'] == plan['library'] and released == [proof]
assert '已释放 2' in dialog.message.text() assert '已释放 2' in dialog.message.text()
assert not dialog.confirm_button.isEnabled() assert not dialog.confirm_button.isEnabled()
+206
View File
@@ -0,0 +1,206 @@
"""Review 319 regressions; native databases are replaced by isolated files."""
from contextlib import contextmanager
from pathlib import Path
from threading import Event
import json
import subprocess
import sys
import time
import pytest
from cbb_refresh_verify import session_record as sessions
from test_session_record import child, record
def retain_unconfirmed(root, library):
root.mkdir(parents=True)
path = root/'session.json'
sessions.write_session_record(path, dict(operation_id=root.name, library=str(library), processes=[]))
done = Event()
sessions.handoff_session(path, evidence_root=root, progress=lambda _: done.set())
assert done.wait(3)
with sessions._observer_guard:
observer = sessions._observers.get(str(path.absolute()).casefold())
if observer:
observer[0].join(3)
assert not observer[0].is_alive()
assert not sessions.library_available(library)
return path
def test_explicit_proof_resumes_queue_after_unconfirmed_observer_ended(tmp_path, monkeypatch):
from cbb_refresh_verify import library_lock as locks
from cbb_refresh_verify.operation_queue import NativeQueue
monkeypatch.setattr(sessions, '_retained_libraries', {})
library = tmp_path/'library/lib.lmc'
library.parent.mkdir(); library.touch()
other = tmp_path/'other.lmc'
retain_unconfirmed(tmp_path/'old', library)
retain_unconfirmed(tmp_path/'independent', other)
@contextmanager
def proof(*args, **kwargs):
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
monkeypatch.setattr(locks, '_prove_library_released', proof)
result = locks.release_retained_library(library, tmp_path/'proof')
assert result['status'] == 'RELEASE_CONFIRMED'
assert sessions.library_available(library), 'successful explicit proof left the queue blocked'
assert sessions.discover_sessions(tmp_path/'old') == [], 'released history reappeared as unconfirmed'
assert not sessions.library_available(other)
assert NativeQueue().run(lambda: 'resumed', ready=lambda: sessions.library_available(library)) == 'resumed'
def test_failed_proof_and_newer_handoff_cannot_clear_queue_retention(tmp_path, monkeypatch):
from cbb_refresh_verify import library_lock as locks
monkeypatch.setattr(sessions, '_retained_libraries', {})
library = tmp_path/'library/lib.lmc'
library.parent.mkdir(); library.touch()
old = retain_unconfirmed(tmp_path/'old', library)
def denied(*args, **kwargs):
raise OSError('external owner remains')
monkeypatch.setattr(locks, '_prove_library_released', denied)
with pytest.raises(OSError, match='external owner'):
locks.release_retained_library(library, tmp_path/'proof')
assert not sessions.library_available(library)
@contextmanager
def proof(*args, **kwargs):
retain_unconfirmed(tmp_path/'new', library)
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
monkeypatch.setattr(locks, '_prove_library_released', proof)
locks.release_retained_library(library, tmp_path/'proof')
assert not sessions.library_available(library)
assert str(old.absolute()).casefold() not in sessions._retained_libraries
def test_discovery_of_exited_session_never_reconciles_library(tmp_path, child, monkeypatch):
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
child.terminate(); child.wait(5)
def forbidden(*args, **kwargs):
pytest.fail('passive history scan attempted library release')
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library', forbidden)
found = sessions.discover_sessions(tmp_path)
assert len(found) == 1 and found[0]['path'] == str(path)
assert found[0]['status'] == 'UNCONFIRMED'
assert found[0]['release_proof'] is None
@pytest.mark.parametrize('phase', ['PRECALL', 'POSTCALL'])
@pytest.mark.parametrize('broken_evidence', [False, True])
def test_parent_loss_exits_idle_worker_even_when_stop_evidence_fails(tmp_path, phase, broken_evidence):
run_watchdog_case(tmp_path, phase, broken_evidence=broken_evidence, exits=True)
@pytest.mark.parametrize('phase', ['NATIVE_CALL', 'CLOSING', 'UNCONFIRMED'])
def test_parent_loss_preserves_pending_or_unknown_worker(tmp_path, phase):
run_watchdog_case(tmp_path, phase, exits=False)
def test_stale_postcall_journal_cannot_override_in_memory_active_call(tmp_path):
run_watchdog_case(tmp_path, 'NATIVE_CALL', stale=True, exits=False)
def test_missing_journal_preserves_worker_until_state_can_be_confirmed(tmp_path):
run_watchdog_case(tmp_path, 'POSTCALL', missing=True, exits=False)
def run_watchdog_case(root, phase, *, exits, broken_evidence=False, stale=False, missing=False):
supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])
script, ready, returned = root/'worker.py', root/'ready', root/'returned'
cancel = root/'missing/cancel' if broken_evidence else root/'cancel'
script.write_text(
'import time\nfrom pathlib import Path\n'
'from cbb_refresh_verify import session_record as s, desktop_worker as d\n'
f'job=dict(operation_id="watchdog-test", session_record_path={str(root/"session.json")!r})\n'
f's.update_worker_state(job, {"POSTCALL" if stale else phase!r})\n'
'original=s.write_session_record\n'
'def write(path,value):\n'
f' if {broken_evidence!r} and Path(path).name=="supervisor-lost.json": raise OSError("disk full")\n'
f' if {stale!r} and Path(path).name=="worker-state.json": raise OSError("stale journal")\n'
' return original(path,value)\n'
's.write_session_record=write\n'
+ (f's.update_worker_state(job,{phase!r})\n' if stale else '') +
(f'Path({str(root/"worker-state.json")!r}).unlink()\n' if missing else '') +
f'd.start_supervisor_watchdog({supervisor.pid},cancel_path={str(cancel)!r},'
'session_record_path=job["session_record_path"],operation_id=job["operation_id"])\n'
f'Path({str(ready)!r}).touch()\n'
f'while not Path({str(returned)!r}).exists(): time.sleep(.02)\n'
's.write_session_record=original\n'
's.update_worker_state(job,"POSTCALL")\n'
'time.sleep(30)\n', encoding='utf-8')
log = (root/'worker.log').open('wb')
worker = subprocess.Popen([sys.executable, str(script)], stdout=log, stderr=log)
try:
deadline = time.monotonic()+6
while not ready.exists() and worker.poll() is None and time.monotonic()<deadline:
time.sleep(.02)
assert ready.exists()
supervisor.terminate(); supervisor.wait(5)
if exits:
assert worker.wait(4) == 75, 'idle orphan must exit without another application checkpoint'
else:
with pytest.raises(subprocess.TimeoutExpired):
worker.wait(.4)
returned.touch()
assert worker.wait(4) == 75, 'watchdog must recheck once the pending call returns'
finally:
for process in (worker, supervisor):
if process.poll() is None:
process.terminate()
process.wait(5)
log.close()
def test_residual_dialog_refresh_shows_runtime_blocker_without_releasing(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify import library_lock as locks
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
monkeypatch.setattr(sessions, '_retained_libraries', {})
library = tmp_path/'library/lib.lmc'
library.parent.mkdir(); library.touch()
monkeypatch.setattr(locks, 'inspect_residual_sessions', lambda *_: {'library': str(library), 'sessions': []})
monkeypatch.setattr(locks, 'release_retained_library', lambda *a, **kw: pytest.fail('preview mutated library'))
dialog = ResidualReleaseDialog(library, tmp_path/'release-checks')
qtbot.addWidget(dialog)
qtbot.waitUntil(lambda: not dialog.busy)
retain_unconfirmed(tmp_path/'late-session', library)
dialog.refresh_button.click()
qtbot.waitUntil(lambda: not dialog.busy)
assert dialog.plan['runtime_sessions'][0]['status'] == 'UNCONFIRMED'
assert '待核对' in dialog.runtime_label.text()
assert not sessions.library_available(library)
def test_passive_handoff_reports_final_unconfirmed_state_without_proof(tmp_path, child, monkeypatch):
from threading import Event
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
monkeypatch.setattr(sessions, '_retained_libraries', {})
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library',
lambda *a, **kw: pytest.fail('startup observer released the library'))
terminal = Event()
sessions.handoff_session(path, evidence_root=tmp_path, observe_only=True,
progress=lambda value: terminal.set() if value['status'] == 'UNCONFIRMED' else None)
child.terminate(); child.wait(5)
assert terminal.wait(3)
states = sessions.retained_session_states(tmp_path/'lib.lmc')
assert len(states) == 1 and states[0]['status'] == 'UNCONFIRMED'
assert states[0]['detail']
assert not sessions.library_available(tmp_path/'lib.lmc')
def test_explicit_release_marks_discovered_history_without_an_in_memory_observer(tmp_path, monkeypatch):
from cbb_refresh_verify import library_lock as locks, residual_sessions as residual
library = tmp_path/'library/lib.lmc'
library.parent.mkdir(); library.touch()
root = tmp_path/'history'; root.mkdir()
path = root/'session.json'
sessions.write_session_record(path, dict(operation_id='old-session', library=str(library), processes=[]))
found = sessions.discover_sessions(root)
assert len(found) == 1
monkeypatch.setattr(residual, 'query_process_snapshot', lambda: [])
@contextmanager
def proof(*args, **kwargs):
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
monkeypatch.setattr(locks, '_prove_library_released', proof)
outcome = residual.release_residual_sessions(dict(library=str(library), sessions=[], runtime_sessions=found),
tmp_path/'release')
assert outcome['status'] == 'RELEASE_CONFIRMED'
assert sessions.discover_sessions(root) == []