Repository navigation
fix(ffi): give async test callbacks an owned completion sender - #542
Conversation
The async ABI tests borrowed a local `Sender` into the runtime callback. std's channel publishes a message before it notifies the receiver, so the test thread could observe the last completion and free the channel while the runtime thread was still inside `Sender::send`, leaving it to touch a released heap counter. Pass an owned sender per submission and take it back at the top of the callback, so the channel stays alive until `send` returns. Rejected submissions never invoke the callback, so the test reclaims the sender itself when submission returns an error.
|
补充一个会让这个 PR 一直红的点: |
The C ABI gained aimux_call_async and aimux_stream_async, so the header now declares 16 exports and the end-to-end dylib check reports 16. This test still expected 14 and failed on both the count and the report line. Keep the expected count a literal: exports is already header_exports(ROOT), so deriving the expectation from it would compare the list with itself.
|
Fixed in 9e18ce2. I kept the number literal instead of deriving it from Before the change, |
|
@yyhhyyyyyy 已 approve。这个 PR 的 CI 现在只剩 Rust core 红,原因是 #544(修 minimax 测试语义冲突)已经合入 master,但本分支还没带上那个修复。麻烦 rebase/merge master 一下,Rust core 就会绿,然后我直接合。 |
Summary
aimux_call_asynchands an opaqueuser_datapointer back to the completion callback on a runtime thread. Inaimux-ffi/tests/abi_primitives.rs,async_calls_fan_out_from_another_runtime_and_own_the_inputsandaccepted_async_calls_report_dispatch_errors_and_abort_oncepassed the address of a localSenderthere and relied on the test thread outliving every callback. Those tests only wait for the completions they expect, so after the last one they drop both channel endpoints, and the runtime thread can still be inside the send that delivered it.std::sync::mpscpublishes a message before it notifies the receiver. The channel write stores the message, marks the slot ready, and only then wakes a receiver. A receiver woken between those two steps can return fromrecv_timeout, let the test run to the end, and free the channel, which releases the heap-allocated counter. The runtime thread then enters the notification path with a pointer to freed memory and follows stale wake-list links.A fault there has no Rust panic line: it is a plain SIGSEGV on x86_64 Linux, which matches the intermittent CI failure this fixes.
Fix
Every submission now hands over an owned sender. The test builds
Box::into_raw(Box::new(send.clone())).cast(), and the callback's first statement takes it back withBox::from_raw, holding it untilsendreturns. The channel therefore cannot be freed while a callback is running.A rejected submission returns an owned error and never invokes the callback. That is the contract in
aimux-ffi/include/aimux.h, and it holds inaimux-ffi/src/abi.rsbecause every failure path returns before the task is spawned. The test therefore reclaims the sender itself when submission returns a non-null error, so each context is freed exactly once.Scope
Two files change, and no production code:
aimux-ffi/tests/abi_primitives.rs: the completion callbacks in the two async tests take an owned sender instead of a pointer to a local one, which is the fix for [bug] abi_primitives crashes with SIGSEGV in the Rust core job #541.scripts/tests/test_check_native_artifact.py:test_macho_dylib_exports_end_to_endnow expects 16 header exports instead of 14. perf(core,ops,ffi): RFC-0049/0050 non-breaking hot-path, wire dispatch, and async C ABI #538 addedaimux_call_asyncandaimux_stream_async, so the header declares 16 and the checker reports 16. The old count failed on both the assertion and the reported message on every macOS leg, which is whyaimux-ffi (aarch64-apple-darwin)is red on master as well.The expected export count stays a literal. The list it is compared with already comes from
header_exports(ROOT), so deriving the expectation from the same call would compare the list with itself and could not catch a missing or extra export. The other numbers in that file are macOS deployment targets and are untouched.Why this is not reproduced locally
The window sits between two adjacent statements in the standard library, so it needs preemption at exactly that point; it has only been observed on a loaded x86_64 Linux CI runner. The machine available here is aarch64 macOS with stable toolchains only, which rules out AddressSanitizer, Miri and a Linux run, and CI is not rerun for diagnosis. The change removes the precondition instead of narrowing the timing: after it, no path can drop the channel while a callback holds it.
Fixes #541
Verification
cargo test -p aimux-ffi --test abi_primitives: 12 passedcargo test -p aimux-ffi: 24 passed across the lib and integration targetscargo fmt --all -- --checkcargo clippy -p aimux-ffi --all-targets -- -D warningsRUSTDOCFLAGS='-D warnings' cargo doc -p aimux-ffi --no-deps