Skip to content

fix(executor): run pre_stop hooks before the plan stops a container - #14477

Merged
ndeloof merged 4 commits into
docker:mainfrom
htoyoda18:fix/pre-stop-on-plan-stops
Nov 27, 2026
Merged

ndeloof merged 4 commits into
docker:mainfrom
htoyoda18:fix/pre-stop-on-plan-stops

Conversation

@htoyoda18

@htoyoda18 htoyoda18 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Since the reconciliation plan (v5.2.0), the containers that up and scale stop on their own (scale down, the container a recreate replaces, a restart: true dependent, a recreated network or volume) were stopped without running their pre_stop hooks. Only stop and down still ran them. The plan's stop operation now runs the service's pre_stop hooks first, as stop and down do.

Related issue

Fixes #14476

Changes made

  • pkg/compose/executor_ops.go: execStopContainer calls a new runPreStop before ContainerStop. It runs the hooks of the project's service on a running container with runHook, the same helper execRunPostStart uses.
    • A failing hook fails the plan, and the container is not stopped.
    • Orphans, containers that are not running, and provider relay containers have no hooks to run. This is the same condition stopContainer uses for stop and down.
  • pkg/compose/executor_test.go: TestExecutePlanStopContainerRunsPreStop covers four cases: the hook runs before the stop; a failing hook fails the plan without stopping; no hook for a container that is not running; no hook for a container of no service of the project.
  • pkg/e2e/hooks_test.go, testdata/TestPreStopHookWhenUpStopsAContainer/compose.yaml: a scenario checking that the hook runs for a scaled-down replica, a restart: true dependent, and a recreated container. Each service writes to its own file in a volume.

Testing done

  • Unit tests pass: make test
  • E2E tests pass: make e2e-compose or make e2e-compose-standalone
  • Linting passes: make lint
  • Code formatted: make fmt
  • Manually tested (describe):

AI tool used (if applicable)

AI Tool:

Additional context

Two design decisions I was unsure about:

  1. The container a recreate replaces now runs pre_stop too (it never did before).
  2. A failing hook fails the plan, as with stop and down. On a recreate, this leaves the old container running.

Also, the output of these hooks is not streamed, because they run before runStart sets the hook-log listener.

(not mandatory) A picture of a cute animal, if possible in relation to what you did

image

@htoyoda18
htoyoda18 marked this pull request as ready for review November 7, 2026 04:41
@htoyoda18
htoyoda18 requested review from a team as code owners November 7, 2026 04:41
@htoyoda18
htoyoda18 requested review from glours and ndeloof November 7, 2026 04:41

@ndeloof ndeloof 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.

Thanks for the fix, and for the clear reproduction in #14476. I checked it against the code: execStopContainer calls ContainerStop directly, and none of the OpStopContainer nodes carry the service, so the plan never reaches the pre_stop hooks that v5.1.4 ran through stopContainer. Putting the hook in execStopContainer covers every stop of the plan (scale down, restart: true dependents, network and volume recreation, recreate) without touching the seven sites in reconcile.go, which is the right place.

I read the diff only. I did not run the tests, and CI has not run yet (only DCO shows), so none of this is verified by a run.

On your two design questions: running pre_stop on the container a recreate replaces is a legitimate change, even if v5.1.4 did not do it, and I would keep it. A failing hook failing the plan is consistent with stop and down.

Changes I would ask for

  1. A container that is gone or stopped by the time of its stop now fails up. stopContainer in down.go ignores NotFound and Conflict coming from the hook, which is what #12986 added for exactly this case. runPreStop returns the error as is. The ctr.State != container.StateRunning check works on the summary observed when the plan was built, which can be stale: in a recreate, the old container is only stopped after the new one has been created. If it exits in between, ExecCreate fails with a conflict and the whole plan fails, where ContainerStop on a stopped container used to succeed. I would ignore NotFound and Conflict from the hook as down does, and go on to ContainerStop.

  2. The hook loop now exists twice. The condition (service known, not a relay container) and the loop over PreStop are in stopContainer and in runPreStop. A shared helper would keep the error policy of point 1 in one place.

To check

  1. The temporary container of a recreate whose hook fails. The recreate plan is create (temporary name) → stop old → remove old → rename. If the hook fails, the old container stays running, as the description says, but the new one stays too, created and not started, with the labels of the service. I could not tell what the next up does with it, for instance whether it counts as a second replica. Since a failing hook makes this easy to reach, could you try a recreate with a failing pre_stop, then a second up, and look at docker ps -a? A test for it would help.

Smaller points

  1. The description says that skipping containers that are not running is "the same condition stopContainer uses". It is not: stopContainer does not look at the state and relies on the Conflict handling of point 1.
  2. The isRelayContainer branch has no test, and the recreate case is only covered by the e2e scenario. A unit case on a plan with a recreate would be quick to add.
  3. The "Testing done" checkboxes are all unchecked and the "AI tool" section is empty. Please fill them in.

@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ndeloof

ndeloof commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

