fix: hand off late cancelled startup cleanup once
This commit is contained in:
1 parent
906cfc06be
commit
5e9509e573
5 files changed
+256
-7
No files matched your search
@@ -651,7 +651,11 @@ def _cancelled_diagnostic_record(row):
|
||||
if baseline is None or _load_evidence(job,row['pid'],baseline,require_autosave=False) is None:
|
||||
continue
|
||||
matches.append(dict(job=job, record_path=str(path), operation_id=operation,
|
||||
closed_frame=closes[0]['hwnd'] if closes else None))
|
||||
closed_frame=closes[0]['hwnd'] if closes else None,
|
||||
declined_buttons=[e['hwnd'] for e in events if e.get('stage')=='startup_prompt_no'
|
||||
and e.get('pcb_pid')==row['pid'] and e.get('control_id')==7 and e.get('hwnd')],
|
||||
dismissed_tips=[e['hwnd'] for e in events if e.get('stage')=='startup_tip_dismissed'
|
||||
and e.get('pcb_pid')==row['pid'] and e.get('hwnd')]))
|
||||
except (OSError, pywintypes.error, ValueError, KeyError, TypeError):
|
||||
continue
|
||||
return matches[0] if len(matches) == 1 else None
|
||||
@@ -680,12 +684,12 @@ def _legacy_diagnostic_windows(row):
|
||||
return result
|
||||
|
||||
|
||||
def _close_registered_startup(row, handle, proof, *, stop=None):
|
||||
def _close_registered_startup(row, handle, proof, *, stop=None, deadline=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()+30 # One proved cancelled-startup intent; generic close still uses 3 seconds.
|
||||
if deadline is None:deadline = time.monotonic()+30
|
||||
def active_owner():
|
||||
check_active()
|
||||
if stop is not None and stop.is_set():
|
||||
@@ -704,7 +708,13 @@ def _close_registered_startup(row, handle, proof, *, stop=None):
|
||||
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']; dismissed_tips = set()
|
||||
state = {'clicked':set()}; closed = proof['closed_frame']; dismissed_tips = set(proof.get('dismissed_tips',()))
|
||||
def read_bound_windows():
|
||||
windows=_legacy_diagnostic_windows(bound)
|
||||
for window in windows:
|
||||
if any(c['hwnd'] in proof.get('declined_buttons',()) for c in window['children']):
|
||||
state['clicked'].add(window['hwnd'])
|
||||
return windows
|
||||
try:
|
||||
while time.monotonic() < deadline:
|
||||
if win32event.WaitForSingleObject(handle, 0) == win32con.WAIT_OBJECT_0:
|
||||
@@ -732,7 +742,7 @@ def _close_registered_startup(row, handle, proof, *, stop=None):
|
||||
break # Never stack another Close after a legacy Tip acknowledgement.
|
||||
try:
|
||||
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),
|
||||
post=post,emit=lambda **event:None,read_windows=read_bound_windows,
|
||||
identity_check=confirmed)
|
||||
except RuntimeError as error:
|
||||
if not str(error).startswith('STARTUP_WINDOW_CHANGED:'):
|
||||
@@ -745,6 +755,69 @@ def _close_registered_startup(row, handle, proof, *, stop=None):
|
||||
return {'release_status':'WAITING','detail':'已请求取消启动清理,等待原生退出后重新检查'}
|
||||
|
||||
|
||||
def _ordinary_startup_close(row, handle, proof, *, stop=None):
|
||||
"""One persisted retained phase, serialized with existing explicit release."""
|
||||
import win32con, win32event
|
||||
from .session_record import read_session_record, write_session_record
|
||||
from .operation_queue import native_phase
|
||||
waiting = {'release_status':'WAITING','detail':'取消启动清理已交接或原生执行通道忙,继续观察'}
|
||||
mutex=win32event.CreateMutex(None,False,r'Local\CbbRefreshVerify795')
|
||||
acquired=False
|
||||
try:
|
||||
acquired=win32event.WaitForSingleObject(mutex,0) in (win32con.WAIT_OBJECT_0,win32con.WAIT_ABANDONED)
|
||||
if not acquired or stop is not None and stop.is_set():return waiting
|
||||
path=Path(proof['record_path'])
|
||||
record=read_session_record(path,evidence_root=path.parent)
|
||||
if 'retained_startup_cleanup' in record or _cancelled_diagnostic_record(row)!=proof:return waiting
|
||||
# Complete loaded identity and every prompt are checked before consuming
|
||||
# the one-shot intent. Unknown/multiple documents remain observation-only.
|
||||
from .diagnostic_open import _target_frame, _pending_prompt
|
||||
windows=_legacy_diagnostic_windows(dict(row,cleanup_desktop=proof['job']['desktop']))
|
||||
frame=_target_frame(windows,Path(proof['job']['pcb']).stem,require_ready=False)
|
||||
if frame is None:return waiting
|
||||
for window in windows:
|
||||
_pending_prompt([window],closing=proof['closed_frame'] is not None,frame=frame,
|
||||
pcb_name=Path(proof['job']['pcb']).name)
|
||||
if _cancelled_diagnostic_record(row)!=proof or stop is not None and stop.is_set():return waiting
|
||||
deadline=time.monotonic()+30
|
||||
record['retained_startup_cleanup']=dict(operation_id=proof['operation_id'],
|
||||
pcb_pid=row['pid'],owner_pid=os.getpid(),state='CLAIMED',closed_frame=proof['closed_frame'],
|
||||
deadline_monotonic=deadline,identity={k:row[k] for k in ('created_handle','image','owner_sid','session_id')})
|
||||
write_session_record(path,record) # Durable before No/Close; never removed or retried.
|
||||
with native_phase(read_only=True):
|
||||
return _close_registered_startup(row,handle,proof,stop=stop,deadline=deadline)
|
||||
except (OSError,ValueError,RuntimeError):
|
||||
return waiting
|
||||
finally:
|
||||
if acquired:win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
|
||||
|
||||
def resume_cancelled_startup(path, *, stop=None):
|
||||
"""Observer-only entry for one late loaded, positively proved ordinary cancel."""
|
||||
from .session_record import _session
|
||||
from .failed_session import cancelled_prewrite_startup
|
||||
path=Path(path)
|
||||
try:
|
||||
session,state,identities=_session(path,path.parent)
|
||||
if 'retained_startup_cleanup' in session or stop is not None and stop.is_set():return False
|
||||
job=json.loads(path.with_name('request.json').read_text(encoding='utf-8'))
|
||||
if job.get('diagnostic_only') is not False:return False
|
||||
native=[p for p in identities if p.get('role')=='native'
|
||||
and Path(p['image']).name.casefold()=='expeditionpcb.exe']
|
||||
if len(native)!=1 or cancelled_prewrite_startup(job,native[0]['pid']) is None:return False
|
||||
plan=inspect_residual_sessions(session['library'],block_names=(session['block'],))
|
||||
rows=[r for r in plan['sessions'] if r['pid']==native[0]['pid']]
|
||||
if len(rows)!=1:return False
|
||||
row=dict(rows[0],session_records=[dict(path=str(path),operation_id=session['operation_id'])])
|
||||
proof=_cancelled_diagnostic_record(row)
|
||||
if proof is None:return False # Includes exact worker/helper exit and fresh loaded logs.
|
||||
_request_close(row,stop=stop)
|
||||
return True
|
||||
except (OSError,ValueError,KeyError,TypeError,RuntimeError):
|
||||
return False
|
||||
|
||||
|
||||
def _request_close(row, *, stop=None):
|
||||
import win32con, win32event
|
||||
with _process_handle(row['pid']) as handle:
|
||||
@@ -766,6 +839,8 @@ def _request_close(row, *, stop=None):
|
||||
return {'release_status':'WAITING','detail':'取消启动会话的用户或 Windows 会话未确认'}
|
||||
if stop is not None and stop.is_set():
|
||||
return {'release_status':'CANCELLED','detail':'已停止释放'}
|
||||
if proof['job'].get('diagnostic_only') is False:
|
||||
return _ordinary_startup_close(row,handle,proof,stop=stop)
|
||||
from .session_record import handoff_session
|
||||
record_path = Path(proof['record_path'])
|
||||
handoff_session(record_path, evidence_root=record_path.parent, observe_only=False)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Bounded, observation-only journal for native calls that outlive a task."""
|
||||
"""Bounded registration and observation for native calls that outlive a task."""
|
||||
from contextlib import ExitStack
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
@@ -407,6 +407,15 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p
|
||||
session={'library': str(process.cbb_library)}, proof=proof)
|
||||
except Exception:
|
||||
pass
|
||||
if result['status'] == 'ACTIVE' and not observation_only and not stop.is_set():
|
||||
try:
|
||||
from .residual_sessions import resume_cancelled_startup
|
||||
if resume_cancelled_startup(path, stop=stop):
|
||||
# Cleanup is not release proof. Re-read all held identities
|
||||
# and independently prove the resource release as usual.
|
||||
result = _reconcile(Path(path), Path(evidence_root), handles, stack)
|
||||
except Exception:
|
||||
logging.getLogger(__name__).warning('Retained startup cleanup unavailable', exc_info=True)
|
||||
if result['status'] == 'IDENTITY_CHANGED' and not observation_only:
|
||||
try:
|
||||
from .library_lock import release_retained_library
|
||||
|
||||
@@ -13,7 +13,7 @@ def ordinary_proof(job,root,baseline,*,worker=None,pid=12):
|
||||
library=job['lmc_file'],session_record_path=str(root/'session.json'),interactive_input=False)
|
||||
job.pop('diagnostic_log_baseline',None)
|
||||
session_record.write_session_record(root/'session.json',dict(operation_id=job['operation_id'],operation='verify',
|
||||
library=job['library'],desktop=job['desktop'],cancel_signal=job['cancel_signal'],processes=[dict(role='worker',**worker)],
|
||||
library=job['library'],block=job.get('block',job.get('block_name')),desktop=job['desktop'],cancel_signal=job['cancel_signal'],processes=[dict(role='worker',**worker)],
|
||||
resource_scope=dict(scope_state='DECLARED',library=job['library'],operation_id=job['operation_id'],projects=[job['project']])))
|
||||
binding={k:job[k] for k in ('operation_id','project','pcb','desktop')}
|
||||
events=[dict(stage='startup_supervisor_no_write',**binding),
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify import diagnostic_open, residual_sessions, session_record
|
||||
from test_registered_startup_release import registered_fixture
|
||||
from test_cancelled_ordinary_startup import ordinary_proof
|
||||
|
||||
|
||||
def fixture(tmp_path, monkeypatch, *, obstacle='', closed=False):
|
||||
load=diagnostic_open._load_evidence
|
||||
job,row,records,messages,state=registered_fixture(tmp_path,monkeypatch,obstacle=obstacle,closed=closed)
|
||||
monkeypatch.setattr(diagnostic_open,'_load_evidence',load)
|
||||
events=ordinary_proof(job,tmp_path,state['prelaunch_baseline'])
|
||||
if closed:events.append(dict(stage='startup_close_requested',pcb_pid=12,hwnd=100))
|
||||
(tmp_path/'events.jsonl').write_text(''.join(json.dumps(e)+'\n' for e in events))
|
||||
(tmp_path/'request.json').write_text(json.dumps(job))
|
||||
monkeypatch.setattr(residual_sessions,'inspect_residual_sessions',lambda *a,**k:dict(sessions=[row]))
|
||||
return job,row,records,messages,state
|
||||
|
||||
|
||||
@pytest.mark.parametrize('closed',[False,True])
|
||||
def test_registered_observer_finishes_late_loaded_ordinary_startup_once(tmp_path,monkeypatch,closed):
|
||||
job,row,records,messages,state=fixture(tmp_path,monkeypatch,closed=closed)
|
||||
original=(tmp_path/'result.json').read_bytes()
|
||||
states=[]
|
||||
def reconcile(*a,**kw):
|
||||
states.append(1)
|
||||
return dict(status='ACTIVE' if records else 'RELEASED',operation_id=job['operation_id'],
|
||||
release_proof=None if records else dict(status='RELEASE_CONFIRMED'))
|
||||
monkeypatch.setattr(session_record,'_reconcile',reconcile)
|
||||
stop=threading.Event()
|
||||
def progress(value):
|
||||
if len(states)>2:stop.set()
|
||||
result=session_record.observe_retained_session(tmp_path/'session.json',evidence_root=tmp_path,stop=stop,progress=progress)
|
||||
assert result['status']=='RELEASED'
|
||||
assert messages==([(31,245)] if closed else [(31,245),(100,16)])
|
||||
assert (tmp_path/'result.json').read_bytes()==original
|
||||
claim=json.loads((tmp_path/'session.json').read_text())['retained_startup_cleanup']
|
||||
assert claim['operation_id']==job['operation_id'] and claim['pcb_pid']==12
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obstacle',['live_worker','other_write','save','foreign_identity','missing_cancel'])
|
||||
def test_automatic_observer_does_not_consume_intent_or_act_without_positive_proof(tmp_path,monkeypatch,obstacle):
|
||||
job,row,records,messages,state=fixture(tmp_path,monkeypatch,obstacle=obstacle)
|
||||
residual_sessions.resume_cancelled_startup(tmp_path/'session.json')
|
||||
assert not messages and records
|
||||
assert 'retained_startup_cleanup' not in json.loads((tmp_path/'session.json').read_text())
|
||||
|
||||
|
||||
def test_timed_out_retained_phase_is_not_restarted_by_observer_or_explicit_release(tmp_path,monkeypatch):
|
||||
job,row,records,messages,state=fixture(tmp_path,monkeypatch)
|
||||
read=residual_sessions._legacy_diagnostic_windows
|
||||
def progress(bound):
|
||||
values=read(bound)
|
||||
if state['declined']:values[0]['children'].append(dict(hwnd=123,**{'class':'msctls_progress32'},visible=True))
|
||||
return values
|
||||
monkeypatch.setattr(residual_sessions,'_legacy_diagnostic_windows',progress)
|
||||
residual_sessions.resume_cancelled_startup(tmp_path/'session.json')
|
||||
first=state['ticks']
|
||||
assert 30<=first<30.2 and messages==[(31,245)]
|
||||
residual_sessions.resume_cancelled_startup(tmp_path/'session.json')
|
||||
assert residual_sessions._request_close(row)['release_status']=='WAITING'
|
||||
assert state['ticks']==first and messages==[(31,245)] and records
|
||||
|
||||
|
||||
def test_claim_is_persisted_before_any_native_action_and_failure_prevents_action(tmp_path,monkeypatch):
|
||||
job,row,records,messages,state=fixture(tmp_path,monkeypatch)
|
||||
def denied(*a,**kw):raise OSError('journal unavailable')
|
||||
monkeypatch.setattr(session_record,'write_session_record',denied)
|
||||
residual_sessions.resume_cancelled_startup(tmp_path/'session.json')
|
||||
assert not messages and records
|
||||
|
||||
|
||||
def test_retained_phase_never_repeats_worker_no_on_still_visible_prompt(tmp_path,monkeypatch):
|
||||
job,row,records,messages,state=fixture(tmp_path,monkeypatch)
|
||||
with (tmp_path/'events.jsonl').open('a') as stream:
|
||||
stream.write(json.dumps(dict(stage='startup_prompt_no',pcb_pid=12,hwnd=31,control_id=7))+'\n')
|
||||
residual_sessions.resume_cancelled_startup(tmp_path/'session.json')
|
||||
assert not messages and records and 30<=state['ticks']<30.2
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obstacle',['unknown','hidden_document','revoked'])
|
||||
def test_late_observer_preserves_unknown_or_changed_loaded_state_before_claim(tmp_path,monkeypatch,obstacle):
|
||||
job,row,records,messages,state=fixture(tmp_path,monkeypatch)
|
||||
read=residual_sessions._legacy_diagnostic_windows
|
||||
def windows(bound):
|
||||
values=read(bound)
|
||||
if obstacle=='unknown':values.append(dict(hwnd=999,pid=12,**{'class':'#32770'},text='Unknown',children=[]))
|
||||
if obstacle=='hidden_document':values[0]['children'].append(dict(hwnd=888,**{'class':'Afx:document'},text='Other',parent=101,visible=False))
|
||||
if obstacle=='revoked':
|
||||
with (tmp_path/'events.jsonl').open('a') as stream:stream.write('{"stage":"startup_write_admitted"}\n')
|
||||
return values
|
||||
monkeypatch.setattr(residual_sessions,'_legacy_diagnostic_windows',windows)
|
||||
residual_sessions.resume_cancelled_startup(tmp_path/'session.json')
|
||||
assert not messages and records
|
||||
assert 'retained_startup_cleanup' not in json.loads((tmp_path/'session.json').read_text())
|
||||
|
||||
|
||||
def test_duplicate_observer_explicit_release_and_process_restart_share_persisted_claim(tmp_path):
|
||||
"""Real process/mutex/file race; only native evidence and action boundary are stubs."""
|
||||
import os,subprocess,sys,time
|
||||
path=tmp_path/'session.json'
|
||||
session_record.write_session_record(path,dict(operation_id='one',block='B',library=str(tmp_path/'lib.lmc')))
|
||||
(tmp_path/'request.json').write_text(json.dumps(dict(diagnostic_only=False)))
|
||||
script=tmp_path/'race.py'
|
||||
script.write_text('''
|
||||
import json,sys,time
|
||||
from pathlib import Path
|
||||
from contextlib import nullcontext
|
||||
import win32api
|
||||
from cbb_refresh_verify import session_record as s,residual_sessions as r,diagnostic_open as d,failed_session as f
|
||||
root=Path(sys.argv[1]);mode=sys.argv[2];tag=sys.argv[3];path=root/'session.json'
|
||||
job=dict(diagnostic_only=False,desktop='CbbVerify_test',pcb=str(root/'B.pcb'))
|
||||
row=dict(pid=1,name='ExpeditionPCB.exe',created_handle='bound',image='ExpeditionPCB.exe',owner_sid='owner',session_id=1)
|
||||
proof=dict(job=job,record_path=str(path),operation_id='one',closed_frame=None)
|
||||
r._process_handle=lambda *a,**k:nullcontext(win32api.GetCurrentProcess())
|
||||
r._handle_identity=lambda h:dict(created_handle='bound',image='ExpeditionPCB.exe')
|
||||
r._force_ownership=lambda *a:dict(force_user_confirmed=True,owner_sid='owner',session_id=1)
|
||||
r._process_windows=lambda *a:{}
|
||||
r._cancelled_diagnostic_record=lambda *a:proof
|
||||
r._legacy_diagnostic_windows=lambda *a:[dict(hwnd=100,pid=1)]
|
||||
d._target_frame=lambda *a,**k:dict(hwnd=100,pid=1)
|
||||
d._pending_prompt=lambda *a,**k:False
|
||||
f.cancelled_prewrite_startup=lambda *a:{}
|
||||
r.inspect_residual_sessions=lambda *a,**k:dict(sessions=[row])
|
||||
s._session=lambda *a:(s.read_session_record(path,evidence_root=root),{},[dict(role='native',pid=1,image='ExpeditionPCB.exe')])
|
||||
def close(*a,**k):
|
||||
assert 'retained_startup_cleanup' in json.loads(path.read_text())
|
||||
(root/('action-'+tag)).write_text('one native phase')
|
||||
until=time.monotonic()+8
|
||||
while not (root/'release').exists() and time.monotonic()<until:time.sleep(.01)
|
||||
return dict(release_status='WAITING')
|
||||
r._close_registered_startup=close
|
||||
(root/('ready-'+tag)).touch()
|
||||
until=time.monotonic()+8
|
||||
while not (root/'go').exists() and time.monotonic()<until:time.sleep(.01)
|
||||
result=r.resume_cancelled_startup(path) if mode=='observer' else r._request_close(row)
|
||||
(root/('result-'+tag)).write_text(json.dumps(result))
|
||||
''',encoding='utf-8')
|
||||
env=dict(os.environ,PYTHONPATH=str(Path(session_record.__file__).parents[1]))
|
||||
children=[]
|
||||
def start(mode,tag):
|
||||
child=subprocess.Popen([sys.executable,str(script),str(tmp_path),mode,tag],env=env)
|
||||
children.append(child);return child
|
||||
try:
|
||||
start('observer','a');start('explicit','b')
|
||||
end=time.monotonic()+5
|
||||
while len(list(tmp_path.glob('ready-*')))<2 and time.monotonic()<end:time.sleep(.01)
|
||||
assert len(list(tmp_path.glob('ready-*')))==2
|
||||
(tmp_path/'go').touch()
|
||||
end=time.monotonic()+5
|
||||
while not list(tmp_path.glob('action-*')) and time.monotonic()<end:time.sleep(.01)
|
||||
assert len(list(tmp_path.glob('action-*')))==1
|
||||
(tmp_path/'release').touch()
|
||||
for child in children:assert child.wait(5)==0
|
||||
assert start('observer','restart').wait(5)==0
|
||||
assert len(list(tmp_path.glob('action-*')))==1
|
||||
finally:
|
||||
(tmp_path/'go').touch();(tmp_path/'release').touch()
|
||||
for child in children:child.wait(10)
|
||||
@@ -10,6 +10,7 @@ from test_cancelled_loaded_startup import loaded_fixture
|
||||
|
||||
def registered_fixture(tmp_path,monkeypatch,*,closed=False,obstacle=''):
|
||||
import win32con,win32event
|
||||
kernel_wait=win32event.WaitForSingleObject
|
||||
job,records,messages,state=loaded_fixture(tmp_path,monkeypatch)
|
||||
state.update(tip=False,closing=closed)
|
||||
state['observer_requests']=[]
|
||||
@@ -52,6 +53,7 @@ def registered_fixture(tmp_path,monkeypatch,*,closed=False,obstacle=''):
|
||||
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):
|
||||
if handle not in (11,12):return kernel_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)
|
||||
|
||||
Reference in new issue
Block a user