pipeline: always destroy step on cancellation #6020
Closed
+28
−6
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.


Fix: always destroy step on pipeline cancellation
What was happening
When a pipeline step was canceled (e.g. manual cancel or agent shutdown), the step execution would exit early, but
DestroyStepwas not guaranteed to run.This could leave backend resources (such as Docker containers) running, resulting in orphaned steps after cancellation.
What this PR changes
This PR ensures that
DestroyStepis always executed, regardless of how the step finishes.DestroyStepviadeferin the step execution pathWhy this approach
Cancellation may stop waiting on a step, but it should never skip cleanup.
Ensuring
DestroyStepalways runs makes step lifecycle handling safer and aligns with expected CI behavior.This also closes the current gap where cleanup depended on higher-level execution flow.
Implementation overview
sequenceDiagram participant Runtime as Pipeline Runtime participant Engine as Backend Engine participant Ctx as Step Context participant Step as Backend Step Runtime->>Engine: StartStep Engine->>Step: start alt Step canceled Ctx-->>Runtime: ctx.Done() Runtime->>Engine: DestroyStep (background context) else Step finishes normally Step-->>Engine: exit Runtime->>Engine: DestroyStep end Engine-->>Runtime: cleanup completed