fix: hand off UI shutdown without releasing native ownership
This commit is contained in:
1 parent
cd7a5e0ca1
commit
dbc685b9a5
10 files changed
+454
-52
No files matched your search
@@ -115,7 +115,28 @@ def test_reader_never_creates_a_desktop_or_process(monkeypatch, tmp_path, reader
|
||||
import win32service
|
||||
import win32process
|
||||
import cbb_refresh_verify.mentor_adapter as module
|
||||
from pywinauto import Desktop
|
||||
import sys
|
||||
import pythoncom
|
||||
if 'pywinauto' not in sys.modules:
|
||||
# Match the real worker's STA apartment. pywinauto's first import probes
|
||||
# MTA and may report its fallback; validate that exact diagnostic only.
|
||||
import warnings
|
||||
pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
|
||||
try:
|
||||
with warnings.catch_warnings(record=True) as diagnostics:
|
||||
warnings.simplefilter('always')
|
||||
from pywinauto import Desktop
|
||||
for diagnostic in diagnostics:
|
||||
if diagnostic.category is UserWarning and str(diagnostic.message) in (
|
||||
'Revert to STA COM threading mode', 'Apply externally defined coinit_flags: 2'):
|
||||
assert sys.coinit_flags == pythoncom.COINIT_APARTMENTTHREADED
|
||||
else:
|
||||
warnings.warn_explicit(diagnostic.message, diagnostic.category,
|
||||
diagnostic.filename, diagnostic.lineno)
|
||||
finally:
|
||||
pythoncom.CoUninitialize()
|
||||
else:
|
||||
from pywinauto import Desktop
|
||||
monkeypatch.setattr(Desktop, 'windows', lambda *_: [])
|
||||
|
||||
closed = []
|
||||
|
||||
@@ -0,0 +1,281 @@
|
||||
"""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 worker_native_channel
|
||||
root=Path(sys.argv[1])
|
||||
while not (root/'gate').exists(): time.sleep(.01)
|
||||
session=json.loads((root/'session.json').read_text('utf-8'))
|
||||
with worker_native_channel(lease_handle=session['worker_lease_handle']):
|
||||
(root/'ready').write_text(str(os.getpid()))
|
||||
while not (root/'finish').exists(): time.sleep(.02)
|
||||
(root/'done').touch()
|
||||
''',encoding='utf-8')
|
||||
script=tmp_path/'owner_app.py'
|
||||
script.write_text('''import sys,time,subprocess,win32api,win32con
|
||||
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
|
||||
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():
|
||||
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,dict(library=str(library),block='A',operation='verify',
|
||||
operation_id='protected-child',session_record_path=str(root/'session.json'),
|
||||
worker_lease_handle=lease,cancel_signal=str(root/'cancel')))
|
||||
(root/'gate').touch()
|
||||
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']
|
||||
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()
|
||||
@@ -559,7 +559,8 @@ def test_queue_close_has_one_deadline_and_reports_pending_scope(tmp_path):
|
||||
thread.join(2)
|
||||
|
||||
|
||||
def test_real_management_transaction_does_not_handoff_between_remove_and_add(management_setup, monkeypatch):
|
||||
@pytest.mark.parametrize('native_policy', ['legacy', 'external'])
|
||||
def test_real_management_transaction_does_not_handoff_between_remove_and_add(management_setup, monkeypatch, native_policy):
|
||||
from dataclasses import replace
|
||||
from types import SimpleNamespace
|
||||
from test_management import execute
|
||||
@@ -576,7 +577,7 @@ def test_real_management_transaction_does_not_handoff_between_remove_and_add(man
|
||||
time.sleep(.08) # Let the real admission monitor observe the active native phase.
|
||||
return result
|
||||
adapter.remove = slow_remove # Native external boundary; transaction stays real.
|
||||
context = BatchTaskContext(library=str(request.library), targets=('Block',))
|
||||
context = BatchTaskContext(library=str(request.library), targets=('Block',), native_policy=native_policy)
|
||||
result = NativeQueue().run(lambda: execute(replace(request, operation='update',
|
||||
source=source, auto_verify=False), adapter), context=context, timeout_seconds=10)
|
||||
assert result.status == 'UPLOADED'
|
||||
|
||||
Reference in new issue
Block a user