Skip to content

libct: do not hang on a half-dead "runc init" - #5431

Merged
kolyshkin merged 2 commits into
opencontainers:mainfrom
kolyshkin:fix-5087-init-hang
Oct 31, 2026
Merged

kolyshkin merged 2 commits into
opencontainers:mainfrom
kolyshkin:fix-5087-init-hang

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5087.

The problem

SCMP_ACT_KILL is SECCOMP_RET_KILL_THREAD: the kernel does do_exit(SIGSYS) on the thread that made the syscall, and on that thread only. runc init locks its main goroutine to the initial OS thread, which is the thread group leader, so a syscall blocked this way kills the leader while the rest of the Go runtime threads live on.

What is left is a process that looks dead and is not:

  • the leader is a zombie, but release_task() for it is delayed until the last thread exits, so /proc/$pid/stat says Z;

  • exit_files() drops only the leader's reference to the shared files_struct, so the child ends of the sync socket and of the exec fifo stay open — the parent never sees an EOF;

  • exit_notify() defers do_notify_parent() until the thread group is empty, so there is no SIGCHLD, wait(2) does not return, and a pidfd never becomes readable — I checked the latter explicitly, with and without PIDFD_THREAD:

    leader state=Z
    flags=0x0   n=0 revents=0x0
    flags=0x80  n=0 revents=0x0   /* PIDFD_THREAD */
    

The parent therefore waits forever. Which wait it gets stuck in depends on where init dies; with write blocked and no --debug, init dies writing to the exec fifo (seccomp is applied just before that), and runc run hangs in waitForFifoReady. The reporter hit the same thing in ReadPacket on the sync socket.

This also means the pidfd fast path added for the fifo wait cannot detect this case at all — /proc/$pid/stat is the only signal that reports a zombie thread group leader.

The fix

Add waitForInitWrite(fd, pid): poll(2) with a 100 ms timeout, and on timeout check whether init went away via /proc. Use it for both places where the parent waits on init — reading the sync socket and waiting for the exec fifo — replacing the three fifo-specific wait helpers. When init is found dead, the fd is polled once more with no timeout: init may have written something (and died, or execve'd a short-lived container process) right after the previous poll timed out, and that must still be read.

Two more things were needed to make the failure actually terminate:

  • once a dead init is detected, kill its leftover threads. They keep the container's stdio descriptors open, and whoever inherited those (runc run's own IO copying goroutines, or bats) would then wait for an EOF just as long.
  • drop the p.wait() in the setnsProcess.start error path — a half-dead child is never reaped, so that wait4(2) never returns. The deferred error handler terminates and reaps it anyway, and SIGKILL is group-wide.

@kolyshkin kolyshkin added this to the 1.6.0-rc.1 milestone Aug 29, 2026
@kolyshkin
kolyshkin marked this pull request as draft August 29, 2026 20:51
@kolyshkin
kolyshkin force-pushed the fix-5087-init-hang branch 2 times, most recently from 993c808 to 52ced03 Compare August 29, 2026 21:54
@kolyshkin
kolyshkin marked this pull request as ready for review August 30, 2026 06:27
@kolyshkin
kolyshkin requested review from cyphar and lifubang and removed request for cyphar September 1, 2026 00:17
@kolyshkin

Copy link
Copy Markdown
Contributor Author

This seems like a real bug fix and the code looks better than before I think; @opencontainers/runc-maintainers PTAL

@kolyshkin

Copy link
Copy Markdown
Contributor Author

@opencontainers/runc-maintainers PTAL

@kolyshkin
kolyshkin requested a review from rata September 9, 2026 03:44
@kolyshkin
kolyshkin force-pushed the fix-5087-init-hang branch 5 times, most recently from 8f26d31 to a81aa51 Compare September 12, 2026 00:27
@kolyshkin

Copy link
Copy Markdown
Contributor Author

Self-reviewed and made some tiny improvements. This is ready for review/merge, PTAL @opencontainers/runc-maintainers @cyphar @rata @lifubang. Merging this will fix the issue and unblock #5414.

I can also drop the unit test as it's too complicated and probably doesn't add much value.

@kolyshkin

Copy link
Copy Markdown
Contributor Author

This is probably one of the last things that stand in a way of 1.6.0-rc.1 release (and I want it to be included into rc.1 to test for any possible regressions). PTAL @opencontainers/runc-maintainers.

@kolyshkin
kolyshkin force-pushed the fix-5087-init-hang branch 2 times, most recently from d1b134e to b7d7b90 Compare September 26, 2026 20:04
@kolyshkin

