"""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()