← Back to Pull requests

runc-v2: release terminal I/O on delete #14312

Closed
unknown closed this pull request without merging on Oct 17, 2026 01:39 UTC
View on GitHub
unknown unknown commented on Oct 07, 2026 23:21 UTC
Author

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

delete closed the process's I/O handles only when io was set. A terminal process keeps its I/O in two other fields, closers and console, and leaves io nil. 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 Close could leave the copier asleep, and a late shutdown could remove a descriptor number that a newer console had already taken.

The fix

delete now shuts the console down, waits for the copiers, and closes closers, console and io on every path. The console that CopyConsole returns owns its own teardown and does it once, so every caller releases through the same two methods.

Six new tests fail on main and 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.

---

  • A terminal exec keeps its I/O alive in the shim after delete whenever the client finishes before calling CloseIO: 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.

  • Cause: delete released the closers inside if io != nil. A terminal process sets io on the pipe path only; its stdin writer sits in closers and its master in console, so both stayed open. Shutdown, deregistration and close of the epoll console were also performed by hand at each site, so a bare Close could leave a copier asleep and a late shutdown could deregister a fd number that a newer console had taken.

  • Fix: delete shuts the console down, drains, then closes closers, console and io on every path. CopyConsole returns a console that owns its teardown: shutdown and deregistration happen once while the fd is open, Close then closes the master and the stdin FIFO reader once, and the copier, the setup-error path, ShutdownConsole and both deletes go through it.

  • Also fixed: the received master is closed when registering it fails, the stdout FIFO writer when opening its reader fails, and a writer parked on the master is woken after the close.

  • Six regression tests fail on main and 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 CloseIO or delete can let go of it

    1. openStdin opens the stdin FIFO for writing, so the console's copier reads a FIFO that stays open while the client is away.
    2. CopyConsole starts a goroutine that copies the FIFO into the console and, at EOF, shuts down and closes the console master and the FIFO reader.
    3. delete closed closers inside if io != nil. io is 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 (setExited had deregistered it from the epoller, but nothing closed it).
    4. A client that calls CloseIO before it exits is fine. A client that exits first leaves the master, the FIFO handles and the copier behind until the collector finalizes the dropped execProcess, whose os.File finalizers 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, shutdownAndDrain then closeIO. ShutdownConsole runs first, since a process deleted from the created state never ran setExited and its copier would otherwise sleep through the 10 s drain; then closers, console and io are closed regardless of io.

  • CopyConsole returns closingConsole, which embeds the EpollConsole and holds the epoller and the stdin FIFO reader. shutdown deregisters it exactly once, while its fd is still open; Close runs shutdown, 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, ShutdownConsole and delete all release through these two, so a late ShutdownConsole is a no-op and the epoller is never asked to drop a fd number that a newer console may own.

  • CopyConsole closes the received master when epoller.Add fails and the stdout FIFO writer when opening its reader fails.
  • Each test fails on main with 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 after Close of 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) that setExited or delete would, and checks that B still delivers output. Also on linux/arm64 with Go 1.27.1: go test -race -count=5 for process and runc, the FIFO test 40 times and the reuse test 20 times in a row, go vet ./cmd/containerd-shim-runc-v2/..., GOOS=darwin go vet for process, 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, from ls -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

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: dmcgowan Review duration: 13097m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%