Skip to content

fix(interp): release builtins blocked on an idle stdin pipe at the run deadline - #710

Open
astuyve wants to merge 2 commits into
mainfrom
patina/stdin-pipe-deadline
Open

astuyve wants to merge 2 commits into
mainfrom
patina/stdin-pipe-deadline

Conversation

@astuyve

@astuyve astuyve commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

What

MaxExecutionTime didn't bound Run while a builtin waited on an *os.File stdin pipe that never sends, which is the setup the docs recommend. 13 builtins hung forever: cat, head, tail, grep, wc, sort, uniq, tr, sed, cut, strings, xargs and jq. Only read, awk and sha256sum had their own way to wake up at the deadline.

Run now binds the runner's stdin to the run's context once (interp/stdin_deadline.go). When the context ends, a watchdog sets an already-expired read deadline on the file, which wakes any blocked Read with os.ErrDeadlineExceeded.

  • The file is never closed and no bytes are read on the caller's behalf.
  • The deadline is cleared when Run returns.
  • Files without read deadlines (regular files, /dev/null) are left alone.

Limits, also noted in SHELL_FEATURES.md:

  • Windows: anonymous pipes there don't support read deadlines, so a blocked stdin read can still delay return.
  • Other stdin types: a stdin that isn't an *os.File isn't covered.
  • Existing deadline: a read deadline the caller already had on stdin is cleared.

Credit

Found by Jacob Hayes with his deterministic-simulation tool patina (finding #5: a never-sending pipe leg plus a never-returns detector, about 140 runs). Findings thread: https://dd.slack.com/archives/D0AC8RLB21J/p1793475631273749

Tests

interp/stdin_pipe_deadline_test.go:

  • TestMaxExecutionTimeBoundsBuiltinBlockedOnStdinPipe: each of the 16 stdin-reading builtins gets an idle os.Pipe stdin and a 200ms MaxExecutionTime, and must return context.DeadlineExceeded within 5s. On main exactly the 13 reported builtins fail.
  • TestStdinPipeDeadlineDoesNotLeakOrStealBytes: after a normal run no deadline is left on the pipe and later bytes are still readable.

On Windows the test skips the 13 cases that rely on a read deadline (the limit above); read, awk and sha256sum still run there. Passes with -race. GOOS=windows/darwin go vet ./interp/ is clean. go test ./... passes except TestPSPentestLargePIDListDoesNotHang in builtins/ps, which fails the same way on clean main on this host.

🤖 Generated with Claude Code

astuyve and others added 2 commits October 31, 2026 17:49
…n deadline

MaxExecutionTime did not bound Run while a builtin waited on an
*os.File stdin pipe that never sends, the setup the docs recommend:
cat, head, tail, grep, wc, sort, uniq, tr, sed, cut, strings, xargs and
jq hung forever. Only read, awk and sha256sum had their own way to wake
up at the deadline.

Run now binds the runner's stdin to the run's context once: when the
context ends, a watchdog sets an expired read deadline on the file,
which wakes any blocked Read with os.ErrDeadlineExceeded. The file is
never closed and no bytes are read on the caller's behalf, and the
deadline is cleared when Run returns. Files without read deadlines
(regular files, /dev/null, anonymous pipes on Windows) are left alone.

Found by Jacob Hayes with patina (https://github.com/JacobHayes/patina).

Environment: Datadog workspace

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Anonymous pipes on Windows have no read deadlines, the documented limit
of this fix; read, awk and sha256sum still run there.

Environment: Datadog workspace

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@astuyve
astuyve marked this pull request as ready for review November 1, 2026 02:05
@astuyve
astuyve requested review from a team as code owners November 1, 2026 02:05
@astuyve
astuyve requested a review from Hitsuji-M November 1, 2026 02:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant