fix: preserve async writer ownership and bounded handoff
This commit is contained in:
1 parent
ddff6bd857
commit
8374cabe5c
12 files changed
+692
-47
No files matched your search
@@ -55,6 +55,97 @@ def register_native_writer(process):
|
||||
return process
|
||||
|
||||
|
||||
class _PendingNativeOperation:
|
||||
"""One asynchronously submitted operation, including its native descendants."""
|
||||
def __init__(self, job, identities):
|
||||
import win32api
|
||||
from .residual_sessions import _handle_identity, _force_ownership
|
||||
from .session_record import IDENTITY_KEYS
|
||||
self.record = job
|
||||
self.job = win32job.CreateJobObject(None, '') # Deliberately no kill-on-close.
|
||||
self.owners = {}
|
||||
self.recorded = set()
|
||||
self.completed = False
|
||||
self.exited = False
|
||||
self.lock = threading.Lock()
|
||||
try:
|
||||
for identity in identities:
|
||||
pid = identity['pid']
|
||||
handle = win32api.OpenProcess(win32con.SYNCHRONIZE | 0x1000 |
|
||||
win32con.PROCESS_SET_QUOTA | win32con.PROCESS_TERMINATE, False, pid)
|
||||
try:
|
||||
actual = dict(pid=pid, **_handle_identity(handle), **_force_ownership(handle, pid))
|
||||
if any(actual[key] != identity.get(key) for key in IDENTITY_KEYS):
|
||||
raise RuntimeError('NATIVE_OPERATION_IDENTITY_CHANGED')
|
||||
win32job.AssignProcessToJobObject(self.job, handle)
|
||||
self.owners[pid] = _RegisteredWriter(handle)
|
||||
self.recorded.add(pid)
|
||||
finally:
|
||||
handle.Close()
|
||||
except Exception:
|
||||
self._close()
|
||||
raise # Admission failure is not permission to post Add without protection.
|
||||
|
||||
def _close(self):
|
||||
for owner in self.owners.values():
|
||||
if owner.returncode is None:
|
||||
owner.handle.Close()
|
||||
self.job.Close()
|
||||
self.exited = True
|
||||
|
||||
def complete(self):
|
||||
with self.lock:
|
||||
self.completed = True # Only an exact native acknowledgement settles the operation.
|
||||
|
||||
def poll(self):
|
||||
with self.lock:
|
||||
return self._poll()
|
||||
|
||||
def _poll(self):
|
||||
if self.exited:
|
||||
return 0
|
||||
pids = set(win32job.QueryInformationJobObject(self.job, win32job.JobObjectBasicProcessIdList))
|
||||
fresh = pids-self.recorded
|
||||
if fresh:
|
||||
from .session_record import update_worker_state
|
||||
from .residual_sessions import _process_handle, _handle_identity, _force_ownership
|
||||
identities = []
|
||||
for pid in fresh:
|
||||
with _process_handle(pid) as handle:
|
||||
if not win32job.IsProcessInJob(handle, self.job):
|
||||
continue # The snapshot PID may have been reused outside this job.
|
||||
identities.append(('native', dict(pid=pid, **_handle_identity(handle),
|
||||
**_force_ownership(handle, pid))))
|
||||
if identities and update_worker_state(self.record, 'UNCONFIRMED', observed_identities=identities):
|
||||
self.recorded.update(identity['pid'] for _, identity in identities)
|
||||
# An acknowledged Add does not retain an otherwise idle LM shell.
|
||||
# Descendants still need observed exit; PID reuse cannot imitate an owner.
|
||||
idle = {pid for pid, owner in self.owners.items() if owner.poll() is None} if self.completed else set()
|
||||
if not pids-idle:
|
||||
self._close()
|
||||
return 0
|
||||
return None
|
||||
|
||||
|
||||
def register_pending_native_operation(job, pids):
|
||||
from .session_record import read_session_record, update_worker_state
|
||||
pids = set(pids)
|
||||
if not pids:
|
||||
raise RuntimeError('NATIVE_OPERATION_OWNER_REQUIRED')
|
||||
# Retain the creation/image/owner identity in the existing bound journal
|
||||
# before asynchronous dispatch, so a restarted UI can validate controls.
|
||||
update_worker_state(job, 'POSTCALL', identities=[('native', pid) for pid in pids], required=True)
|
||||
path = Path(job['session_record_path']).with_name('worker-state.json')
|
||||
record = read_session_record(path, evidence_root=path.parent)
|
||||
identities = [p for p in record.get('processes', ()) if p['pid'] in pids and p.get('role') == 'native']
|
||||
if record['operation_id'] != job['operation_id'] or {p['pid'] for p in identities} != pids:
|
||||
raise RuntimeError('NATIVE_OPERATION_OWNER_UNCONFIRMED')
|
||||
operation = _PendingNativeOperation(job, identities)
|
||||
with _automation_clients_lock:
|
||||
_automation_clients.append(operation)
|
||||
return operation
|
||||
|
||||
|
||||
@contextmanager
|
||||
def worker_native_channel(*, lease_handle=None, inspection_only=False):
|
||||
"""The native worker, not its disposable supervisor, owns write exclusion."""
|
||||
@@ -221,9 +312,12 @@ def start_supervisor_watchdog(supervisor_pid, *, cancel_path=None, stop_clients=
|
||||
pending = _record_write_pending(session_record_path, operation_id)
|
||||
# The same gate blocks new calls/helpers after loss. A stale
|
||||
# POSTCALL on disk cannot override an active in-memory call.
|
||||
if (pending is False and _phase_pending(_native_phases.get(operation_id)) is False
|
||||
and all(helper.poll() is not None for helper in _automation_clients)):
|
||||
os._exit(75) # This Python worker only; native children remain untouched.
|
||||
returned = pending is False and _phase_pending(_native_phases.get(operation_id)) is False
|
||||
clients = tuple(_automation_clients)
|
||||
# Polling can publish a job descendant's exact identity. Never
|
||||
# hold the client registry lock while that journal is updated.
|
||||
if returned and all(helper.poll() is not None for helper in clients):
|
||||
os._exit(75) # This Python worker only; native children remain untouched.
|
||||
threading.Event().wait(.2)
|
||||
finally:
|
||||
parent.Close()
|
||||
|
||||
@@ -563,11 +563,9 @@ def _prove_library_released(library, block_name=None, *, block_names=(), allow_u
|
||||
from .icdb_ownership import inspect_release_addresses
|
||||
# Checkpoint I/O is per-library; only native inspection owns
|
||||
# the host channel. The sidecar still excludes other writers.
|
||||
from .operation_queue import native_channel
|
||||
with native_channel(inspection_only=True):
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('已停止释放检查')
|
||||
inspections = inspect_release_addresses(files, stop=stop)
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('已停止释放检查')
|
||||
inspections = inspect_release_addresses(files, stop=stop)
|
||||
try:
|
||||
current = stack.enter_context(hold_release_files(library, roots, stop=stop))
|
||||
except OSError as error:
|
||||
|
||||
@@ -174,7 +174,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
from .verify_runner import VerifyRequest, run_verify
|
||||
from .backup_recovery import _idle_check
|
||||
from contextlib import ExitStack
|
||||
from .operation_queue import check_active, native_channel, task_stop
|
||||
from .operation_queue import check_active, native_channel, task_stop, remember_result
|
||||
stop = task_stop(stop)
|
||||
import pythoncom
|
||||
if adapter is None:
|
||||
@@ -219,11 +219,15 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
progress({'stage': stage, 'block': request.name, 'operation': request.operation, **details})
|
||||
def finish(status, detail='', safe=True):
|
||||
nonlocal change_report
|
||||
remember_result(ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', status, safe, detail, report, release_proof, change_report))
|
||||
if mutated and original is not None and change_report is None:
|
||||
change_report = {'status': 'UNCONFIRMED', 'scope': original['scope'],
|
||||
**comparison_rules(scope),
|
||||
'unrelated_cbb_content_checked': False,
|
||||
'detail': '原生会话尚未释放,未读取写后内容'}
|
||||
remember_result(ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', status, safe, detail, report, release_proof, change_report))
|
||||
if safe:
|
||||
try:
|
||||
after = build_exclusive_library_manifest(library_root, scope=scope, progress=progress)
|
||||
@@ -245,12 +249,13 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
detail = (detail+'\n'+change_report['detail']).strip()
|
||||
result = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', status, safe, detail, report, release_proof, change_report)
|
||||
remember_result(result)
|
||||
if report:
|
||||
try:
|
||||
_write_json(report, {**asdict(result), 'history': history, 'backup_policy': 'manual'})
|
||||
except OSError as error:
|
||||
result = replace(result, detail=f'{detail}\n操作记录写入失败:{error}'.strip())
|
||||
return result
|
||||
return remember_result(result)
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
@@ -449,7 +454,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
def run_management_batch(requests, *, stop=None, progress=None, item_executor=None, context=None, **kwargs):
|
||||
from threading import Event
|
||||
from .write_admission import WriteAdmission
|
||||
from .operation_queue import QueueCancelled, TaskHandoff, request_library_keys
|
||||
from .operation_queue import QueueCancelled, TaskHandoff, request_library_keys, remember_result
|
||||
if context is not None:
|
||||
stop = stop if stop is not None else context.stop
|
||||
progress = context.guard_callback(progress) if progress else None
|
||||
@@ -472,6 +477,7 @@ def run_management_batch(requests, *, stop=None, progress=None, item_executor=No
|
||||
continue
|
||||
def action():
|
||||
result = run_management(request, progress=progress, stop=stop, **kwargs)
|
||||
remember_result(result)
|
||||
if not result.safe_to_continue:
|
||||
from .operation_lifecycle import recheck_failed_release
|
||||
result = recheck_failed_release(request.library, request.name, result, stop=stop)
|
||||
@@ -479,10 +485,18 @@ def run_management_batch(requests, *, stop=None, progress=None, item_executor=No
|
||||
try:
|
||||
result = item_executor(request, action) if item_executor is not None else action()
|
||||
except QueueCancelled as error:
|
||||
result = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete',
|
||||
'TIMEOUT' if isinstance(error, TaskHandoff) and not error.cancelled else 'CANCELLED',
|
||||
not isinstance(error, TaskHandoff), str(error))
|
||||
known = error.result if isinstance(error, TaskHandoff) else None
|
||||
if isinstance(known, ManagementResult):
|
||||
result = replace(known, safe_to_continue=False,
|
||||
status='RECOVERY_REQUIRED' if known.status in ('PASS', 'UPLOADED', 'UPLOADED_VERIFIED', 'REMOVED')
|
||||
else known.status,
|
||||
release_proof=dict(status='RELEASE_PENDING', lease_state='PENDING',
|
||||
resource_scope=error.scope, previous_release_proof=known.release_proof))
|
||||
else:
|
||||
result = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete',
|
||||
'TIMEOUT' if isinstance(error, TaskHandoff) and not error.cancelled else 'CANCELLED',
|
||||
not isinstance(error, TaskHandoff), str(error))
|
||||
results.append(result)
|
||||
if progress:
|
||||
progress({'stage': result.status, 'block': result.name, 'detail': result.detail,
|
||||
|
||||
@@ -745,6 +745,9 @@ def _probe_add_dialog(app, job, desktop, owned_pids, root, *, library=None):
|
||||
import gc
|
||||
gc.collect() # Release UIA wrappers before closing native child dialogs.
|
||||
if not job.get('_native_add_pending'):
|
||||
writer = job.pop('_native_add_writer', None)
|
||||
if writer is not None:
|
||||
writer.complete()
|
||||
_close_probe_dialogs(desktop, owned_pids)
|
||||
|
||||
|
||||
@@ -851,9 +854,18 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None)
|
||||
_require_add_supervisor(job, root)
|
||||
from .session_record import dispatch_native, update_worker_state
|
||||
def submit_add():
|
||||
_require_add_supervisor(job, root) # Registration/journal I/O may have outlived cancellation.
|
||||
job['_native_add_pending'] = True
|
||||
win32gui.PostMessage(ok, win32con.BM_CLICK, 0, 0)
|
||||
dispatch_native(job, submit_add, asynchronous=True, required=True)
|
||||
from .desktop_worker import register_pending_native_operation
|
||||
writer = register_pending_native_operation(job, owned_pids)
|
||||
job['_native_add_writer'] = writer
|
||||
try:
|
||||
dispatch_native(job, submit_add, asynchronous=True, required=True)
|
||||
except Exception:
|
||||
if not job.get('_native_add_pending'):
|
||||
writer.complete()
|
||||
raise
|
||||
success = None
|
||||
while time.monotonic() < deadline:
|
||||
pythoncom.PumpWaitingMessages()
|
||||
@@ -884,6 +896,7 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None)
|
||||
if not success:
|
||||
raise TimeoutError('未取得原生新增成功消息;保留副本与私有桌面证据。')
|
||||
job['_native_add_pending'] = False
|
||||
writer.complete()
|
||||
update_worker_state(job, 'POSTCALL')
|
||||
|
||||
|
||||
|
||||
@@ -278,9 +278,8 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr
|
||||
job_path.write_text(json.dumps(job, ensure_ascii=False), encoding='utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)]
|
||||
from .operation_queue import native_channel, task_stop
|
||||
with native_channel(inspection_only=inspection_only is True):
|
||||
return _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress, task_stop(stop), job)
|
||||
from .operation_queue import task_stop
|
||||
return _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress, task_stop(stop), job)
|
||||
|
||||
|
||||
def _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress, stop, job):
|
||||
@@ -289,15 +288,19 @@ def _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress,
|
||||
worker.bind_cancellation(cancel)
|
||||
if hasattr(worker, 'bind_session'):
|
||||
worker.bind_session(job)
|
||||
from .operation_queue import check_active
|
||||
check_active()
|
||||
Path(job['start_gate']).touch()
|
||||
from .operation_queue import check_active, native_channel
|
||||
native = native_channel(inspection_only=job.get('inspection_only') is True)
|
||||
entered = False
|
||||
# Worker admission includes filesystem/ownership checks before the native
|
||||
# timeout begins. Bound that preparation separately; never extend on polls.
|
||||
deadline = time.monotonic()+60+max(1, float(timeout_seconds))+5
|
||||
event_count = 0
|
||||
cancelled = False
|
||||
try:
|
||||
check_active()
|
||||
native.__enter__()
|
||||
entered = True
|
||||
Path(job['start_gate']).touch()
|
||||
while worker.poll() is None and time.monotonic()<deadline:
|
||||
if not cancelled and stop is not None and stop.is_set():
|
||||
worker.request_stop()
|
||||
@@ -313,6 +316,9 @@ def _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress,
|
||||
break
|
||||
event_count += 1
|
||||
time.sleep(.2)
|
||||
if worker.poll() is not None:
|
||||
native.__exit__(None, None, None)
|
||||
entered = False
|
||||
outcome = root/'repair-result.json'
|
||||
if worker.poll() is None or not outcome.is_file():
|
||||
worker.request_stop()
|
||||
@@ -326,6 +332,8 @@ def _supervise_repair(command, desktop, cancel, root, timeout_seconds, progress,
|
||||
result['status'] = 'UNCONFIRMED'
|
||||
return {**result, 'evidence_path': str(root)}
|
||||
finally:
|
||||
if entered:
|
||||
native.__exit__(None, None, None)
|
||||
if worker.poll() is None and getattr(worker, 'session_record_path', None):
|
||||
from .session_record import handoff_session
|
||||
from .operation_queue import hold_native_handoff
|
||||
|
||||
@@ -67,6 +67,7 @@ class _Call:
|
||||
abandoned: bool = False
|
||||
native_depth: int = 0
|
||||
native_handoff: str = ''
|
||||
handoff_cancelled: bool = False
|
||||
phase_started: float = field(default_factory=time.monotonic)
|
||||
result: object = None
|
||||
last_result: object = None
|
||||
@@ -101,19 +102,18 @@ def remember_result(result):
|
||||
|
||||
|
||||
def hold_native_handoff(process, session_record_path):
|
||||
"""Keep this thread's mutex while a recorded native client is unreturned.
|
||||
"""Yield parent admission to controls; the gated worker retains write exclusion.
|
||||
|
||||
The caller registers the observer first. Only UI/scheduling is handed off;
|
||||
this thread and its native channel remain alive until that exact handle exits.
|
||||
The caller registers the exact worker observer first. Its writer barrier and
|
||||
inherited sidecar survive this supervisor, including while a user answers Save.
|
||||
"""
|
||||
call = getattr(_local, 'call', None)
|
||||
if call is not None:
|
||||
with _tasks_guard:
|
||||
if not call.native_depth:
|
||||
raise RuntimeError('Native handoff requires an owned native phase')
|
||||
call.handoff_cancelled = call.is_set()
|
||||
call.native_handoff = str(session_record_path)
|
||||
while process.poll() is None:
|
||||
Event().wait(.1)
|
||||
call.abandoned = True
|
||||
raise TaskHandoff(call.context, cancelled=call.handoff_cancelled, result=call.last_result)
|
||||
|
||||
|
||||
@contextmanager
|
||||
@@ -252,7 +252,7 @@ class BatchTaskContext:
|
||||
while not call.done.wait(.02):
|
||||
with _tasks_guard:
|
||||
if call.native_handoff:
|
||||
cancelled = call.is_set()
|
||||
cancelled = call.handoff_cancelled
|
||||
call.abandoned = True
|
||||
raise TaskHandoff(self, cancelled=cancelled, result=call.last_result)
|
||||
elapsed = time.monotonic()-call.phase_started
|
||||
|
||||
@@ -138,7 +138,8 @@ def register_worker_session(worker, job):
|
||||
worker.cbb_library = job['library']
|
||||
|
||||
|
||||
def update_worker_state(job, phase, *, identities=(), write_outcome=None, required=False, continuation=False):
|
||||
def update_worker_state(job, phase, *, identities=(), observed_identities=(), write_outcome=None,
|
||||
required=False, continuation=False):
|
||||
"""required controls I/O; only existing same-phase calls may continue recording after supervisor loss."""
|
||||
if not job.get('session_record_path'):
|
||||
if required:
|
||||
@@ -155,6 +156,12 @@ def update_worker_state(job, phase, *, identities=(), write_outcome=None, requir
|
||||
for role, pid in identities:
|
||||
if pid and not any(p['pid'] == pid for p in state['processes']):
|
||||
state['processes'].append({'role': role, **process_identity(pid)})
|
||||
# Callers supply these only after validating an open handle's exact
|
||||
# job membership; do not reopen a snapshot PID during publication.
|
||||
for role, identity in observed_identities:
|
||||
bound = {key: identity[key] for key in IDENTITY_KEYS}
|
||||
if not any(all(p.get(key) == value for key, value in bound.items()) for p in state['processes']):
|
||||
state['processes'].append({'role': role, **bound})
|
||||
state['phase'] = phase
|
||||
if write_outcome is not None:
|
||||
state['write_outcome'] = write_outcome
|
||||
|
||||
@@ -965,6 +965,11 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
worker_launch_attempted = False
|
||||
native = native_channel()
|
||||
phase_entered = False
|
||||
def end_native_phase():
|
||||
nonlocal phase_entered
|
||||
if phase_entered:
|
||||
native.__exit__(None, None, None)
|
||||
phase_entered = False
|
||||
try:
|
||||
timing.begin('worker_setup')
|
||||
with (root/'worker.log').open('w', encoding='utf-8') as log:
|
||||
@@ -1186,6 +1191,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
return finish(VerifyResult(status, stderr=detail, safe_to_continue=False))
|
||||
return finish(VerifyResult('TIMEOUT', stderr='操作超时,当前项未完成。', error_stage=phase.value, safe_to_continue=False))
|
||||
time.sleep(.2)
|
||||
end_native_phase() # Exact worker exit precedes all optional outcome/evidence I/O.
|
||||
worker_result = root/'outcome.json'
|
||||
if not worker_result.exists():
|
||||
return finish(VerifyResult('FAIL', stderr=f'执行器异常退出 ({process.returncode}),见 worker.log', safe_to_continue=False))
|
||||
@@ -1215,6 +1221,10 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
close_errors = []
|
||||
worker_exited = False
|
||||
automation_release = {'safe_to_continue': False}
|
||||
if 'process' not in locals() or process.poll() is not None or getattr(process, 'session_record_path', None):
|
||||
# A gated worker owns its writer barrier and inherited sidecar.
|
||||
# Parent admission must not block controls needed for its exit.
|
||||
end_native_phase()
|
||||
if 'dialog_agent' in locals() and dialog_agent is not None:
|
||||
try:
|
||||
dialog_agent.close()
|
||||
@@ -1238,10 +1248,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
{'stage': 'session_observation', 'block': request.block_name, **state}))
|
||||
_notify_progress(progress, {'stage': 'session_handoff', 'block': request.block_name,
|
||||
'detail': '已转后台观察,可查看状态'})
|
||||
if getattr(process, 'native_write_pending', lambda: None)() is not False:
|
||||
from .operation_queue import hold_native_handoff
|
||||
hold_native_handoff(process, process.session_record_path)
|
||||
worker_exited = process.poll() is not None
|
||||
from .operation_queue import hold_native_handoff
|
||||
hold_native_handoff(process, process.session_record_path)
|
||||
except Exception as error:
|
||||
close_errors.append(str(error))
|
||||
if 'process' in locals():
|
||||
@@ -1333,8 +1341,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
except OSError:
|
||||
pass
|
||||
finally:
|
||||
if phase_entered:
|
||||
native.__exit__(None, None, None)
|
||||
end_native_phase()
|
||||
|
||||
if result.status == 'PASS' and not request.diagnostic_only:
|
||||
timing.begin('library_flag')
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Run the real Add dispatch with harmless process and Win32/UIA boundaries."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
from types import SimpleNamespace
|
||||
|
||||
from pytest import MonkeyPatch
|
||||
|
||||
from cbb_refresh_verify import desktop_worker, native_management as native
|
||||
from cbb_refresh_verify.session_record import update_worker_state
|
||||
|
||||
|
||||
def run(job_path):
|
||||
job = json.loads(Path(job_path).read_text(encoding='utf-8'))
|
||||
root = Path(job['root'])
|
||||
signals = root.parent
|
||||
mode = job['test_mode']
|
||||
lm_script = '''import subprocess,sys,time
|
||||
from pathlib import Path
|
||||
root=Path(sys.argv[1])
|
||||
input()
|
||||
if sys.argv[2] == 'pending':
|
||||
code='import sys,time; from pathlib import Path; p=Path(sys.argv[1]); '+\
|
||||
'\\nwhile not p.exists(): time.sleep(.02)'
|
||||
child=subprocess.Popen([sys.executable,'-c',code,str(root/'native-done')])
|
||||
(root/'descendant-pid').write_text(str(child.pid))
|
||||
while not (root/'lm-exit').exists(): time.sleep(.02)
|
||||
'''
|
||||
lm = subprocess.Popen([sys.executable, '-c', lm_script, str(signals), mode], stdin=subprocess.PIPE, text=True)
|
||||
(signals/'lm-pid').write_text(str(lm.pid))
|
||||
update_worker_state(job, 'POSTCALL', identities=[('native', lm.pid)], required=True)
|
||||
desktop_worker.start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'],
|
||||
session_record_path=job['session_record_path'], operation_id=job['operation_id'],
|
||||
supervisor_identity=job['supervisor_identity'])
|
||||
state = {'new': False, 'posted': False, 'closed': False}
|
||||
patch = MonkeyPatch()
|
||||
import win32gui, win32process
|
||||
if mode == 'assignment-failed':
|
||||
def deny_job(*args):
|
||||
raise OSError('exact native job admission denied')
|
||||
patch.setattr(desktop_worker.win32job, 'AssignProcessToJobObject', deny_job)
|
||||
if mode == 'cancel-after-registration':
|
||||
assign = desktop_worker.win32job.AssignProcessToJobObject
|
||||
def cancel_after_assign(*args):
|
||||
result = assign(*args)
|
||||
(root/'cancel-native-add.signal').touch()
|
||||
return result
|
||||
patch.setattr(desktop_worker.win32job, 'AssignProcessToJobObject', cancel_after_assign)
|
||||
tab = SimpleNamespace(window_text=lambda: 'Logical - Physical', is_selected=lambda: True)
|
||||
patch.setitem(sys.modules, 'pywinauto', SimpleNamespace(Desktop=lambda **kw: SimpleNamespace(
|
||||
window=lambda **kw: SimpleNamespace(descendants=lambda **kw: [tab]))))
|
||||
patch.setattr(win32process, 'GetWindowThreadProcessId', lambda _: (1, lm.pid))
|
||||
patch.setattr(win32gui, 'IsWindowVisible', lambda _: True)
|
||||
patch.setattr(win32gui, 'IsWindowEnabled', lambda _: True)
|
||||
patch.setattr(win32gui, 'GetWindowText', lambda h: {
|
||||
100: 'Reusable Blocks', 200: 'New Reusable Block', 300: 'Native result',
|
||||
301: 'New Logical-Physical Reuse Block was successfully added.'}.get(int(h), ''))
|
||||
patch.setattr(win32gui, 'GetDlgItem', lambda h, c:
|
||||
110 if h == 100 and c == 1066 else 112 if h == 100 else 210 if c == 1083 else 302 if h == 300 else c+1000)
|
||||
patch.setattr(win32gui, 'GetClassName', lambda _: 'Edit')
|
||||
patch.setattr(win32gui, 'IsWindow', lambda _: not state['closed'])
|
||||
patch.setattr(win32gui, 'EnumChildWindows', lambda h, fn, arg:
|
||||
fn(301, arg) if h == 300 and mode == 'acknowledged' else None)
|
||||
# Only the external native text-control setter is substituted.
|
||||
patch.setattr(native, '_set_probe_field', lambda *args: None)
|
||||
def post(hwnd, *args):
|
||||
if hwnd == 110: state['new'] = True
|
||||
if hwnd == 210:
|
||||
state['posted'] = True
|
||||
lm.stdin.write('Add\n')
|
||||
lm.stdin.flush()
|
||||
(signals/'add-dispatched').touch()
|
||||
if hwnd == 112: state['closed'] = True
|
||||
patch.setattr(win32gui, 'PostMessage', post)
|
||||
def windows():
|
||||
if state['closed']: return []
|
||||
return ([100, 200, 300] if state['posted'] and mode == 'acknowledged' else
|
||||
[100, 200] if state['new'] else [100])
|
||||
desktop = SimpleNamespace(EnumDesktopWindows=windows)
|
||||
try:
|
||||
native._probe_add_dialog(SimpleNamespace(OnReusableBlocksEditor=lambda: None),
|
||||
job, desktop, {lm.pid}, root)
|
||||
result = {'success': True, 'safe_to_continue': False}
|
||||
except (TimeoutError, OSError, RuntimeError) as error:
|
||||
result = {'success': False, 'safe_to_continue': False, 'detail': str(error)}
|
||||
finally:
|
||||
patch.undo()
|
||||
(root/'result.json').write_text(json.dumps(result))
|
||||
update_worker_state(job, 'UNCONFIRMED')
|
||||
(signals/'add-unwound').touch()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
desktop_worker.run_native_worker(run, Path(sys.argv[1]))
|
||||
@@ -300,21 +300,27 @@ def test_maintenance_checkpoint_admission_does_not_hold_native_channel(tmp_path,
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_native_handoff_keeps_real_mutex_until_bound_process_returns(tmp_path):
|
||||
def test_native_handoff_keeps_worker_barrier_until_bound_process_returns(tmp_path):
|
||||
import subprocess
|
||||
import sys
|
||||
from cbb_refresh_verify.operation_queue import (
|
||||
BatchTaskContext, NativeQueue, TaskHandoff, hold_native_handoff, native_channel)
|
||||
from cbb_refresh_verify.session_record import write_session_record
|
||||
process = subprocess.Popen([sys.executable, '-c', 'input()'], stdin=subprocess.PIPE, text=True)
|
||||
process = None
|
||||
context = BatchTaskContext(library=str(tmp_path/'A.lmc'))
|
||||
record = tmp_path/'session.json'
|
||||
write_session_record(record, dict(operation_id=context.operation_id, library=context.library,
|
||||
worker_pid=process.pid))
|
||||
queue = NativeQueue()
|
||||
effects = []
|
||||
def action():
|
||||
nonlocal process
|
||||
with native_channel():
|
||||
script = ('from cbb_refresh_verify.desktop_worker import worker_native_channel\n'
|
||||
'with worker_native_channel():\n print("ready",flush=True)\n input()\n')
|
||||
process = subprocess.Popen([sys.executable, '-c', script], stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, text=True)
|
||||
assert process.stdout.readline().strip() == 'ready'
|
||||
write_session_record(record, dict(operation_id=context.operation_id, library=context.library,
|
||||
worker_pid=process.pid))
|
||||
hold_native_handoff(process, record)
|
||||
with native_channel():
|
||||
effects.append('late mutation')
|
||||
@@ -322,9 +328,9 @@ def test_native_handoff_keeps_real_mutex_until_bound_process_returns(tmp_path):
|
||||
with pytest.raises(TaskHandoff):
|
||||
queue.run(action, context=context)
|
||||
assert process.poll() is None
|
||||
assert context.snapshot()['pending_native']
|
||||
assert not context.snapshot()['pending_native'] # Parent admission yielded to exact controls.
|
||||
assert queue.run(lambda: 'pure read') == 'pure read'
|
||||
with pytest.raises(RuntimeError, match='NATIVE_CHANNEL_BUSY'):
|
||||
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
|
||||
with native_channel():
|
||||
pytest.fail('writer overlapped handed-off native call')
|
||||
process.communicate('\n', timeout=3)
|
||||
@@ -336,7 +342,7 @@ def test_native_handoff_keeps_real_mutex_until_bound_process_returns(tmp_path):
|
||||
pass
|
||||
assert effects == []
|
||||
finally:
|
||||
if process.poll() is None:
|
||||
if process is not None and process.poll() is None:
|
||||
process.communicate('\n', timeout=3)
|
||||
context.close(timeout_seconds=3)
|
||||
|
||||
|
||||
@@ -0,0 +1,387 @@
|
||||
"""Real lifecycle regressions from the independent Task 2 review."""
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from threading import Event, Thread
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from test_management import setup as management_setup
|
||||
|
||||
|
||||
def wait_done(context):
|
||||
deadline = time.monotonic()+3
|
||||
while context.snapshot()['pending'] and time.monotonic()<deadline:
|
||||
time.sleep(.01)
|
||||
assert not context.snapshot()['pending']
|
||||
|
||||
|
||||
def test_management_result_survives_actual_late_release_read_handoff(management_setup, monkeypatch):
|
||||
from cbb_refresh_verify.management import run_management_batch
|
||||
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue
|
||||
from cbb_refresh_verify.session_record import write_session_record
|
||||
request, adapter, _ = management_setup
|
||||
entered, resume = Event(), Event()
|
||||
session = []
|
||||
remove = adapter.remove
|
||||
def fail_remove(req, evidence):
|
||||
native = remove(req, evidence)
|
||||
path = evidence.parent/'session.json'
|
||||
write_session_record(path, dict(operation_id='failed-remove', library=str(req.library),
|
||||
block=req.name, processes=[]))
|
||||
session.append(path)
|
||||
return replace(native, success=False, safe_to_continue=False, detail='exact native removal failure')
|
||||
adapter.remove = fail_remove
|
||||
opening = Path.open
|
||||
def open_path(path, *args, **kwargs):
|
||||
if session and path == session[0] and args == ('rb',):
|
||||
entered.set()
|
||||
assert resume.wait(4)
|
||||
return opening(path, *args, **kwargs)
|
||||
monkeypatch.setattr(Path, 'open', open_path)
|
||||
context = BatchTaskContext(library=str(request.library), targets=(request.name,))
|
||||
queue = NativeQueue()
|
||||
try:
|
||||
results = run_management_batch([request], adapter=adapter, idle_check=lambda *_: None,
|
||||
item_executor=lambda req, action: queue.run(action, context=context, timeout_seconds=1))
|
||||
assert entered.is_set()
|
||||
result = results[0]
|
||||
assert result.status == 'RECOVERY_REQUIRED'
|
||||
assert result.detail.startswith('exact native removal failure')
|
||||
assert result.evidence_path.is_file()
|
||||
assert result.change_report['status'] == 'UNCONFIRMED'
|
||||
assert result.release_proof['lease_state'] == 'PENDING'
|
||||
assert not result.safe_to_continue
|
||||
finally:
|
||||
resume.set()
|
||||
wait_done(context)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status,expected', [('UPLOAD_FAILED', 'UPLOAD_FAILED'), ('UPLOADED', 'RECOVERY_REQUIRED')])
|
||||
def test_management_batch_preserves_carried_handoff_result(management_setup, status, expected):
|
||||
from cbb_refresh_verify.management import ManagementResult, run_management_batch
|
||||
from cbb_refresh_verify.operation_queue import BatchTaskContext, TaskHandoff
|
||||
request, _, _ = management_setup
|
||||
known = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', status, False, 'known native detail',
|
||||
request.evidence_dir/'checkpoint.json', change_report={'status': 'UNCONFIRMED'})
|
||||
context = BatchTaskContext(library=str(request.library))
|
||||
def executor(req, action):
|
||||
raise TaskHandoff(context, result=known)
|
||||
result = run_management_batch([request], item_executor=executor)[0]
|
||||
assert result.status == expected and result.detail == known.detail
|
||||
assert result.evidence_path == known.evidence_path and result.change_report == known.change_report
|
||||
assert result.release_proof['lease_state'] == 'PENDING' and not result.safe_to_continue
|
||||
|
||||
|
||||
def test_exited_verify_worker_outcome_read_is_bounded_and_releases_channel(tmp_path, monkeypatch):
|
||||
import json
|
||||
from test_verify_runner import fixture, request
|
||||
from cbb_refresh_verify.verify_runner import run_verify
|
||||
from cbb_refresh_verify.operation_queue import BatchTaskContext, native_channel
|
||||
fixture(tmp_path)
|
||||
entered, resume, completed, stop = Event(), Event(), Event(), Event()
|
||||
probes, results, errors = [], [], []
|
||||
class ExitedWorker:
|
||||
desktop = None
|
||||
returncode = 0
|
||||
def __init__(self, command, *args, **kwargs):
|
||||
root = Path(command[-1]).parent
|
||||
(root/'outcome.json').write_text(json.dumps(dict(status='PASS')))
|
||||
def poll(self): return 0
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', ExitedWorker)
|
||||
original = Path.read_text
|
||||
def read(path, *args, **kwargs):
|
||||
if path.name == 'outcome.json':
|
||||
entered.set()
|
||||
assert resume.wait(4)
|
||||
return original(path, *args, **kwargs)
|
||||
monkeypatch.setattr(Path, 'read_text', read)
|
||||
context = BatchTaskContext(library=str(tmp_path/'lib.lmc'))
|
||||
def run():
|
||||
try:
|
||||
results.append(run_verify(request(tmp_path), stop=stop, context=context,
|
||||
admission_timeout_seconds=1, port_check=lambda: (True, ''),
|
||||
flag_reader=lambda *a: probes.append('late native status read')))
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
finally:
|
||||
completed.set()
|
||||
thread = Thread(target=run)
|
||||
thread.start()
|
||||
try:
|
||||
assert entered.wait(3), (results, errors)
|
||||
stop.set()
|
||||
assert completed.wait(1.5), 'exited worker metadata retained native phase'
|
||||
with native_channel():
|
||||
pass
|
||||
assert not errors and not results[0].safe_to_continue
|
||||
resume.set()
|
||||
wait_done(context)
|
||||
assert probes == []
|
||||
finally:
|
||||
resume.set()
|
||||
thread.join(4)
|
||||
wait_done(context)
|
||||
|
||||
|
||||
def test_repair_session_write_before_gate_is_bounded_without_host_channel(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.native_repair import run_native_repair, RepairTarget
|
||||
from cbb_refresh_verify.operation_queue import BatchTaskContext, TaskHandoff, native_channel
|
||||
from cbb_refresh_verify import session_record
|
||||
entered, resume, completed = Event(), Event(), Event()
|
||||
errors = []
|
||||
class GatedWorker:
|
||||
returncode = 0
|
||||
def __init__(self, command, *args, **kwargs): pass
|
||||
def bind_cancellation(self, path): pass
|
||||
def bind_session(self, job):
|
||||
session_record.write_session_record(Path(job['session_record_path']),
|
||||
{'operation_id': job['operation_id']})
|
||||
def poll(self): return 0
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', GatedWorker)
|
||||
original = Path.open
|
||||
def opening(path, *args, **kwargs):
|
||||
if path.name.startswith('session.json.') and path.suffix == '.pending':
|
||||
entered.set()
|
||||
assert resume.wait(4)
|
||||
return original(path, *args, **kwargs)
|
||||
monkeypatch.setattr(Path, 'open', opening)
|
||||
context = BatchTaskContext(library=str(tmp_path/'A.lmc'))
|
||||
def run():
|
||||
try:
|
||||
context.run(lambda: run_native_repair(RepairTarget(tmp_path/'A.prj', tmp_path/'database'),
|
||||
tmp_path/'evidence'), timeout_seconds=.1)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
finally:
|
||||
completed.set()
|
||||
thread = Thread(target=run)
|
||||
thread.start()
|
||||
try:
|
||||
assert entered.wait(2)
|
||||
assert completed.wait(.8), 'Repair pre-gate journal retained native phase'
|
||||
with native_channel():
|
||||
pass
|
||||
assert len(errors) == 1 and isinstance(errors[0], TaskHandoff)
|
||||
resume.set()
|
||||
wait_done(context)
|
||||
assert not (tmp_path/'evidence/start-repair.signal').exists()
|
||||
finally:
|
||||
resume.set()
|
||||
thread.join(4)
|
||||
wait_done(context)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('control', ['save', 'cancel'])
|
||||
def test_handed_off_worker_accepts_exact_controls_but_excludes_new_writers(tmp_path, monkeypatch, control):
|
||||
import subprocess
|
||||
import sys
|
||||
from cbb_refresh_verify.operation_queue import BatchTaskContext, TaskHandoff, native_channel, hold_native_handoff
|
||||
from cbb_refresh_verify import residual_sessions as sessions
|
||||
from cbb_refresh_verify.session_record import write_session_record
|
||||
library = tmp_path/'library/lib.lmc'
|
||||
library.parent.mkdir()
|
||||
library.touch()
|
||||
context = BatchTaskContext(library=str(library))
|
||||
child = []
|
||||
ready, answered = Event(), Event()
|
||||
errors = []
|
||||
script = ('from cbb_refresh_verify.desktop_worker import worker_native_channel\n'
|
||||
'with worker_native_channel():\n print("ready",flush=True)\n input()\n')
|
||||
def action():
|
||||
with native_channel():
|
||||
process = subprocess.Popen([sys.executable, '-c', script], stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, text=True)
|
||||
child.append(process)
|
||||
assert process.stdout.readline().strip() == 'ready'
|
||||
record = tmp_path/'session.json'
|
||||
write_session_record(record, dict(operation_id=context.operation_id, library=str(library)))
|
||||
ready.set()
|
||||
hold_native_handoff(process, record)
|
||||
controller = None
|
||||
try:
|
||||
with pytest.raises(TaskHandoff):
|
||||
context.run(action)
|
||||
assert ready.is_set() and child[0].poll() is None
|
||||
row = dict(pid=child[0].pid, name='iCDBProjectBackup.exe', created='exact-child',
|
||||
executable='C:/Mentor/iCDBProjectBackup.exe', command=f'backup -p "{library.parent}/A.prj"',
|
||||
scope_confirmed=True, cancel_prompt={'title': 'captured'})
|
||||
def answer(observed, *args, **kwargs):
|
||||
assert observed['pid'] == child[0].pid
|
||||
child[0].communicate('\n', timeout=3)
|
||||
answered.set()
|
||||
return {'release_status': 'RELEASED'}
|
||||
if control == 'save':
|
||||
monkeypatch.setattr(sessions, '_answer_save_question', answer)
|
||||
command = lambda: sessions.answer_save_question(row, 6)
|
||||
else:
|
||||
monkeypatch.setattr(sessions, '_request_native_cancel', answer)
|
||||
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [row] if child[0].poll() is None else [])
|
||||
monkeypatch.setattr(sessions, '_process_windows', lambda _: {})
|
||||
command = lambda: sessions.release_residual_sessions(dict(library=str(library), sessions=[row]),
|
||||
tmp_path/'controls', cancel_native=True)
|
||||
def control_call():
|
||||
try: command()
|
||||
except Exception as error: errors.append(error)
|
||||
# New writes still fail, but the exact corrective control can progress.
|
||||
with pytest.raises(RuntimeError):
|
||||
with native_channel():
|
||||
pytest.fail('overlapping new writer admitted')
|
||||
controller = Thread(target=control_call)
|
||||
controller.start()
|
||||
assert answered.wait(1), errors
|
||||
controller.join(5)
|
||||
assert not controller.is_alive() and not errors
|
||||
wait_done(context)
|
||||
with native_channel():
|
||||
pass
|
||||
finally:
|
||||
if child and child[0].poll() is None:
|
||||
child[0].communicate('\n', timeout=3)
|
||||
if controller is not None: controller.join(5)
|
||||
wait_done(context)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode', ['pending', 'acknowledged', 'assignment-failed', 'cancel-after-registration'])
|
||||
def test_real_async_add_unwind_and_parent_exit_keep_only_pending_writer_scope(tmp_path, mode):
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import win32api, win32con, win32event
|
||||
from test_management import project
|
||||
from cbb_refresh_verify import library_lock as locks
|
||||
from cbb_refresh_verify.operation_queue import native_channel
|
||||
library = tmp_path/'library/lib.lmc'
|
||||
library.parent.mkdir()
|
||||
library.touch()
|
||||
source = project(tmp_path/'source', name='Block')
|
||||
for path in (source, source.parent/'PCB/pcb.prj'):
|
||||
with path.open('a', encoding='utf-8') as stream:
|
||||
stream.write(f'KEY CentralLibrary "{library}"\n')
|
||||
root = tmp_path/'native-add'
|
||||
root.mkdir()
|
||||
helper = Path(__file__).parent/'support/async_add_lifecycle_fixture.py'
|
||||
parent_script = '''import sys,json,time,subprocess,os,win32api,win32con
|
||||
from pathlib import Path
|
||||
from cbb_refresh_verify.library_lock import LibraryLock,duplicate_worker_lease
|
||||
from cbb_refresh_verify.session_record import register_worker_session,process_identity
|
||||
library,source,root,helper=map(Path,sys.argv[1:5])
|
||||
job=dict(operation='probe_create_add',library=str(library),source=str(source),root=str(root),name='Block',
|
||||
operation_id='actual-add',session_record_path=str(root/'session.json'),start_gate=str(root/'gate'),
|
||||
cancel_signal=str(root/'cancel'),supervisor_pid=os.getpid(),supervisor_identity=process_identity(os.getpid()),
|
||||
timeout_seconds=.3,test_mode=sys.argv[5])
|
||||
(root/'request.json').write_text(json.dumps(job))
|
||||
with LibraryLock(library,checkpoint=False):
|
||||
with (root/'worker-output.txt').open('w') as output:
|
||||
worker=subprocess.Popen([sys.executable,str(helper),str(root/'request.json')],stdout=output,stderr=output)
|
||||
(root.parent/'worker-pid').write_text(str(worker.pid))
|
||||
handle=win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE,False,worker.pid)
|
||||
try: inherited=duplicate_worker_lease(library,handle)
|
||||
finally: handle.Close()
|
||||
register_worker_session(worker,{**job,'worker_lease_handle':inherited})
|
||||
(root/'gate').touch()
|
||||
deadline=time.monotonic()+12
|
||||
while not (root.parent/'add-unwound').exists() and time.monotonic()<deadline: time.sleep(.02)
|
||||
assert (root.parent/'add-unwound').exists()
|
||||
'''
|
||||
parent = subprocess.Popen([sys.executable, '-c', parent_script, str(library), str(source), str(root), str(helper), mode])
|
||||
handles = []
|
||||
def open_pid_file(name):
|
||||
pid = int((tmp_path/name).read_text())
|
||||
handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, pid)
|
||||
handles.append(handle)
|
||||
return handle
|
||||
try:
|
||||
code = parent.wait(timeout=15)
|
||||
assert code == 0, (root/'worker-output.txt').read_text(errors='replace')
|
||||
if mode in ('assignment-failed', 'cancel-after-registration'):
|
||||
assert not (tmp_path/'add-dispatched').exists()
|
||||
detail = 'exact native job admission denied' if mode == 'assignment-failed' else 'NATIVE_ADD_CANCELLED'
|
||||
assert detail in (root/'result.json').read_text()
|
||||
with native_channel(): pass
|
||||
with locks.LibraryLock(library, checkpoint=False): pass
|
||||
return
|
||||
assert (tmp_path/'add-dispatched').exists()
|
||||
lm = open_pid_file('lm-pid')
|
||||
if mode == 'pending':
|
||||
worker = open_pid_file('worker-pid')
|
||||
from cbb_refresh_verify.session_record import read_session_record
|
||||
journal = read_session_record(root/'worker-state.json', evidence_root=root)
|
||||
identity = next(p for p in journal['processes'] if p['pid'] == int((tmp_path/'lm-pid').read_text()))
|
||||
assert identity['created_handle'] and identity['image'] and identity['owner_sid']
|
||||
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
|
||||
with native_channel(): pass
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks.LibraryLock(library, checkpoint=False): pass
|
||||
(tmp_path/'lm-exit').touch()
|
||||
assert win32event.WaitForSingleObject(lm, 3000) == win32con.WAIT_OBJECT_0
|
||||
assert win32event.WaitForSingleObject(worker, 0) != win32con.WAIT_OBJECT_0
|
||||
deadline = time.monotonic()+2
|
||||
descendant = int((tmp_path/'descendant-pid').read_text())
|
||||
while time.monotonic()<deadline:
|
||||
journal = read_session_record(root/'worker-state.json', evidence_root=root)
|
||||
if any(p['pid'] == descendant for p in journal['processes']): break
|
||||
time.sleep(.02)
|
||||
assert any(p['pid'] == descendant and p['created_handle'] for p in journal['processes'])
|
||||
with pytest.raises(RuntimeError, match='NATIVE_WRITER_ACTIVE'):
|
||||
with native_channel(): pass
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks.LibraryLock(library, checkpoint=False): pass
|
||||
(tmp_path/'native-done').touch()
|
||||
assert win32event.WaitForSingleObject(worker, 5000) == win32con.WAIT_OBJECT_0
|
||||
else:
|
||||
deadline = time.monotonic()+3
|
||||
while time.monotonic()<deadline:
|
||||
try:
|
||||
with native_channel(): pass
|
||||
break
|
||||
except RuntimeError:
|
||||
time.sleep(.02)
|
||||
with native_channel(): pass
|
||||
assert win32event.WaitForSingleObject(lm, 0) != win32con.WAIT_OBJECT_0, 'idle LM was killed'
|
||||
with locks.LibraryLock(library, checkpoint=False): pass
|
||||
finally:
|
||||
(tmp_path/'lm-exit').touch()
|
||||
(tmp_path/'native-done').touch()
|
||||
for handle in handles:
|
||||
win32event.WaitForSingleObject(handle, 5000)
|
||||
handle.Close()
|
||||
if (tmp_path/'worker-pid').exists():
|
||||
try:
|
||||
worker = open_pid_file('worker-pid')
|
||||
except Exception as error:
|
||||
if getattr(error, 'winerror', None) != 87: raise
|
||||
else:
|
||||
assert win32event.WaitForSingleObject(worker, 5000) == win32con.WAIT_OBJECT_0
|
||||
worker.Close()
|
||||
parent.wait(timeout=15)
|
||||
|
||||
|
||||
def test_add_descendant_snapshot_cannot_journal_an_unrelated_reused_pid(tmp_path, monkeypatch):
|
||||
import subprocess
|
||||
import sys
|
||||
from cbb_refresh_verify import desktop_worker
|
||||
from cbb_refresh_verify.session_record import write_session_record, read_session_record
|
||||
monkeypatch.setattr(desktop_worker, '_automation_clients', [])
|
||||
owner = subprocess.Popen([sys.executable, '-c', 'input()'], stdin=subprocess.PIPE, text=True)
|
||||
foreign = subprocess.Popen([sys.executable, '-c', 'input()'], stdin=subprocess.PIPE, text=True)
|
||||
operation = None
|
||||
query = desktop_worker.win32job.QueryInformationJobObject
|
||||
try:
|
||||
job = dict(operation_id='descendant-identity', session_record_path=str(tmp_path/'session.json'))
|
||||
write_session_record(tmp_path/'worker-state.json', dict(operation_id=job['operation_id'], processes=[]))
|
||||
operation = desktop_worker.register_pending_native_operation(job, {owner.pid})
|
||||
monkeypatch.setattr(desktop_worker.win32job, 'QueryInformationJobObject',
|
||||
lambda *args: [owner.pid, foreign.pid])
|
||||
assert operation.poll() is None
|
||||
journal = read_session_record(tmp_path/'worker-state.json', evidence_root=tmp_path)
|
||||
assert not any(p['pid'] == foreign.pid for p in journal['processes'])
|
||||
finally:
|
||||
owner.communicate('\n', timeout=3)
|
||||
foreign.communicate('\n', timeout=3)
|
||||
monkeypatch.setattr(desktop_worker.win32job, 'QueryInformationJobObject', query)
|
||||
if operation is not None:
|
||||
assert operation.poll() == 0
|
||||
@@ -7,6 +7,20 @@ import pytest
|
||||
ERROR = b'REUSE BLOCK: INFORMATION: Block\tError! Cannot copy iCDB base of Reusable Block project.\r\n'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def owned_add_process(monkeypatch):
|
||||
import subprocess, sys
|
||||
from cbb_refresh_verify import desktop_worker
|
||||
monkeypatch.setattr(desktop_worker, '_automation_clients', [])
|
||||
process = subprocess.Popen([sys.executable, '-c', 'input()'], stdin=subprocess.PIPE, text=True)
|
||||
try:
|
||||
yield process
|
||||
finally:
|
||||
process.communicate('\n', timeout=3)
|
||||
for operation in desktop_worker._automation_clients:
|
||||
assert operation.poll() is not None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['', 'target', 'pcb_present', 'question', 'extra_button', 'wrong_button', 'escape'])
|
||||
def test_editor_missing_pcb_notice_is_only_for_unrelated_incomplete_entries(tmp_path, change):
|
||||
from cbb_refresh_verify.native_add_failures import missing_other_pcb_notice
|
||||
@@ -54,7 +68,7 @@ def test_add_timeout_publishes_cancellation_without_killing_native_owner(tmp_pat
|
||||
|
||||
@pytest.mark.parametrize('cancel', [True, False])
|
||||
@pytest.mark.parametrize('journal_error', [False, True])
|
||||
def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, cancel, journal_error):
|
||||
def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, cancel, journal_error, owned_add_process):
|
||||
import os,sys
|
||||
from types import SimpleNamespace
|
||||
import win32gui,win32process
|
||||
@@ -70,7 +84,7 @@ def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, canc
|
||||
tab=SimpleNamespace(window_text=lambda:'Logical - Physical',is_selected=lambda:True)
|
||||
monkeypatch.setitem(sys.modules,'pywinauto',SimpleNamespace(Desktop=lambda **k:SimpleNamespace(
|
||||
window=lambda **k:SimpleNamespace(descendants=lambda **k:[tab]))))
|
||||
monkeypatch.setattr(win32process,'GetWindowThreadProcessId',lambda h:(1,99))
|
||||
monkeypatch.setattr(win32process,'GetWindowThreadProcessId',lambda h:(1,owned_add_process.pid))
|
||||
monkeypatch.setattr(win32gui,'IsWindowVisible',lambda h:True)
|
||||
monkeypatch.setattr(win32gui,'IsWindowEnabled',lambda h:True)
|
||||
monkeypatch.setattr(win32gui,'GetWindowText',lambda h:{100:'Reusable Blocks',200:'New Reusable Block'}[int(h)])
|
||||
@@ -102,7 +116,7 @@ def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, canc
|
||||
expected = OSError if journal_error and not cancel else RuntimeError
|
||||
message = 'NATIVE_ADD_CANCELLED' if cancel else 'phase journal unavailable' if journal_error else 'unsafe OK posted'
|
||||
with pytest.raises(expected, match=message):
|
||||
native._probe_add_dialog_impl(SimpleNamespace(OnReusableBlocksEditor=lambda:None),job,desktop,{99},root)
|
||||
native._probe_add_dialog_impl(SimpleNamespace(OnReusableBlocksEditor=lambda:None),job,desktop,{owned_add_process.pid},root)
|
||||
assert state['posted']==([110] if cancel or journal_error else [110,210])
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user