← Back to Pull requests

imagebuildah: don't delete stage containers while stages are running #7098

Open
unknown opened this pull request with 507 lines diff across 6 files
View on GitHub
unknown unknown commented on Sep 09, 2026 05:19 UTC
Author

What type of PR is this?

/kind bug

What this PR does / why we need it:

With --jobs greater than one, a single stage failure can take the whole build process down with a SIGSEGV, and can leave images with missing layers behind in the store.

When a stage reports an error, Build returns from its result loop immediately (executor.go, return "", nil, r.Error), while the goroutines for the other stages are still running. The deferred cleanup() then walks cleanupStages and calls Delete() on every stage executor registered there, including ones that are still executing, since stages are registered before Execute() runs when forceRmIntermediateCtrs is set or layers are disabled.

Delete() removes the stage's working container and clears s.builder, so a stage that is still running loses the filesystem underneath it. It then either dereferences the builder that was just cleared:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xa0 pc=0x147f6fe]

goroutine 221736 [running]:
github.com/containers/buildah/imagebuildah.(*StageExecutor).Execute(...)
        github.com/containers/buildah/imagebuildah/stage_executor.go:1766
github.com/containers/buildah/imagebuildah.(*Executor).buildStage(...)
        github.com/containers/buildah/imagebuildah/executor.go:586
github.com/containers/buildah/imagebuildah.(*Executor).Build.func3.1()
        github.com/containers/buildah/imagebuildah/executor.go:953

(addr=0xa0 is the offset of ContainerID in Builder, reached from
s.containerIDs = append(s.containerIDs, s.builder.ContainerID).)

or fails partway through an instruction, with errors such as:

error unmounting container: unmounting build container "": layer not known
initializing source containers-storage:<id>-working-container-1: unable to read layer "<hash>": layer not known
Error: identifier is not a container: building at STEP "COPY --from=stage-0 /opt /opt": copier: stat: "/opt": no such file or directory

b.stagesLock protects the cleanupStages map, not the lifetime of the builders it holds, so it does not prevent any of this.

The fix waits for the stage goroutines to finish before deleting anything they might still be using. The dereference is additionally guarded so that a working container removed by any other path fails that stage rather than the
whole process.

This should also cover the "layer not known" reports in #5805, since the same teardown is what removes layers other stages are still referencing.

How to verify it

Reproduces most easily with many stages and --jobs > 1 where one stage fails. It was found on a build with 49 FROM scratch stages feeding a final stage at --jobs=8: one stage failing reliably panicked the process and left damaged images in the store, which then broke later builds against that store.

Note the failure is probabilistic; #5805 reports roughly 66%, so it is worth running a reproducer several times before and after.

Which issue(s) this PR fixes:

None
<!-- Related to #5805; happy to link it as Fixes if maintainers agree the
"layer not known" reports there share this root cause. -->

Special notes for your reviewer:

Two points I would like a maintainer's opinion on:

1. This changes teardown timing. A failing build now waits for in-flight stages before returning instead of tearing down immediately, so a failure with slow stages takes longer to surface. That seemed the right trade against corrupting the store, but it is a user-visible behaviour change.

2. The wait is deadlock-safe because ch is buffered with len(stages), so every stage can report and exit without a reader, and cancel stops not-yet-started stages from doing work.

Does this PR introduce a user-facing change?

``release-note
Fixed a crash and possible image store corruption when a stage fails during a multi-stage build run with --jobs greater than one.
``

Diff: 507 lines (+380 / -127) across 6 files Reviewer: ajoshua2004 Review duration: 40415m
All Vouch review confidence checks passed
Residual risk 0.27 = Change Risk (0.48) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 507 lines diff across 6 files
0.48
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
42%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.373
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #7098"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 43.0% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 47.6% (MEDIUM) · Higher = larger blast radius
47.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 43.0% · Lower = rushed or shallow review
43.0%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 27.2% · Safe to merge
27.2%