fix: preserve async writer ownership and bounded handoff

This commit is contained in:
阿白叔 committed 2026-11-16 19:51:21 +08:00
1 parent ddff6bd857
commit 8374cabe5c
12 files changed
+692 -47

No files matched your search

@@ -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]))