One more point, about where the hooks live, which I would not make a blocker for this fix.

The patch runs the pre_stop hooks inside execStopContainer, so the plan still shows a single StopContainer: the hooks have no node, no dependencies and no progress event of their own. The other hooks are explicit operations of the plan: post_start is OpRunPostStart, and pre_start is OpRunPreStart with OpCreateHookContainer. The doc of OperationType also says that each operation maps to exactly one Docker API call, which a StopContainer that first runs the hooks no longer does.

From an architecture point of view, a OpRunPreStop node ahead of each OpStopContainer, carrying the service, would be the cleaner model: the plan would show the hooks, they would have their own events and failure handling, and a dependency such as "the hook ends before the replacement is created" could be expressed. The cost is a larger change for this PR: the stop nodes are emitted at seven sites of reconcile.go, and for a recreate the position of the hook relative to the creation of the replacement would have to be decided.

I am fine with the current approach as the fix for #14476, since it covers every stop in one place. If we keep it, a comment on execStopContainer saying that the hooks are deliberately not a node of the plan would help the next reader. If the explicit operation seems worth it, it could also be a follow-up once this one is in.

@htoyoda18

Copy link
Copy Markdown
Contributor Author

@ndeloof
Thanks! 1, 2 and 5 are addressed (one helper for the hooks and the NotFound/Conflict check; new unit cases). 4: you're right.

On 3, I tried it, and it breaks things. A recreate whose hook fails leaves the replacement as <id>_web-1 (created). After fixing the hook, up fails with a name conflict, and up -d --wait then waits forever on that never-started container. Only down recovers.

Options:

  • (a) run the old container's pre_stop before the replacement is created (an OpRunPreStop node)
  • (b) skip pre_stop on the replaced container, as in v5.1.4

I'd go with (b) here and (a) as a follow-up. What do you think?

@htoyoda18

Copy link
Copy Markdown
Contributor Author

@ndeloof I went ahead with (b): a recreate no longer runs the replaced container's pre_stop hooks, as in v5.1.4. Every other stop of the plan still runs them.

I also added the comment you suggested on execStopContainer.

(a) can be a follow-up. I can open an issue for it if you'd like.

Could you take another look, and approve the CI workflows when you have a moment? Thanks!

@htoyoda18
htoyoda18 requested a review from ndeloof November 15, 2026 09:56
@ndeloof

ndeloof commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@htoyoda18 can you please rebase and resolve conflicts ?

Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
@htoyoda18
htoyoda18 force-pushed the fix/pre-stop-on-plan-stops branch from 1cb375d to d9fad6d Compare November 25, 2026 14:03
Signed-off-by: Hiroto Toyoda <hirototoyoda20020927@gmail.com>
@htoyoda18
htoyoda18 force-pushed the fix/pre-stop-on-plan-stops branch from d9fad6d to 49d2941 Compare November 25, 2026 14:07
@htoyoda18

Copy link
Copy Markdown
Contributor Author

@ndeloof Rebased onto the latest main and resolved the conflicts.

Could you take another look when you have a moment?

@ndeloof
ndeloof disabled auto-merge November 25, 2026 15:58

@ndeloof ndeloof 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.

Blocking: the e2e test does not find its compose file (see inline). Everything else looks good to me.

@@ -0,0 +1,39 @@
services:

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 directory is pkg/e2e/testdata/, but the e2e suite lives in internal/e2e/ and the scenario anchors on internal/e2e/testdata/<TestName>/. As submitted, TestPreStopHookWhenUpStopsAContainer fails at its first step with no configuration file provided: not found.

Moving the file to internal/e2e/testdata/TestPreStopHookWhenUpStopsAContainer/compose.yaml fixes it: with that move the new test passes against a daemon, together with the other TestPreStopHook* tests, and go test ./pkg/compose ./internal/planning and lint are clean.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof
Thanks for catching this! The compose file was left behind under pkg/e2e/testdata/ when I rebased onto the internal/e2e move. It's now under internal/e2e/testdata/TestPreStopHookWhenUpStopsAContainer/.

…ner to internal/e2e

Signed-off-by: Hiroto Toyoda <hirototoyoda20020927@gmail.com>
auto-merge was automatically disabled November 26, 2026 14:51

Head branch was pushed to by a user without write access

@htoyoda18
htoyoda18 requested a review from ndeloof November 26, 2026 14:57
@ndeloof
ndeloof enabled auto-merge (rebase) November 26, 2026 15:19
@ndeloof
ndeloof disabled auto-merge November 27, 2026 07:40
@ndeloof
ndeloof merged commit 5af4844 into docker:main Nov 27, 2026
61 checks passed
ndeloof added a commit to ndeloof/compose that referenced this pull request Nov 27, 2026
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>
glours pushed a commit that referenced this pull request Nov 27, 2026
The stop of the reconciliation plan runs the pre_stop hooks itself, in the
executor, since #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>
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.

[BUG] pre_stop hooks are skipped when up or scale stops a container

2 participants