gvisor icon indicating copy to clipboard operation
gvisor copied to clipboard

containerd-shim-runsc-v1: every container start is delayed by exactly 5 s (shim waits for runsc create pipes that the sandbox keeps open)

Open svr8 opened this issue 3 weeks ago • 3 comments

Environment

  • gVisor release-20261102.0 (also release-20261019.0), runsc and containerd-shim-runsc-v1 from the release tarball (gvisor-bin/ helpers installed next to runsc)
  • containerd 2.2.1 (Ubuntu 24.04 package) and 1.7.28 (upstream tarball): same result
  • Ubuntu 24.04, kernel 6.8 and 7.0; reproduced on a GCP VM (kvm and systrap), a Vultr VM (systrap) and Vultr bare metal (kvm)
  • runsc config via --runtime-config-path: only [runsc_config] platform = ...

What happens ctr run -d --runtime io.containerd.runsc.v1 --runtime-config-path <toml> docker.io/library/alpine:3.20 c1 sleep 60 returns after 5.1 s every time. runc returns in 0.1 s. Running runsc create/start/run by hand from a terminal is instant (0.06 s).

Evidence

  • With --debug-log, runsc create exits about 10 ms after it starts and the sandbox then sits idle waiting for start. The shim calls runsc start exactly 5.00 s later.
  • A SIGQUIT goroutine dump of the shim during the gap shows it parked in runsccmd.(*Runsc).Create -> cmdOutput waiting on the command's output pipes, after runsc create has already exited.
  • pkg/shim/v1/runsccmd sets cmd.WaitDelay = 5s (master source; I did not diff the release tarball's source): Wait force-closes the pipes that long after the command exits. The sandbox process forked by runsc create inherits create's stdout/stderr pipes and keeps them open, so the pipes never reach EOF and the shim always waits out the full delay.
  • Ruled out (all still 5.1 s): platform (systrap, kvm), debug logging on/off, overlay2 and network modes, cgroup settings (ignore-cgroups), containerd version, release, VM vs bare metal.

Workaround that confirms the cause: point binary_name in the shim's runsc.toml at a wrapper that runs runsc create with stdout and stderr redirected to a file (replayed after, exit code kept) and execs runsc for every other command. Start drops from 5.09 s to 0.02 s on all three setups; exec, kill and delete still work.

# /etc/containerd/runsc.toml
binary_name = "/usr/local/bin/runsc-nopipe"
# runsc-nopipe
case " $* " in *" create "*) f=$(mktemp); /usr/local/bin/runsc "$@" </dev/null >"$f" 2>&1; rc=$?; cat "$f"; rm -f "$f"; exit $rc;; esac
exec /usr/local/bin/runsc "$@"

Suggested fix: after the sandbox has booted, runsc create should not leave its stdout/stderr pipes open in the sandbox/gofer children (redirect them to /dev/null or the log file in the child), or the shim should not wait on them for the sandbox child.

Related note (not a bug): ctr --memory-limit alone did not stop a process from exceeding the limit on a host with swap, because only memory.max is set; limits held once the OCI spec also set memory.swap equal to the limit.

svr8 avatar Nov 11 '26 07:11 svr8

Hi, I'd be interested in working on this issue. I’d like to investigate the runsc create process lifecycle and how stdout/stderr file descriptors are inherited by the sandbox and gofer processes.

I can work on identifying a safe fix that prevents the shim from waiting for the full WaitDelay, while preserving command output, exit status, and existing process behavior. I’d also add regression tests to verify that container startup is no longer delayed.

Would the maintainers be open to me investigating this and submitting a PR?

AbarnaaSree avatar Nov 11 '26 17:11 AbarnaaSree

I reproduced this on release-20261102.0 (Ubuntu 24.04 host, containerd 2.2.1, systrap). ctr run -d takes 5.07s every time. A few more data points narrow it down:

  • Adding --annotation io.kubernetes.cri.container-type=sandbox to the same ctr run -d brings it to 0.05s.
  • In the foreground, ctr run --rm ... sh -c 'echo early; sleep 7; echo late' prints nothing. The container's output is lost, not only delayed.
  • On release-20260907.0, before WaitDelay was added in 9d1aa3e7, runsc create never returns (I stopped it at 60s). WaitDelay turned a hang into the 5s delay.

The cause is in the shim's idea of which container is the sandbox. service.go sets p.Sandbox only for an explicit sandbox annotation. runsc uses specutils.IsRootContainer, which also treats a spec with no annotation as root, so runsc create boots a sandbox and donates its own stdio as the container's stdio (--stdio-fds). Because p.Sandbox is false, Init.Create leaves opts.IO unset, Runsc.Create runs create through cmdOutput, and the shim's internal output buffer becomes the container's stdout and stderr. The real IO goes to runsc start instead, which ignores it for a root container.

You can see it in /proc during the gap: the Sentry's fds 254 and 255 point at the same pipe as the shim's cmdOutput buffer, after runsc create has exited.

So the sandbox keeping those fds open is intended, and closing them in the child would hide the output instead of fixing it. The runsc-nopipe wrapper has the same side effect: the container's stdout ends up in the temp file. The fix is to use IsRootContainer for p.Sandbox, which also matches utils.IsSandbox in the same package. With that change, start takes 0.05s, foreground output comes back, and exec, kill and delete still work. I'll send a PR.

bitflicker64 avatar Nov 12 '26 04:11 bitflicker64

Thanks for the report!

This is not a regression. The bug is old.

A recent PR https://github.com/google/gvisor/pull/14549 only changed it from an endless hang into the 5s

cc: @fvoznika

milantracy avatar Nov 15 '26 12:11 milantracy