Files
cbb-refresh-verify-795/tests/test_issue15_shutdown_handoff.py

284 lines
14 KiB
Python

"""Actual process shutdown and captured native ownership regressions."""
from pathlib import Path
from threading import Event
from types import SimpleNamespace
import json
import os
import subprocess
import sys
import time
import pytest
from test_issue15_ui_integration import window_for, load
def test_close_retains_captured_mapping_while_native_supervisor_is_active(qtbot,tmp_path,monkeypatch):
from cbb_refresh_verify.operation_queue import native_channel
from cbb_refresh_verify.network_path import LibraryPathLease
from test_network_path import FakeWNet
window=window_for(qtbot,tmp_path,monkeypatch)
load(window,qtbot)
backend=FakeWNet()
lease=LibraryPathLease.acquire(r'\\server\share\A\library.lmc',backend=backend)
window.library_lease=lease; window.mentor_lmc_path=lease.mentor_path
entered,release=Event(),Event()
def action():
with native_channel(): entered.set(); release.wait(5)
job=window._new_job('verify',library=lease.mentor_path)
window._start(action,lambda _:None,job=job)
qtbot.waitUntil(entered.is_set)
try:
window.close(); qtbot.wait(150)
assert not lease._closed, 'active native owner lost its captured mapping'
assert lease._retained_for_recovery
finally:
release.set(); qtbot.waitUntil(lambda:not window.isVisible())
lease.release_after_recovery({'status':'RELEASE_CONFIRMED','library':str(lease.mentor_path)})
lease.close()
def test_old_release_proof_cleans_its_lease_without_updating_new_operation(qtbot,tmp_path,monkeypatch):
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
monkeypatch.setattr(ResidualReleaseDialog,'refresh',lambda self:None)
cleaned=[]
monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases',
lambda proof,**kw:cleaned.append((proof,kw)))
window=window_for(qtbot,tmp_path,monkeypatch); load(window,qtbot)
first,second=Event(),Event()
window._start(lambda:first.wait(5),lambda _:None,queued=False)
window.check_library_release(); panel=window.release_dialog
first.set(); qtbot.waitUntil(lambda:not window.busy)
window._start(lambda:second.wait(5),lambda _:None,queued=False)
window.status_label.setText('NEW OPERATION STATUS')
pending={'library':Path('a.lmc'),'names':['A'],'maintenance':False,'released':False}
window._resume_batch=pending
try:
panel.released.emit({'status':'RELEASE_CONFIRMED','library':'a.lmc','evidence_path':'old-proof.json'})
qtbot.waitUntil(lambda:bool(cleaned)); qtbot.waitUntil(lambda:window._lease_pending==0)
assert window.status_label.text()=='NEW OPERATION STATUS'
assert pending['released'] is False
assert cleaned[0][0]['evidence_path']=='old-proof.json'
finally:
second.set(); qtbot.waitUntil(lambda:not window.busy); panel.close()
def test_shutdown_role_rejects_unregistered_ui_write_before_its_body(qtbot,tmp_path,monkeypatch):
from cbb_refresh_verify.operation_queue import native_phase
window=window_for(qtbot,tmp_path,monkeypatch); load(window,qtbot)
effects=[]
def action():
with native_phase(): effects.append('write')
window._start(action,lambda _:None)
qtbot.waitUntil(lambda:not window.busy)
assert effects==[]
assert 'NATIVE_ROLE_REQUIRED' in window.status_label.text()
def test_readonly_native_phase_process_really_exits_while_observer_remains_pending(tmp_path):
script=tmp_path/'readonly_app.py'
script.write_text('''from threading import Event
from pathlib import Path
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QTimer
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.operation_queue import native_phase
app=QApplication([])
MainWindow._show_unfinished_management=lambda self:None
w=MainWindow(reader=lambda _:None,data_root=Path(sys.argv[1])); w.show()
def action():
with native_phase(read_only=True):
print('READONLY_PHASE_ACTIVE',flush=True)
Event().wait(90)
w._start(action,lambda _:None)
QTimer.singleShot(250,w.close)
app.exec()
print('APP_RETURNED',flush=True)
''',encoding='utf-8')
process=subprocess.Popen([sys.executable,'-X','utf8',str(script),str(tmp_path/'data')],
stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,encoding='utf-8',
env=dict(os.environ,QT_QPA_PLATFORM='offscreen'))
try:
stdout,stderr=process.communicate(timeout=8)
assert process.returncode==0,stderr
assert 'READONLY_PHASE_ACTIVE' in stdout and 'APP_RETURNED' in stdout
finally:
if process.poll() is None: process.kill(); process.communicate()
def test_main_process_exit_hands_off_exact_worker_with_sidecar_and_writer_barrier(tmp_path):
import win32api,win32con,win32event
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
from cbb_refresh_verify.operation_queue import native_channel
library=tmp_path/'lib.lmc'; library.write_bytes(b'isolated metadata')
child_script=tmp_path/'protected_child.py'
child_script.write_text('''import sys,time,json,os
from pathlib import Path
from cbb_refresh_verify.desktop_worker import run_native_worker
root=Path(sys.argv[1])
def action(path):
(root/'ready').write_text(str(os.getpid()))
while not (root/'finish').exists(): time.sleep(.02)
run_native_worker(action,root/'request.json')
(root/'done').touch()
''',encoding='utf-8')
script=tmp_path/'owner_app.py'
script.write_text('''import sys,time,subprocess,win32api,win32con,json
from pathlib import Path
from threading import Event
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QTimer
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.operation_queue import native_channel,commit_worker_start
from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease
from cbb_refresh_verify.session_record import register_worker_session
root=Path(sys.argv[1]); library=root/'lib.lmc'
app=QApplication([]); MainWindow._show_unfinished_management=lambda self:None
w=MainWindow(reader=lambda _:None,data_root=root/'data'); w.show()
def action():
with LibraryLock(library,checkpoint=False), native_channel():
record=dict(library=str(library),block='A',operation='verify',
operation_id='protected-child',session_record_path=str(root/'session.json'),
cancel_signal=str(root/'cancel'),start_gate=str(root/'gate'),start_gate_protocol='event-v1')
(root/'request.json').write_text(json.dumps(record))
child=subprocess.Popen([sys.executable,'-X','utf8',str(root/'protected_child.py'),str(root)],
stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
handle=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,child.pid)
try: lease=duplicate_worker_lease(library,handle)
finally: handle.Close()
register_worker_session(child,{**record,'worker_lease_handle':lease})
commit_worker_start(child,record)
Event().wait(90)
job=w._new_job('verify',library=library,targets=('A',))
w._start(action,lambda _:None,job=job)
def close_ready():
if (root/'ready').exists():
print('PROTECTED_WRITER_ACTIVE',flush=True); w.close()
else: QTimer.singleShot(20,close_ready)
QTimer.singleShot(20,close_ready)
app.exec(); print('APP_RETURNED',flush=True)
''',encoding='utf-8')
started=time.monotonic()
process=subprocess.Popen([sys.executable,'-X','utf8',str(script),str(tmp_path)],
stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,encoding='utf-8',
env=dict(os.environ,QT_QPA_PLATFORM='offscreen'))
try:
stdout,stderr=process.communicate(timeout=36)
assert process.returncode==0,stderr
assert 'PROTECTED_WRITER_ACTIVE' in stdout and 'APP_RETURNED' in stdout
assert time.monotonic()-started<30
record=json.loads((tmp_path/'session.json').read_text('utf-8'))
pid=int((tmp_path/'ready').read_text())
assert record['processes'][0]['pid']==pid and record['worker_lease_handle']
assert record['start_gate_protocol']=='event-v1' and record['worker_start_handle']
receipt=json.loads(next((tmp_path/'data'/'shutdown').glob('*.json')).read_text('utf-8'))
assert receipt['safe_to_continue'] is False and receipt['status']=='UNCONFIRMED'
bindings=[binding for scope in receipt['task_scopes'] for binding in scope['worker_sessions']]
assert any(binding['operation_id']=='protected-child' and binding['path']==str(tmp_path/'session.json')
and binding['worker_protection']=='duplicated_lease' for binding in bindings)
assert record['task_operation_id'] in {scope['operation_id'] for scope in receipt['task_scopes']}
handle=win32api.OpenProcess(win32con.SYNCHRONIZE,False,pid)
try: assert win32event.WaitForSingleObject(handle,0)==win32con.WAIT_TIMEOUT
finally: handle.Close()
with pytest.raises(LibraryBusy):
with LibraryLock(library,checkpoint=False): pass
with pytest.raises(RuntimeError,match='NATIVE_WRITER_ACTIVE'):
with native_channel(): pass
finally:
(tmp_path/'finish').touch()
if process.poll() is None: process.kill(); process.communicate()
deadline=time.monotonic()+5
while (tmp_path/'ready').exists() and not (tmp_path/'done').exists() and time.monotonic()<deadline:
time.sleep(.02)
assert (tmp_path/'done').exists()
with LibraryLock(library,checkpoint=False), native_channel(): pass
def test_shutdown_during_registration_returns_waiter_and_never_opens_late_gate(tmp_path,monkeypatch):
from threading import Thread
import win32api,win32con
from cbb_refresh_verify import session_record
from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease
from cbb_refresh_verify.operation_queue import BatchTaskContext,TaskHandoff,native_channel
library=tmp_path/'lib.lmc'; library.write_bytes(b'isolated')
context=BatchTaskContext(str(library),native_policy='external')
entered,resume,returned=Event(),Event(),Event(); gates=[]; child=[]
original=session_record.write_session_record
def stalled(path,value):
entered.set(); resume.wait(5); return original(path,value)
monkeypatch.setattr(session_record,'write_session_record',stalled)
def action():
with LibraryLock(library,checkpoint=False), native_channel():
process=subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
child.append(process)
handle=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,process.pid)
try: lease=duplicate_worker_lease(library,handle)
finally: handle.Close()
session_record.register_worker_session(process,dict(library=str(library),operation='verify',
operation_id='registration',session_record_path=str(tmp_path/'session.json'),worker_lease_handle=lease))
gates.append('late native dispatch')
def run():
try: context.run(action)
except TaskHandoff: pass
finally: returned.set()
thread=Thread(target=run); thread.start(); assert entered.wait(2)
try:
state=context.shutdown()
assert state['safe_to_continue'] is False and returned.wait(1)
resume.set()
deadline=time.monotonic()+3
while context.snapshot()['pending'] and time.monotonic()<deadline: time.sleep(.01)
assert not context.snapshot()['pending']
assert gates==[]
finally:
resume.set(); thread.join(3)
for process in child: process.communicate('\n',timeout=3)
def test_readonly_observer_cannot_enter_a_mutating_phase(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext,native_phase,native_channel
effects=[]
def action():
with native_phase(read_only=True):
with native_channel(): effects.append('write')
with pytest.raises(RuntimeError,match='NATIVE_ROLE_REQUIRED'):
BatchTaskContext(str(tmp_path/'lib.lmc'),native_policy='external').run(action)
assert effects==[]
def test_readonly_role_rejects_mutating_worker_at_common_registration(tmp_path):
from cbb_refresh_verify.operation_queue import BatchTaskContext,native_phase
from cbb_refresh_verify.session_record import register_worker_session
process=subprocess.Popen([sys.executable,'-c','input()'],stdin=subprocess.PIPE,text=True)
effects=[]
def action():
with native_phase(read_only=True):
register_worker_session(process,dict(library=str(tmp_path/'lib.lmc'),operation='remove',
operation_id='forbidden',session_record_path=str(tmp_path/'session.json'),worker_lease_handle=1))
effects.append('write gate')
try:
with pytest.raises(RuntimeError,match='NATIVE_ROLE_REQUIRED'):
BatchTaskContext(str(tmp_path/'lib.lmc'),native_policy='external').run(action)
assert effects==[]
finally: process.communicate('\n',timeout=3)
def test_shutdown_does_not_abandon_a_legacy_inprocess_mutation(tmp_path):
from threading import Thread
from cbb_refresh_verify.operation_queue import BatchTaskContext,native_phase
context=BatchTaskContext(str(tmp_path/'lib.lmc'))
entered,release,returned=Event(),Event(),Event()
def action():
with native_phase(): entered.set(); release.wait(3)
def run():
try: context.run(action)
finally: returned.set()
thread=Thread(target=run); thread.start(); assert entered.wait(1)
try:
state=context.shutdown()
assert state['pending_native'] and not state['shutdown_handoff']
assert state['safe_to_continue'] is False and not returned.wait(.1)
finally: release.set(); thread.join(3)
assert returned.is_set()