Skip to content

fix(build): read bake's stderr to the end before waiting for it - #14335

Merged
glours merged 1 commit into
docker:mainfrom
ndeloof:fix/bake-stderr-pipe-race
Oct 13, 2026
Merged

glours merged 1 commit into
docker:mainfrom
ndeloof:fix/bake-stderr-pipe-race

Conversation

@ndeloof

@ndeloof ndeloof commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

What this PR does, in one sentence

Stops compose build from sometimes losing the reason of a build failure: the end of buildx's output, where the error is, was dropped when the progress display lagged behind the process.

Context

compose build runs docker buildx bake and reads its progress from the process's standard error, forwarding the solve statuses to the progress display and keeping the other lines, which are the error. The process was waited for in a goroutine while that stream was still being read, and Go closes the pipe as soon as it sees the process exit — what was not read yet is gone. When the display is slower than the process, which a loaded machine makes likely, the tail of the stream is lost: the build fails with exit code 1 but without the Dockerfile excerpt and the failed to solve: … line that explain why, or with them cut in the middle.

That is what a few end-to-end tests have been failing on intermittently, in the CI jobs of otherwise unrelated pull requests: TestBuildPlatformsWithCorrectBuildxConfig/platform_not_supported_by_builder and TestBuildSSH/build_failed_with_ssh_default_value expect an error message on standard error and find the output cut right before it (in the failing run, the Dockerfile excerpt stops after FROM alpine, with no final error line). The pattern dates from the introduction of the bake build.

What the PR brings

The stream is now read to the end before the process is waited for, as the os/exec documentation requires, so the failure of a build always reaches the user with its reason. The case in the reader that only existed to absorb the pipe being closed under it is removed with the race, and a read error now stops the process instead of leaving it blocked on a stream nobody reads.

A regression test runs a fake bake that writes a long stream of statuses then an error and exits at once, with a deliberately slow consumer: it fails deterministically without the fix (the error line is lost) and passes with it. I could not reproduce the failure of the end-to-end tests themselves on a quiet or a heavily loaded machine; the test pins the mechanism instead.

🤖 Generated with Claude Code

@ndeloof
ndeloof requested review from a team as code owners October 13, 2026 09:52

@docker-agent docker-agent 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

@docker-agent docker-agent 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

Comment thread pkg/compose/build_bake.go Resolved
Comment thread pkg/compose/build_bake.go Resolved
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/compose/build_bake.go 54.54% 4 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@ndeloof
ndeloof requested a review from docker-agent October 13, 2026 10:06

@docker-agent docker-agent 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.

This review covers only the commits pushed since e7f8690.

Assessment: 🟢 APPROVE

The single changed line restores fmt.Errorf("failed to execute bake: %w", readErr) error wrapping in forwardBakeStatus, which was inadvertently dropped in the prior commit. This is the correct fix: it preserves error chain unwrappability (%w) and adds the diagnostic context string "failed to execute bake" so callers can identify the source of the I/O error. No bugs introduced.

@ndeloof
ndeloof force-pushed the fix/bake-stderr-pipe-race branch from b42b340 to 170b634 Compare October 13, 2026 10:19
@ndeloof
ndeloof requested a review from docker-agent October 13, 2026 10:20

@docker-agent docker-agent 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

The fix correctly resolves the race between reading bake's stderr and waiting for the process to exit — reading to EOF before calling cmd.Wait() is precisely what the os/exec documentation requires, and the regression test pins the mechanism reliably.

Lower-confidence findings (not posted inline)

  • [low] pkg/compose/build_bake_test.go:566 — consumer goroutine may leak if an earlier assertion calls t.Fatal (confidence: not scored — low severity, skipped verification)

    The goroutine that counts received status records sends its result to the unbuffered received channel. If any assertion before <-received (lines 568–572) fails and triggers t.Fatal/t.FailNow, the test function returns immediately, leaving the goroutine permanently blocked on the send. Using received := make(chan int, 1) (capacity 1) would eliminate the risk without changing any other logic.

@ndeloof
ndeloof requested a review from docker-agent October 13, 2026 10:33

@docker-agent docker-agent 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.

This review covers only the commits pushed since 170b6346ae37.

Assessment: 🟢 APPROVE

The single change — buffering the received channel with capacity 1 in TestWaitBakeKeepsTheEndOfTheStream — is correct and sufficient. The goroutine sends exactly one value to received after ch is closed, so a buffer of 1 guarantees the goroutine exits cleanly even if an assertion aborts the test via t.FailNow() before the <-received receive is reached, preventing a goroutine leak. No issues introduced by this change.

doBuildBake waited for the buildx process in a goroutine while reading its
rawjson stderr stream. os/exec closes the pipe as soon as Wait sees the
process exit, dropping whatever was not read yet: when the consumer of the
solve statuses (the progress display) lags behind the process, the end of the
stream is lost. That is where bake tells why it failed (the Dockerfile
excerpt, then "failed to solve: ..."), so the build failed with exit code 1
but without its reason, or with it truncated.

Read the stream to EOF first, as the os/exec documentation requires, then
Wait. The os.ErrClosed case of the reader, which only existed to absorb the
pipe being closed under it, goes away with the race.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
@ndeloof
ndeloof force-pushed the fix/bake-stderr-pipe-race branch from 1f1b41a to a2e88f5 Compare October 13, 2026 11:05
@glours
glours merged commit b10678d into docker:main Oct 13, 2026
61 checks passed
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.

3 participants