ffi: fix sandlock_spawn failure under multi-threaded callers with restricted seccomp (#47)#49
Open
congwang-mk wants to merge 3 commits into
Open
ffi: fix sandlock_spawn failure under multi-threaded callers with restricted seccomp (#47)#49congwang-mk wants to merge 3 commits into
congwang-mk wants to merge 3 commits into
Conversation
Signed-off-by: Cong Wang <cwang@multikernel.io>
Signed-off-by: Cong Wang <cwang@multikernel.io>
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.
Summary
Fixes #47.
Sandbox(policy).run([...])was failing withsandlock_spawn failedwhen called from a multi-threaded Python host (uvicorn/asyncio) on a Kubernetes pod with theRuntimeDefaultseccomp profile.Root cause: every FFI entry point built a fresh
tokio::runtime::Runtime::new(), which isnew_multi_thread()and spawns worker threads eagerly viapthread_create. Kubernetes'RuntimeDefaultblocksclone3withENOSYS, and the multi-thread builder's eager-spawn path returnedErrbefore glibc's fallback could help, surfacing to the caller as a NULL handle.Fix: switch FFI entry points to a per-thread cached
current_threadTokio runtime, which spawns no threads at construction. Three runtime shapes:sandlock_runand the rest of the one-shot entry pointscurrent_threadsandlock_create(live handle)startandwait; one persistent worker is unavoidable heresandlock_create_for_run(new)current_threadSandbox.run()isstartthenwaitback-to-back, so suspension across the gap is fine and avoids the one worker threadsandlock_createwould have spawnedSandbox.run()is wired tosandlock_create_for_run. The seccomp supervisor's blockingSECCOMP_IOCTL_NOTIF_RECVthread is left as-is: it spawns throughpthread_create, which the reporter's diagnostic confirms works in their environment ("new_thread": "ok").Test plan
cargo build --releaseclean on devpytest python/tests/249/249 pass, including the newTestSandlockRunCAbiMultiThreadedandTestSandboxRunMultiThreadedregression testsRuntimeDefaultseccomp (the only environment where the original failure was empirically observable)Note: the new tests are not red-on-pristine on an unrestricted dev box because glibc's
clone3 → clone(2)fallback masks the failure mode locally. The docstring onTestSandlockRunCAbiMultiThreadeddocuments this honestly.