Skip to content

Reap nft process on errors - #53517

Merged
thaJeztah merged 1 commit into
moby:masterfrom
kojah:reap-nft-process-on-errors
Aug 31, 2026
Merged

thaJeztah merged 1 commit into
moby:masterfrom
kojah:reap-nft-process-on-errors

Conversation

@kojah

@kojah kojah commented Aug 29, 2026 •

Copy link
Copy Markdown

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. If nft produced 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.Cmd and uses cmd.Run(). The os/exec package 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 nft wrapper that writes more than a pipe buffer to stderr:

$ test_bin="$(mktemp -d)"
$ cat > "$test_bin/nft" <<'EOF'
#!/bin/sh
head -c 1048576 /dev/zero >&2
exit 1
EOF
$ chmod +x "$test_bin/nft"

Start dockerd with the wrapper first in PATH:

$ sudo env PATH="$test_bin:$PATH" dockerd \
    --debug \
    --firewall-backend=nftables

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 nft failure instead of hanging.

Testing

Added TestApplyDrainsStderrConcurrently. It runs the test binary as a fake nft command and writes 4 MiB to stderr, ensuring the output exceeds the pipe capacity.

Against the parent commit, the test reproduces the deadlock:

$ go test ./daemon/libnetwork/internal/nftables -run '^TestApplyDrainsStderrConcurrently$' -count=1
--- FAIL: TestApplyDrainsStderrConcurrently (5.01s)
    nft_exec_linux_test.go:56: Apply deadlocked while nft was writing stderr
FAIL

With this change:

$ go test ./daemon/libnetwork/internal/nftables -run '^TestApplyDrainsStderrConcurrently$' -count=1
ok  	github.com/moby/moby/v2/daemon/libnetwork/internal/nftables	0.020s

$ go test -race ./daemon/libnetwork/internal/nftables -run '^TestApplyDrainsStderrConcurrently$' -count=1
ok  	github.com/moby/moby/v2/daemon/libnetwork/internal/nftables	1.085s

$ CGO_ENABLED=0 go test ./daemon/libnetwork/internal/nftables -run '^TestApplyDrainsStderrConcurrently$' -count=1
ok  	github.com/moby/moby/v2/daemon/libnetwork/internal/nftables	0.017s

$ go test ./daemon/libnetwork/internal/nftables -count=1
ok  	github.com/moby/moby/v2/daemon/libnetwork/internal/nftables	0.033s

Release notes (optional)

Prevent dockerd from hanging when the nft command produces enough stderr output to fill its pipe.

Assistance disclosure

I found this bug using gohawk's processownership analyzer, 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

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]>
@github-actions github-actions Bot added area/networking Networking area/daemon Core Engine labels Aug 29, 2026
@vvoland vvoland modified the milestones: 29.9.0, 29.8.0 Aug 31, 2026
@vvoland vvoland added the kind/bugfix PR's that fix bugs label Aug 31, 2026

@vvoland vvoland left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment on lines +27 to +35
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}

originalLookPathNft := lookPathNft
lookPathNft = sync.OnceValues(func() (string, error) {
return executable, nil
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this pattern to be fair.. But I don't think there's actually a better way to do it 🙈

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah :/ not without changing the production code

@vvoland vvoland added impact/changelog and removed area/daemon Core Engine labels Aug 31, 2026

@GordonTheTurtle GordonTheTurtle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 new cmd.Run() error handling): collapsing all failure paths into a single fmt.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 when stderrBuf is empty (e.g. an exec.Start failure). Not a functional bug.
  • daemon/libnetwork/internal/nftables/nft_exec_linux_test.go (TestApplyDrainsStderrConcurrently): uses a fixed 5 * time.Second wall-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.

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@thaJeztah
thaJeztah merged commit f006b75 into moby:master Aug 31, 2026
388 of 400 checks passed
@kojah
kojah deleted the reap-nft-process-on-errors branch August 31, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants