policy_fn: join the callback thread before the sandbox returns - #205
Merged
Conversation
The policy-fn thread was detached and only exited once every clone of its event sender had dropped, which happens whenever the last Arc<SupervisorCtx> goes away, not when run() returns. Non-gated events are fire-and-forget, so the thread could still be draining them after the caller had its result. Through the Python binding that is a use-after-free: the Sandbox temporary in test_passthrough_no_modification is collected as soon as run() returns, which frees the ctypes trampoline the thread is about to call. CI caught it as a SIGSEGV on a thread with no Python frame. Give the thread an owner. PolicyFnWorker lives in the sandbox Runtime next to the other supervisor handles and its Drop sends an in-band Shutdown message and joins; an in-band message rather than channel closure because the supervisor's sender clones outlive an abort(). Every entry point goes through the existing teardown, so the guarantee is not per-path. Signed-off-by: Cong Wang <cwang@multikernel.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CI segfaulted in the Python suite right after
test_passthrough_no_modificationpassed, on a thread with no Python frame.The
sandlock-policy-fnthread was detached and only exited once every clone of its event sender dropped, which happens when the lastArc<SupervisorCtx>goes away, not whenrun()returns. Non-gated events are fire-and-forget, so the thread could still be draining them after the caller had its result. Through the Python binding that is a use-after-free: theSandboxtemporary in the test is collected as soon asrun()returns, freeing the ctypes trampoline the thread is about to call.Fix
Give the thread an owner.
PolicyFnWorkerlives in the sandboxRuntimenext to the other supervisor handles; itsDropsends an in-bandShutdownmessage and joins. In-band rather than channel closure because the supervisor's sender clones outliveabort(). Every entry point goes through the existing teardown, so the guarantee is not per-path and the Python side needs no change.Tests
worker_drop_waits_for_queued_callbacks: queues an event whose callback sleeps, drops the worker, asserts the callback ran (fails without the join).cargo test --release --lib: all pass. Python suite: 410 passed;test_policy_fn.pyrun 5x without a crash.