Repository navigation
fix(plan): run the pre_stop hooks before every stop the plan makes - #14555
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
There was a problem hiding this comment.
🟡 Changes recommended
Container stops are not deduplicated by ID, allowing scale-down or provider migration to schedule duplicate hook/stop chains that can run hooks concurrently or interrupt them.
1 open issue
What changed in this PR
This PR adds pre_stop hooks to Compose’s reconciliation plan, bringing plan-driven stops closer to stop, restart, and down behavior. It is the first step toward the lifecycle consolidation tracked in #14551.
Changes:
- Adds a
RunPreStopoperation before plan-driven stops and replacement creation. - Integrates hook execution and progress reporting.
- Adds unit and end-to-end coverage for ordering, failures, skipped hooks, and recreation recovery.
| File | Description |
|---|---|
pkg/compose/reconcile.go |
Schedules hooks before stops and replacement creation. |
pkg/compose/reconcile_pre_stop_test.go |
Tests hook planning, ordering, and reuse. |
pkg/compose/executor.go |
Dispatches the new hook operation. |
pkg/compose/executor_pre_stop_test.go |
Tests execution, failures, skips, and progress events. |
pkg/compose/executor_ops.go |
Executes hooks through the existing hook runner. |
pkg/compose/executor_events.go |
Reports stopping progress while hooks run. |
internal/planning/plan.go |
Defines the RunPreStop operation. |
internal/planning/plan_test.go |
Tests the operation’s display name. |
internal/e2e/testdata/TestUpSkipsPreStopHookOfAStoppedContainerItRecreates/compose.yaml |
Provides the stopped-container fixture. |
internal/e2e/testdata/TestUpRunsPreStopHookOfTheReplicaItScalesDown/compose.yaml |
Provides the scale-down fixture. |
internal/e2e/testdata/TestUpRunsPreStopHookOfTheContainerItRecreates/compose.yaml |
Provides the recreation fixture. |
internal/e2e/testdata/TestUpKeepsTheContainerWhoseFailingPreStopHookBlocksItsReplacement/compose.yaml |
Provides configurable hook failure and recovery. |
internal/e2e/hooks_plan_stop_test.go |
Adds four hook lifecycle scenarios. |
internal/e2e/checks.go |
Adds a check that running containers remain running. |
Review effort: Balanced
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
04f1a94 to
f013c10
Compare
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
The stop of the reconciliation plan runs the pre_stop hooks itself, in the executor, since docker#14477, except the stop of a recreate: the replacement is created under a temporary name before the old container is stopped, and a failing hook would leave it behind for the next up to trip on. So a container that `up` replaces still goes without its hooks. The hooks are now an operation of the plan, RunPreStop, that the stop depends on. Every stop the plan makes has it: a recreated container, a scale down, the containers of a recreated network or volume, the dependents restarted with a recreated dependency, a service that moves to a provider. A container that has no process to run them in skips them and is stopped all the same, as for restart and down; a hook that fails fails the plan and leaves the container running. A container stopped for two reasons is stopped once, and its hooks run once. A recreation runs the hooks before it creates the replacement: one that fails then leaves nothing behind, and no stop has to skip them any more. The plans of services without pre_stop hooks are unchanged. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
A scale down or a move to a provider added its own stop (and its own pre_stop hooks) for a container an earlier stage, a recreated network, had already stopped: the hooks ran twice, concurrently, and one stop could interrupt the hooks of the other. planStop now reuses the stop already scheduled for the container and records the one it adds. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
f013c10 to
9a6d85f
Compare
docker-agent
left a comment
There was a problem hiding this comment.

What this PR does
The
pre_stophooks become an operation of the reconciliation plan, so that every containerupstops runs them, the one a recreation replaces included, and a recreation runs them before it creates the replacement.Related issue
First sub-task of #14551.
Context
#14477 made
uprun thepre_stophooks when it scales a service down or stops a container for a recreated network, volume or dependency. It does so inside the executor's stop, and skips them for the container a recreation replaces: the hooks would run after its replacement is created under a temporary name, and one that fails would leave that replacement behind for the nextupto trip on. So the same hook still runs or not depending on whyupstops the container, andstop,restartanddowneach carry their own copy of the logic.What the PR brings
RunPreStop), and the stop depends on it. Every stop the plan makes has it: a recreated container, a scale down, the containers of a recreated network or volume, the dependents restarted with a recreated dependency, a service that moves to a provider. The executor's stop is a plain stop again, andSkipPreStopis gone.restartanddown. A container with no process to run the hooks in (stopped, paused) skips them and is stopped all the same; a relay has no hooks to run; a dry run runs none. A hook that fails failsup, and the container it was about to stop is left running.Recreate/Recreatedrow.pre_stophook are the ones they were, golden tests included. Orphans are left out: their service is not in the model, and nothing the engine stores says what their hooks were.This changes what users with
pre_stophooks see: the hook now also runs whenuprecreates a container, which is what the hook is for. As forrestartanddown, the hooks are the ones of the model being applied, which a change of hooks makes differ from the ones the old container was created with.Why this is the right first brick
stop,restartanddownare still to move onto the plan (#14551). With the hooks a node of the plan, they stop being something only those three know how to do, and the next steps can reuse it instead of carrying a copy.Testing done
-raceforpkg/compose,internal/planningandcmd, after the rebase on fix(executor): run pre_stop hooks before the plan stops a container #14477golangci-lint run --build-tags e2eupthat recreates,upthat scales down,upthat recreates a stopped container, and a failing hook that leaves the container running, leaves no replacement behind, and letsupreplace it once fixed. Run before the rebase on fix(executor): run pre_stop hooks before the plan stops a container #14477; not rerun since,TestPreStopHookWhenUpStopsAContainer(fix(executor): run pre_stop hooks before the plan stops a container #14477) now expects the replaced container to run its hook.AI tool used
Claude Code (Anthropic) wrote the change, with review by the submitter; independently reviewed with the GitHub Copilot CLI.
Additional context
The tests are the bulk of the change:
reconcile_pre_stop_test.go(plan),executor_pre_stop_test.go(execution) andhooks_plan_stop_test.go(e2e, withRunningContainersKeptadded tochecks.go). Rebasing on #14477 removed its two unit tests of the executor-side hooks, superseded by these.