Repository navigation
Fix capture hanging on Linux while a sandbox process runs - #1
Merged
Merged
Conversation
On Linux, Foundation's Process notices that its child exited when every copy of a socket it gives the child has been closed. That socket isn't close-on-exec, and runAttached passed every such descriptor to its child. If another thread was starting a Process at that moment, the sandbox process (or anything it left running) kept a copy, and that thread's capture waited for it to exit. CI hit this when the timeout test ran next to the runAttached grace-period test: the killed `sleep` was gone in 3.5 s, but capture returned only when the orphaned `sleep 30` from the other test exited, at 30 s. runAttached now closes every descriptor past stdio in the child on Linux, except the ones registered to be inherited (the runner lock, registered by FileLock.tryAcquire(inheritable:)). It uses posix_spawn_file_actions_addclosefrom_np when glibc has it, so descriptors opened by other threads at the same time are closed too. Also, a child inherits the signal mask of the thread that spawns it, and libdispatch's worker threads on Linux block nearly every signal. capture's timeout SIGTERM never got through, and only the SIGKILL 3 s later stopped the child. capture and stream now start the child with an empty mask on Linux (Foundation already does this on macOS), and runAttached asks posix_spawn for an empty mask on both platforms. The runAttached tests move to a serialized suite, because runAttached swaps process-wide signal handlers while it runs.
Kernel-Hunter
added a commit
that referenced
this pull request
Oct 10, 2026
The test closed a server to get a free port and assumed nothing else would listen there. With tests running in parallel another test server could take the port, so CONNECT succeeded and the test saw 200 instead of 502 (seen on the macOS runner in PR #1). Bind a socket without listening and keep it open for the whole test instead.
Kernel-Hunter
added a commit
that referenced
this pull request
Oct 10, 2026
Windows has no signal masks or posix_spawn, so start() just runs the process there, and the POSIX-only runAttached tests are skipped.
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.
CI run 37959076163 failed on Linux in "capture stops a program that runs past its timeout":
capture("/bin/sleep", ["30"], timeout: 0.5)returned after 30.0 s.timedOutwas set, so the watchdog fired. The wait after the kill is what hung.Cause
There are two Linux-only problems. I reproduced both in
swift:6.2under Docker.The 30 s comes from a leaked socket. swift-corelibs-foundation's
Processlearns that its child exited when every copy of a socketpair end it gives the child is closed, and that descriptor isn't close-on-exec.runAttachedspawns withposix_spawnpand no file actions, so its child got every descriptor without O_CLOEXEC. That includes the socket, if another thread is insideProcess.run()at that moment. In CI the other thread was the timeout test, which runs next to therunAttachedgrace-period test. That test kills itsshbut leaves an orphanedsleep 30. The orphan held the socket, sowaitUntilExitwaited the full 30 s. A stress loop shows it on main every time: captures of/bin/truewhile another thread keeps callingrunAttachedonsleep 30 & exit 0give a worst capture of 30.002 s. On this branch, 26,666 captures had a worst time of 5 ms.The other 3 s comes from the signal mask. A child inherits the signal mask of the thread that spawns it, and libdispatch's worker threads on Linux block almost every signal, SIGTERM included (
SigBlk: fffffffe3bfbea27on thesleepchild). The timeout's SIGTERM never landed, and the child only died to the SIGKILL 3 s later. That's why the test took 3.5 s on every Linux run, including the ones that passed.Fix
runAttachedcloses every descriptor past stdio in the child, except those registered withProcessRunner.inherit. That's the runner lock:FileLock.tryAcquire(inheritable: true)registers it andreleaseunregisters it. When glibc hasposix_spawn_file_actions_addclosefrom_np(2.34+, looked up withdlsym), descriptors that other threads open at the same moment get closed too. Without it, the child gets whatever/proc/self/fdlisted closed. The lock may have a different number in the child, and nothing depends on the number.captureandstreamstart the child with an empty signal mask on Linux. corelibsProcesshas no hook for spawn attributes, so the calling thread's mask is cleared aroundrun(). Foundation on macOS already does this.runAttachedsetsPOSIX_SPAWN_SETSIGMASKwith an empty mask on both platforms. A caller on a thread with no signals blocked sees no change.Otherwise macOS behaves as before. Descriptor handling is unchanged there, and so are
captureandstream.Tests
The new
Child processessuite is serialized, becauserunAttachedswaps process-wide signal handlers while it runs.capture,streamandrunAttachedstarted from a thread that blocks SIGTERM still die from SIGTERM. The timeout path ends with status 15, not 9.runAttachedchild gets the inheritable lock but not an unrelated pipe.The new tests fail on main under Docker and pass with the fix. On macOS, main already passes the
captureandstreamones; therunAttachedmask test fails there too. The full suite passes 3 times inswift:6.2(212 tests) and on macOS (211; the descriptor test is Linux-only). The original timeout test now takes 0.51 s on Linux instead of 3.5 s.