runc-v2: release terminal I/O on delete #14312
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #14312"
general
weight: 0.35
dmcgowan
The problem
When a process runs in a container with a terminal, the shim holds the terminal's master end and pumps bytes between it and the client's named pipes. When that process is deleted, the shim is supposed to let all of that go.
For terminal processes the release was skipped. The pty master, the shim's handles on the stdin pipe, and the goroutine copying stdin stayed open after
delete. Go's garbage collector closed them later, up to two minutes later. When the client kept its own end of the stdin pipe open, as a daemon with an attached session does, they stayed open for good.Each leaked exec costs a pty, several descriptors, and a goroutine inside a long-lived process. On a busy host that is a steady climb toward the descriptor and pty limits.
The cause
deleteclosed the process's I/O handles only wheniowas set. A terminal process keeps its I/O in two other fields,closersandconsole, and leavesionil. So the closing code was skipped on exactly the path that has a pty to close.The steps that retire a console, wake its reader, remove it from epoll, close it, were also repeated by hand at each call site. A bare
Closecould leave the copier asleep, and a late shutdown could remove a descriptor number that a newer console had already taken.The fix
deletenow shuts the console down, waits for the copiers, and closes closers, console and io on every path. The console thatCopyConsolereturns owns its own teardown and does it once, so every caller releases through the same two methods.Six new tests fail on
mainand pass here. Load runs on three hosts show the unpatched shim climbing to about 40 descriptors and holding the last exec's pty and pipes, and this shim staying at baseline.Why it is worth taking: this is the default Linux runtime, every terminal exec hits the leak, and the change stays inside the shim's delete path and one console type.
---
deletewhenever the client finishes before callingCloseIO: the shim's own stdin FIFO writer, the console's FIFO reader and copier goroutine, and the pty master stay until a garbage collection finalizes the dropped process object (the runtime forces one within two minutes of the last), and for as long as the client still holds its own writer on that FIFO, not even that releases them.deletereleased the closers insideif io != nil. A terminal process setsioon the pipe path only; its stdin writer sits inclosersand its master inconsole, so both stayed open. Shutdown, deregistration and close of the epoll console were also performed by hand at each site, so a bareClosecould leave a copier asleep and a late shutdown could deregister a fd number that a newer console had taken.deleteshuts the console down, drains, then closes closers, console and io on every path.CopyConsolereturns a console that owns its teardown: shutdown and deregistration happen once while the fd is open,Closethen closes the master and the stdin FIFO reader once, and the copier, the setup-error path,ShutdownConsoleand both deletes go through it.mainand pass here, and a 4000-exec run on a separate instance shows the unmodified shim climbing between collections and holding the last exec's handles while this shim stays at baseline (numbers below).The shim keeps the stdin FIFO open on purpose, so only
CloseIOordeletecan let go of it1.
openStdinopens the stdin FIFO for writing, so the console's copier reads a FIFO that stays open while the client is away.2.
CopyConsolestarts a goroutine that copies the FIFO into the console and, at EOF, shuts down and closes the console master and the FIFO reader.3.
deleteclosedclosersinsideif io != nil.iois set on the pipe path only, so on the terminal path the writer stayed open, the copier stayed blocked on the FIFO, and the master stayed open (setExitedhad deregistered it from the epoller, but nothing closed it).4. A client that calls
CloseIObefore it exits is fine. A client that exits first leaves the master, the FIFO handles and the copier behind until the collector finalizes the droppedexecProcess, whoseos.Filefinalizers close the shim's writer and let the copier see EOF; the runtime forces a collection two minutes after the previous one, so a quiet shim churns them on that cadence. A client that keeps its own writer on the FIFO, as a daemon holding an attach does, defeats even that: the copier never sees EOF.Found in containerd/nerdbox, whose shim derives from this code (containerd/nerdbox#300). That review asked for the fix to land here first.
Release on every path, and release through one owner
execProcess.delete,Init.delete: one shared sequence,shutdownAndDrainthencloseIO.ShutdownConsoleruns first, since a process deleted from the created state never ransetExitedand its copier would otherwise sleep through the 10 s drain; then closers, console and io are closed regardless ofio.CopyConsolereturnsclosingConsole, which embeds theEpollConsoleand holds the epoller and the stdin FIFO reader.shutdownderegisters it exactly once, while its fd is still open;Closerunsshutdown, closes the master, wakes a writer that was parked on it, and closes the FIFO reader, which ends a copier blocked reading stdin whether or not the client still holds its own writer. The stdin copier, the setup-error defer,ShutdownConsoleanddeleteall release through these two, so a lateShutdownConsoleis a no-op and the epoller is never asked to drop a fd number that a newer console may own.CopyConsolecloses the received master whenepoller.Addfails and the stdout FIFO writer when opening its reader fails.Each test fails on
mainwith the message shown and passes with this change| Test | On
main||---|---|
|
TestExecDeleteReleasesTerminalIO|stdin FIFO writer closed 0 times, want 1||
TestExecDeleteShutsConsoleDownBeforeClosing|console operations = "", want shutdown,close||
TestCopyConsoleReleasesConsoleOnSetupError|console master closed 0 times after a setup error, want 1||
TestConsoleCloseEndsTheCopier| the stdout copier stays asleep afterCloseof a master that lives outside Go's poller, as the one from the runc console socket does ||
TestCopyConsoleSetupErrorReleasesStdinFifo|descriptors on the stdin FIFO went 2 -> 4 across the failed setup: CopyConsole leaked its reader||
TestShutdownAfterCloseLeavesAReusedFdAlone|B's copier delivered nothing: the stale shutdown deregistered the reused fd|The last test closes console A, lets console B take A's fd number, issues the late
ShutdownConsole(A)thatsetExitedordeletewould, and checks that B still delivers output. Also on linux/arm64 with Go 1.27.1:go test -race -count=5forprocessandrunc, the FIFO test 40 times and the reuse test 20 times in a row,go vet ./cmd/containerd-shim-runc-v2/...,GOOS=darwin go vetforprocess,go build ./cmd/containerd-shim-runc-v2/.Load run: 500 terminal execs per scenario on a separate c7g.2xlarge, shim descriptors and goroutines after each 100
Isolated containerd on Ubuntu 26.04 arm64, kernel 7.0, runc 1.4.0, busybox, each exec
sh -c 'echo ready; sleep 0.4; echo bye'with a terminal, deleted after exit; goroutines from the shim's SIGUSR1 dump; baseline is the idle shim; "settled" is one second after the last delete. Counts are all of the shim's descriptors and drift by a few with ttrpc connections; the "at 500" column names, fromls -l /proc/<pid>/fd, only the descriptors that belong to an exec. The run was made before the console's shutdown became idempotent; that change touches no release path the run measures.| client behaviour | shim | baseline | peak during 500 | at 500 | settled |
|---|---|---|---|---|---|
| keeps stdin open, no
CloseIO|main| 13 fds, 15 gor. | 30 fds, 18 gor. | 19 fds: pty master + 4 stdin FIFO handles of the last exec | 13, 15 || keeps stdin open, no
CloseIO| this PR (commit 1) | 14, 15 | 15, 15 | 14, 15 | 13, 15 || closes stdin after delete |
main| 15, 15 | 37 fds, 19 gor. | 24 fds: 2 masters + 8 FIFO handles | 13, 15 || closes stdin after delete | this PR (commit 1) | 15, 15 | 17, 15 | 17, 15 | 13, 15 |
| no stdin stream |
main| 13, 15 | 17 fds | 17 fds: 2 masters | 13, 15 || no stdin stream | this PR (commit 1) | 14, 15 | 15, 15 | 13, 15 | 13, 15 |
| calls
CloseIO| both | 13–15, 15 | 17, 15 | 13–17, 15 | 13, 15 |Under steady traffic the collector reclaims
main's leftovers, so the settled counts match; the peaks and the handles still held after the last delete are the leak. A second run against the final branch is in this comment.🤖 Generated with Claude Code
Agent: claude-fable-5-1 | Claude Code 2.1.284 | session 5015 835e20da-cd9c-47a0-8339-6b1502f4c34b