Skip to content

Fix capture hanging on Linux while a sandbox process runs - #1

Merged
Kernel-Hunter merged 2 commits into
mainfrom
fix/linux-child-signals-and-fds
Oct 10, 2026
Merged

Kernel-Hunter merged 2 commits into
mainfrom
fix/linux-child-signals-and-fds

Conversation

@Kernel-Hunter

Copy link
Copy Markdown
Owner

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. timedOut was 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.2 under Docker.

The 30 s comes from a leaked socket. swift-corelibs-foundation's Process learns 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. runAttached spawns with posix_spawnp and no file actions, so its child got every descriptor without O_CLOEXEC. That includes the socket, if another thread is inside Process.run() at that moment. In CI the other thread was the timeout test, which runs next to the runAttached grace-period test. That test kills its sh but leaves an orphaned sleep 30. The orphan held the socket, so waitUntilExit waited the full 30 s. A stress loop shows it on main every time: captures of /bin/true while another thread keeps calling runAttached on sleep 30 & exit 0 give 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: fffffffe3bfbea27 on the sleep child). 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

  • On Linux, runAttached closes every descriptor past stdio in the child, except those registered with ProcessRunner.inherit. That's the runner lock: FileLock.tryAcquire(inheritable: true) registers it and release unregisters it. When glibc has posix_spawn_file_actions_addclosefrom_np (2.34+, looked up with dlsym), descriptors that other threads open at the same moment get closed too. Without it, the child gets whatever /proc/self/fd listed closed. The lock may have a different number in the child, and nothing depends on the number.
  • capture and stream start the child with an empty signal mask on Linux. corelibs Process has no hook for spawn attributes, so the calling thread's mask is cleared around run(). Foundation on macOS already does this.
  • runAttached sets POSIX_SPAWN_SETSIGMASK with 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 capture and stream.

Tests

The new Child processes suite is serialized, because runAttached swaps process-wide signal handlers while it runs.

  • Children of capture, stream and runAttached started from a thread that blocks SIGTERM still die from SIGTERM. The timeout path ends with status 15, not 9.
  • On Linux, a runAttached child gets the inheritable lock but not an unrelated pipe.
  • The existing grace-period test moved here from QAPass unchanged.

The new tests fail on main under Docker and pass with the fix. On macOS, main already passes the capture and stream ones; the runAttached mask test fails there too. The full suite passes 3 times in swift: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.

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
Kernel-Hunter merged commit 9d68118 into main Oct 10, 2026
3 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant