fix: acknowledge diagnostic prompt ownership before native close
This commit is contained in:
1 parent
de81bbf293
commit
4402486baa
5 files changed
+232
-16
No files matched your search
@@ -9,6 +9,7 @@ import ctypes
|
||||
from ctypes import wintypes
|
||||
import heapq
|
||||
import hashlib
|
||||
import json
|
||||
import ntpath
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -283,6 +284,32 @@ def _cancel(job):
|
||||
raise RuntimeError('VERIFY_CANCELLED: native opening check cancelled')
|
||||
|
||||
|
||||
def _request_close_handoff(job, pcb_pid, frame, session, emit, deadline):
|
||||
from uuid import uuid4
|
||||
intent = dict(pcb_pid=int(pcb_pid), hwnd=frame['hwnd'], operation_id=job['operation_id'],
|
||||
desktop=job['desktop'], project=job['project'], pcb=job['pcb'], close_id=uuid4().hex)
|
||||
ack = Path(job['root'])/'diagnostic-close-ack.json'
|
||||
emit(stage='diagnostic_close_requested', **intent)
|
||||
def read_ack():
|
||||
try:
|
||||
with ack.open(encoding='utf-8') as stream:
|
||||
return json.loads(stream.read(8192))
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
while True:
|
||||
_cancel(job)
|
||||
remaining = deadline-time.monotonic()
|
||||
if remaining <= 0 or not session.pcb_alive():
|
||||
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: supervisor handoff not acknowledged')
|
||||
try:
|
||||
acknowledged = _bounded_call(read_ack, min(2, remaining))
|
||||
except TimeoutError as error:
|
||||
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: supervisor acknowledgement read timed out') from error
|
||||
if acknowledged == intent:
|
||||
return intent
|
||||
time.sleep(min(.1, max(0, deadline-time.monotonic())))
|
||||
|
||||
|
||||
def _owned_by_frame(window, frame):
|
||||
if frame is None or window['pid'] != frame['pid']: return False
|
||||
if window.get('owner') == frame['hwnd']: return True
|
||||
@@ -388,17 +415,22 @@ def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=No
|
||||
if time.monotonic() >= deadline:
|
||||
raise RuntimeError('DIAGNOSTIC_OPEN_UNCONFIRMED: current target MDI/path/PID logs missing')
|
||||
time.sleep(.1)
|
||||
# Re-read the exact PID/frame before the sole close request.
|
||||
_cancel(job)
|
||||
windows = scan(deadline)
|
||||
_pending_prompt(windows, frame=frame, pcb_name=Path(job['pcb']).name)
|
||||
current = _target_frame(windows, Path(job['pcb']).stem)
|
||||
if current is None or current['hwnd'] != frame['hwnd']:
|
||||
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: target frame changed')
|
||||
session.post(frame['hwnd'], 0x10)
|
||||
emit(stage='diagnostic_wm_close', pcb_pid=int(pcb_pid), hwnd=frame['hwnd'],
|
||||
operation_id=job.get('operation_id'), desktop=job.get('desktop'), project=job['project'], pcb=job['pcb'])
|
||||
# Finish any in-flight supervisor poll before WM_CLOSE can create a prompt.
|
||||
close_deadline = time.monotonic() + 30
|
||||
intent = _request_close_handoff(job, pcb_pid, frame, session, emit, close_deadline)
|
||||
# Re-read the exact PID/frame after acknowledgement and before the sole close.
|
||||
_cancel(job)
|
||||
error = _read_bounded(error_reader) if error_reader else None
|
||||
if error: raise RuntimeError(str(error))
|
||||
windows = scan(close_deadline)
|
||||
pending = _pending_prompt(windows, frame=frame, pcb_name=Path(job['pcb']).name)
|
||||
current = _target_frame(windows, Path(job['pcb']).stem)
|
||||
if (pending or time.monotonic() >= close_deadline or current is None
|
||||
or any(current[key] != frame[key] for key in ('hwnd', 'pid', 'class', 'text'))):
|
||||
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: target frame changed')
|
||||
_cancel(job)
|
||||
session.post(frame['hwnd'], 0x10)
|
||||
emit(stage='diagnostic_wm_close', **intent)
|
||||
clicked = set()
|
||||
last_pending = ''
|
||||
while session.pcb_alive() or session.launcher_alive():
|
||||
|
||||
@@ -413,11 +413,39 @@ def phase_for_event(event, current=None):
|
||||
|
||||
|
||||
def _diagnostic_close_event(job, event, target_pid):
|
||||
return (job.get('diagnostic_only') is True and event.get('stage') == 'diagnostic_wm_close'
|
||||
close_id = event.get('close_id')
|
||||
return (job.get('diagnostic_only') is True and event.get('stage') == 'diagnostic_close_requested'
|
||||
and target_pid is not None and event.get('pcb_pid') == target_pid
|
||||
and type(event.get('hwnd')) is int and event['hwnd'] > 0
|
||||
and isinstance(close_id, str) and len(close_id) == 32 and all(c in '0123456789abcdef' for c in close_id)
|
||||
and bool(job.get('operation_id')) and event.get('operation_id') == job['operation_id']
|
||||
and all(event.get(key) == job.get(key) for key in ('project', 'pcb', 'desktop')))
|
||||
and all(job.get(key) and event.get(key) == job[key] for key in ('project', 'pcb', 'desktop')))
|
||||
|
||||
|
||||
def _acknowledge_diagnostic_close(job, event, stop):
|
||||
from .operation_queue import check_active
|
||||
check_active()
|
||||
if stop is not None and stop.is_set() or Path(job['cancel_signal']).exists():
|
||||
raise InterruptedError('VERIFY_CANCELLED: diagnostic handoff cancelled')
|
||||
ack = Path(job['root'])/'diagnostic-close-ack.json'
|
||||
pending = ack.with_suffix('.pending')
|
||||
pending.write_text(json.dumps({key: event[key] for key in
|
||||
('pcb_pid', 'hwnd', 'operation_id', 'desktop', 'project', 'pcb', 'close_id')}), encoding='utf-8')
|
||||
check_active()
|
||||
if stop is not None and stop.is_set() or Path(job['cancel_signal']).exists():
|
||||
raise InterruptedError('VERIFY_CANCELLED: diagnostic handoff cancelled')
|
||||
pending.replace(ack)
|
||||
|
||||
|
||||
def _close_intent_after_poll(job, events, offset, target_pid):
|
||||
if not job.get('diagnostic_only') or not events.exists(): return None
|
||||
with events.open(encoding='utf-8') as stream:
|
||||
stream.seek(offset)
|
||||
for line in stream:
|
||||
try: event = json.loads(line)
|
||||
except json.JSONDecodeError: break
|
||||
if _diagnostic_close_event(job, event, target_pid): return event
|
||||
return None
|
||||
|
||||
|
||||
def classify_verify_dispatch(*, verify_invoked, invoke_returned, target_pid, observation):
|
||||
@@ -1055,6 +1083,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
break
|
||||
timing.record(event)
|
||||
if _diagnostic_close_event(job, event, target_pid):
|
||||
_acknowledge_diagnostic_close(job, event, stop)
|
||||
diagnostic_closing = True
|
||||
if event.get('pcb_pid'): target_pid = event['pcb_pid']
|
||||
if event.get('launcher_pid'): launcher_pid = event['launcher_pid']
|
||||
@@ -1116,6 +1145,13 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
dialog_result = dialog_supervisor.poll()
|
||||
else:
|
||||
dialog_result = dialog_supervisor.poll()
|
||||
if not diagnostic_closing:
|
||||
intent = _close_intent_after_poll(job, events, offset, target_pid)
|
||||
if intent is not None:
|
||||
_acknowledge_diagnostic_close(job, intent, stop)
|
||||
diagnostic_closing = True
|
||||
from .dialog_supervisor import DialogPollResult
|
||||
dialog_result = DialogPollResult(activity=False)
|
||||
if (version_prompt is not None and consent is None and dialog_agent is not None
|
||||
and not request.diagnostic_only and not request.native_lm_verify
|
||||
and dialog_result.error_code == 'DATABASE_RECOVERY_REQUIRED'
|
||||
|
||||
Reference in new issue
Block a user