fix: dismiss a late load-failure prompt after prewrite cancellation

This commit is contained in:
阿白叔 committed 2026-11-23 19:13:08 +08:00
1 parent 3223889dc7
commit 48f38d1bdd
2 files changed
+95 -10

No files matched your search

+57 -10
View File
@@ -612,7 +612,7 @@ def _cancelled_diagnostic_record(row):
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 (result.get('write_outcome') or {}).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']]
@@ -911,6 +911,7 @@ def finish_owned_idle_session(path, *, stop=None):
return False
elif not isinstance(result, dict) or result.get('safe_to_continue') is not False:
return False
cancelled_before_write = None
if operation == 'verify':
if result is None:
return False
@@ -918,6 +919,10 @@ def finish_owned_idle_session(path, *, stop=None):
failed_open = (result.get('error_stage') == 'PCB_STARTING'
and result.get('error_category') in ('MENTOR_DATABASE_LOAD_FAILED','ICDB_REPAIR_REQUIRED')
and not path.with_name('save.log').exists())
if (result.get('status') == 'CANCELLED' and result.get('error_stage') == 'PCB_STARTING'
and state.get('phase') == 'UNCONFIRMED'):
from .failed_session import cancelled_prewrite_startup
cancelled_before_write = cancelled_prewrite_startup(job)
saved_closed = (isinstance(facts, dict) and facts.get('interrupted') is False
and all(facts.get(k) == v for k,v in (
('save_state','COMMITTED'),('close_state','CLOSED'),('helper_state','EXITED'))))
@@ -926,7 +931,8 @@ def finish_owned_idle_session(path, *, stop=None):
or result.get('block_name') != job['block']
or Path(result.get('evidence_path','')).resolve() != path.with_name('result.json').resolve()
or Path(job.get('root','')).resolve() != path.parent.resolve()
or job.get('lmc_file') != job['library'] or not (failed_open or saved_closed)): return False
or job.get('lmc_file') != job['library']
or not (failed_open or saved_closed or cancelled_before_write is not None)): return False
client_name = 'expeditionpcb.exe'
elif operation == 'catalog' and job.get('worker_role') == 'catalog_readonly' and desktop.startswith('CbbManagement_'):
client_name = 'librarymanager.exe'
@@ -962,29 +968,70 @@ def finish_owned_idle_session(path, *, stop=None):
for c in w.get('children',[])) for w in windows)):
return None
return close_target(client_name, windows, incomplete=_window_error(snapshot,client['pid']))[0]
def load_failure_button():
from .failed_session import _startup_snapshot, _cancel_target
snapshot = _process_windows({client['pid']}, include_hidden_documents=True)
windows = snapshot.get(client['pid'], [])
if (_window_error(snapshot, client['pid']) or not windows
or any(w.get('desktop') != desktop for w in windows)):
return None
modals = [w for w in windows if w['class'] == '#32770']
if len(modals) != 1:
return None
with _window_desktop(desktop):
observed = _startup_snapshot(modals[0]['hwnd'], client['pid'])
target = _cancel_target(observed)
if target is None or target[0] != 'database_load_log':
return None
return observed, target[1].handle
if not same_client(): return False
if (cancelled_before_write is not None
and cancelled_prewrite_startup(job, client['pid']) != cancelled_before_write):
return False
target = empty_target()
if target is None or stop is not None and stop.is_set(): return False
failure = load_failure_button() if target is None and cancelled_before_write is not None else None
if (target is None and failure is None) or stop is not None and stop.is_set(): return False
check_active()
current, _, _ = _session(path, path.parent)
if current != session: return False
claim = dict(operation_id=session['operation_id'],pid=client['pid'],identity=client,
status='REQUESTED',hwnd=target['hwnd'],desktop=desktop)
status='REQUESTED',hwnd=target['hwnd'] if target is not None else failure[1],
desktop=desktop)
write_session_record(path, {**session,'owned_idle_cleanup':claim})
# Re-read after durable I/O; never act on a window that gained a document.
latest, latest_state, latest_identities = _session(path, path.parent)
if (latest_state != state or latest_identities != identities
or latest.get('owned_idle_cleanup') != claim
or any(latest.get(k) != v for k,v in session.items() if k != 'recorded_at')
or not same_client() or empty_target() != target or read('request.json') != job
or not same_client() or read('request.json') != job
or (empty_target() != target if failure is None else load_failure_button() != failure)
or (cancelled_before_write is not None
and cancelled_prewrite_startup(job, client['pid']) != cancelled_before_write)
or read_result() != result or result is None and not orphaned_catalog_session(path)
or stop is not None and stop.is_set()): return False
with native_phase(read_only=True):
_post_window_message(target,target['hwnd'],client['pid'],win32con.WM_CLOSE)
deadline = time.monotonic()+3
while time.monotonic() < deadline:
if win32event.WaitForSingleObject(handle,100) == win32con.WAIT_OBJECT_0: break
if stop is not None and stop.is_set(): break
if failure is not None:
_post_window_message({'desktop': desktop}, failure[1], client['pid'], win32con.BM_CLICK)
deadline = time.monotonic() + 5
while time.monotonic() < deadline and same_client():
if stop is not None and stop.is_set():
break
target = empty_target()
if target is not None:
break
time.sleep(.1)
if (target is not None and (not same_client() or empty_target() != target
or cancelled_prewrite_startup(job, client['pid']) != cancelled_before_write)):
target = None
if target is not None and (stop is None or not stop.is_set()):
_post_window_message(target, target['hwnd'], client['pid'], win32con.WM_CLOSE)
deadline = time.monotonic() + 3
while time.monotonic() < deadline:
if win32event.WaitForSingleObject(handle, 100) == win32con.WAIT_OBJECT_0:
break
if stop is not None and stop.is_set():
break
exited = win32event.WaitForSingleObject(handle,0) == win32con.WAIT_OBJECT_0
write_session_record(path.with_name('owned-idle-cleanup-result.json'),dict(
operation_id=session['operation_id'],library=session['library'],
+38
View File
@@ -139,3 +139,41 @@ def test_discovery_hides_only_completed_read_only_catalog(tmp_path, monkeypatch,
found = session_record.discover_sessions(tmp_path)
assert bool(found) == (not hidden)
@pytest.mark.parametrize('proved', [True, False])
def test_cancelled_prewrite_can_dismiss_exact_late_load_failure(qtbot, tmp_path, monkeypatch, proved):
import json
from contextlib import nullcontext
from types import SimpleNamespace
from test_owned_idle_cleanup import owned_fixture
from cbb_refresh_verify import failed_session, residual_sessions
root, job, result, windows, state = owned_fixture(tmp_path, monkeypatch)
result.update(status='CANCELLED', error_category='TASK_HANDOFF', write_outcome=None)
(root/'result.json').write_text(json.dumps(result), encoding='utf-8')
frame = windows[0]
frame['documents'] = [99]
windows.append(dict(hwnd=200, title='Expedition PCB', desktop=job['desktop'], children=[],
enabled=True, **{'class': '#32770'}))
baseline = {'started_ns': 123}
monkeypatch.setattr(failed_session, 'cancelled_prewrite_startup', lambda *a: baseline if proved else None)
monkeypatch.setattr(residual_sessions, '_window_desktop', lambda *a, **kw: nullcontext())
monkeypatch.setattr(failed_session, '_startup_snapshot', lambda *a: 'exact load failure')
monkeypatch.setattr(failed_session, '_cancel_target',
lambda _: ('database_load_log', SimpleNamespace(handle=201)))
def post(window, hwnd, pid, message):
state['posts'].append((hwnd, pid, message))
if hwnd == 201:
windows.pop()
frame['documents'].clear()
else:
state['native_alive'] = False
ticks = [0.]
monkeypatch.setattr(residual_sessions.time, 'monotonic', lambda: ticks[0])
monkeypatch.setattr(residual_sessions.time, 'sleep', lambda delay: ticks.__setitem__(0, ticks[0]+delay))
monkeypatch.setattr(residual_sessions, '_post_window_message', post)
original = (root/'result.json').read_bytes()
assert residual_sessions.finish_owned_idle_session(root/'session.json') is proved
assert state['posts'] == ([(201, 12, 245), (100, 12, 16)] if proved else [])
assert (root/'result.json').read_bytes() == original