Fix flaky TestIsStdinIsReadableWithAPipe by draining stdin in the test helper - #4140
Merged
Conversation
…t helper On Windows, the piped-input case (echo output | helper) left data unread in the pipe. When the helper exited without draining, the upstream writer failed with 'The process tried to write to a nonexistent pipe.' and that message leaked into the parent's CombinedOutput, corrupting the exact "true" assertion. Drain stdin with io.Copy(io.Discard, os.Stdin) before exiting so the pipe closes cleanly.
DrJosh9000
approved these changes
Jul 30, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Dan Niknam · Slack thread
Description
TestIsStdinIsReadableWithAPipeis intermittently flaky on Windows.Before: the test spawns a helper with piped input (
echo output | helper), but the helper never reads that input. On Windows, when the helper exits with data still unread in the pipe, the upstream writer (echo) hits a broken pipe and printsThe process tried to write to a nonexistent pipe.. That message leaks into the parent'sCombinedOutput, so the captured output is no longer exactly"true"and the assertion fails intermittently.After: the helper drains stdin before exiting, so the pipe closes cleanly,
echonever hits a broken pipe, and the assertion sees exactly"true".Context
Requested via the Slack thread linked above.
This fixes one of four recently-observed CI flakes on
main. The other three (a Linux hook-hang, a Linuxfork/exec: bad file descriptor, and a WindowsGetExitCodeProcess: handle is invalid) form a separate concurrent-spawn fd/handle-race cluster that did not reproduce under local stress testing and are being investigated separately. They are intentionally out of scope here.Changes
Add
io.Copy(io.Discard, os.Stdin)in theTestMainhelper'scase "1", immediately after theIsReadable()call, to drain stdin before the helper exits. This is harmless for the nil-stdin and file-redirect helper invocations (there is simply nothing, or a bounded amount, to drain). Production code instdin.gois unchanged — this is a test-only fix.Testing
go test ./...)go tool gofumpt -extra -w .)Verified locally with Go 1.26.5:
go build ./...— passesgo vet ./internal/stdin/...— passesgo test ./internal/stdin/... -count=3— passesDisclosures / Credits
Claude Code produced this test-helper change.
Generated by Claude Code