Repository navigation
Reap nft process on errors - #53517
Conversation
Let os/exec manage the nft command's stdin, stdout, stderr, and process lifecycle instead of handling its pipes sequentially. This prevents stderr from blocking while stdout is being drained and ensures the child is waited for on execution errors. Add a regression test that fills stderr beyond pipe capacity and verifies that Apply returns instead of deadlocking. Signed-off-by: James Ko <[email protected]>
| executable, err := os.Executable() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| originalLookPathNft := lookPathNft | ||
| lookPathNft = sync.OnceValues(func() (string, error) { | ||
| return executable, nil | ||
| }) |
There was a problem hiding this comment.
Not a fan of this pattern to be fair.. But I don't think there's actually a better way to do it 🙈
There was a problem hiding this comment.
Yeah :/ not without changing the production code
GordonTheTurtle
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
This PR replaces the manual StdinPipe/StdoutPipe/StderrPipe + sequential io.Copy handling in nftCtx.Apply with cmd.Stdin/cmd.Stdout/cmd.Stderr + cmd.Run(). The old code read stdout to completion before starting to read stderr, which could deadlock if the child (nft) wrote enough to stderr to fill the OS pipe buffer while blocked — cmd.Run() delegates concurrent draining of stdout/stderr (and non-blocking stdin writing) to the standard library, which correctly fixes this. The new test (re-executing the test binary as a fake nft that emits >4MB to stderr, with a 5s deadlock-detection timeout) is a solid, self-contained regression test for exactly this scenario, and includes proper cleanup.
No high/medium-severity or security issues were found. Two minor, non-blocking observations (not verified further, informational only):
daemon/libnetwork/internal/nftables/nft_exec_linux.go(around the newcmd.Run()error handling): collapsing all failure paths into a singlefmt.Errorf("running nft: %s %w", stderrBuf.String(), err)loses some of the previous stage-specific error context (e.g. "starting nft" vs "reading stdout of nft"), and produces a cosmetic double space whenstderrBufis empty (e.g. anexec.Startfailure). Not a functional bug.daemon/libnetwork/internal/nftables/nft_exec_linux_test.go(TestApplyDrainsStderrConcurrently): uses a fixed5 * time.Secondwall-clock timeout to distinguish "fixed" from "deadlocked" behavior. Generally safe, but timing-based tests carry a small inherent flakiness risk on slow/overloaded CI runners.
Overall this is a well-scoped, correct fix with good test coverage.
Follow up to: #52886
Summary
Fix process and pipe handling in the subprocess-based nftables backend.
Previously, dockerd created separate stdin, stdout, and stderr pipes for
nft, then drained stdout completely before reading stderr. Ifnftproduced enough stderr output to fill its pipe while stdout remained open, the child blocked writing stderr while dockerd blocked waiting for EOF on stdout.Errors while writing stdin or reading either output stream also returned without calling
Wait, potentially leaving the child process unreaped.This change attaches the input and output buffers directly to
exec.Cmdand usescmd.Run(). Theos/execpackage drains stdout and stderr concurrently and waits for the child after it starts, including when copying input or output fails.PR #52886 made this subprocess implementation the default to avoid crashes in the libnftables implementation, increasing the exposure of this path.
Reproduction
Use a disposable test environment with the existing Docker daemon stopped.
Create an
nftwrapper that writes more than a pipe buffer to stderr:Start dockerd with the wrapper first in
PATH:Before this change, dockerd becomes stuck while applying nftables rules: the wrapper blocks once its stderr pipe fills, while dockerd waits for EOF on stdout.
After this change, stdout and stderr are drained concurrently. The wrapper completes, and dockerd reports the
nftfailure instead of hanging.Testing
Added
TestApplyDrainsStderrConcurrently. It runs the test binary as a fakenftcommand and writes 4 MiB to stderr, ensuring the output exceeds the pipe capacity.Against the parent commit, the test reproduces the deadlock:
With this change:
Release notes (optional)
Assistance disclosure
I found this bug using gohawk's
processownershipanalyzer, part of a custom Go static-analysis suite I am writing. I used Codex to review the finding, verify that it was not a false positive, develop the reproduction, and add the regression test.A picture of a cute animal (not mandatory but encouraged)
🐳
Created with: Codex