Copy link
Copy Markdown
Contributor Author

I am more or less ready for 1.6.0-rc.1, just wanted this one in. PTAL @lifubang @cyphar @AkihiroSuda

@kolyshkin kolyshkin mentioned this pull request Sep 26, 2026
@kolyshkin

Copy link
Copy Markdown
Contributor Author

@lifubang @AkihiroSuda ptal

@kolyshkin
kolyshkin force-pushed the fix-5087-init-hang branch 2 times, most recently from 63cf96b to 220dccf Compare October 2, 2026 16:59
@kolyshkin

Copy link
Copy Markdown
Contributor Author

@rata PTAL 🙏🏻

@kolyshkin

Copy link
Copy Markdown
Contributor Author

Did another self-review and rebase, simplified code and tests a bit. PTAL @rata @lifubang @AkihiroSuda (I would like that in 1.6.0-rc.2)

@kolyshkin
kolyshkin force-pushed the fix-5087-init-hang branch 2 times, most recently from 118d1cf to 86e3f55 Compare October 25, 2026 01:21
Seccomp's SCMP_ACT_KILL is SECCOMP_RET_KILL_THREAD: it kills the thread
that performed the syscall, not the whole thread group. As "runc init"
locks its main goroutine to the thread group leader, killing that thread
leaves a zombie leader whose remaining Go runtime threads are alive,
keeping the shared descriptor table -- and thus the child ends of the
sync socket and of the exec fifo -- open. The parent therefore never sees
an EOF, and since the thread group is not dead, neither wait(2) nor a
pidfd report anything either, so the parent waits forever.

Add waitForInitWrite, which polls the descriptor with a timeout and, when
nothing arrives, consults /proc/[pid]/stat -- the only thing that reports
a zombie thread group leader. Use it both for reading the sync socket and
for waiting on the exec fifo; the latter replaces the pidfd fast path,
which can not detect this case at all.

Kill and reap the leftover threads once a dead init is detected while
waiting for the exec fifo. They keep the container's stdio descriptors
open, so whoever inherited those would wait for an EOF just as long, and
they keep its cgroup populated, so destroying the container would fail.

Make nonChildProcess.terminate send SIGKILL rather than fail, so that
killDeadInit can use terminate for any init, be it our child ("runc
run") or not ("runc start"). In the latter case, init is only killed,
as reaping it is up to its parent.

Finally, do not wait(2) for the child in the setnsProcess.start error
path: a half-dead child is never reaped, and the deferred error handler
terminates and reaps it anyway.

Fixes opencontainers#5087

Assisted-by: Claude Opus 5
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
SCMP_ACT_KILL kills a single thread, so "runc init" is left as a zombie
thread group leader surrounded by live threads that keep its descriptors
open. Check that runc reports that instead of hanging.

The container gets its own stdio rather than the one bats captures: were
this to regress, the leftover threads would keep bats' end open, and the
suite would hang instead of reporting a failure.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Assisted-by: Claude Opus 5
@kolyshkin
kolyshkin added this pull request to the merge queue Oct 31, 2026
Merged via the queue into opencontainers:main with commit 1a2d97e Oct 31, 2026
96 checks passed
kolyshkin added a commit to kolyshkin/crun that referenced this pull request Oct 31, 2026
The new runc test "runc run [seccomp SCMP_ACT_KILL kills init]"
(opencontainers/runc#5431) uses a seccomp filter which kills the
process on write(2), and expects runc to report that its init died.

crun does not call write(2) after loading the seccomp filter, so the
container (running /bin/true) just succeeds. Skip the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/crun that referenced this pull request Oct 31, 2026
The new runc test "runc run [seccomp SCMP_ACT_KILL kills init]"
(opencontainers/runc#5431) uses a seccomp filter which kills the
process on write(2), and expects runc to report that its init died.

crun does not call write(2) after loading the seccomp filter, so the
container (running /bin/true) just succeeds. Skip the test.

Assisted-by: Claude Code (claude-opus-5-5)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/crun that referenced this pull request Oct 31, 2026
The new runc test "runc run [seccomp SCMP_ACT_KILL kills init]"
(opencontainers/runc#5431) uses a seccomp filter which kills the
process on write(2), and expects runc to report that its init died.

crun does not call write(2) after loading the seccomp filter, so the
container (running /bin/true) just succeeds. Skip the test.

Assisted-by: Claude Code (claude-opus-5-5)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
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.

Indefinite parent hang in ReadPacket upon abnormal child exit after procRun

2 participants