imagebuildah: don't delete stage containers while stages are running #7098
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
ajoshua2004
What type of PR is this?
/kind bug
What this PR does / why we need it:
With
--jobsgreater 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,
Buildreturns from its result loop immediately (executor.go,return "", nil, r.Error), while the goroutines for the other stages are still running. The deferredcleanup()then walkscleanupStagesand callsDelete()on every stage executor registered there, including ones that are still executing, since stages are registered beforeExecute()runs whenforceRmIntermediateCtrsis set or layers are disabled.Delete()removes the stage's working container and clearss.builder, so a stage that is still running loses the filesystem underneath it. It then either dereferences the builder that was just cleared:(
addr=0xa0is the offset ofContainerIDinBuilder, reached froms.containerIDs = append(s.containerIDs, s.builder.ContainerID).)or fails partway through an instruction, with errors such as:
b.stagesLockprotects thecleanupStagesmap, 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 49FROM scratchstages 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
chis buffered withlen(stages), so every stage can report and exit without a reader, andcancelstops 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.