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

+13 -7
View File
@@ -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)