Skip to content

core: don't let a late Detach force-kill a service being stopped - #14440

Merged
sipsma merged 6 commits into
dagger:mainfrom
sipsma:sipsma/services-late-detach-stop
Oct 1, 2026
Merged

sipsma merged 6 commits into
dagger:mainfrom
sipsma:sipsma/services-late-detach-stop

Conversation

@sipsma

@sipsma sipsma commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Problem

A service that is being stopped gracefully on request can be SIGKILLed partway through its shutdown. Any service that takes longer than 10 seconds to exit after SIGTERM is affected.

In CI this broke TestCachePersistence in trace 70fbaf5a. The test fails at engine_persistence_test.go:533 because a nested engine's cache didn't survive a restart.

Root cause

An explicit graceful Service.stop (Services.stop with force=false) set the service's bindings to 0. It then sent SIGTERM and waited. The service stays registered until it actually exits.

Its remaining bindings are usually released after that, e.g. when a tunnel shuts down. Each release calls Detach, which:

  1. decremented bindings to -1,
  2. saw no binders left and started its own graceful stop: a second SIGTERM,
  3. sent SIGKILL after TerminateGracePeriod (10s) if the service hadn't exited.

Nothing checked whether a stop was already in flight. So the explicit stop's clean shutdown was cut short 10 seconds after the late Detach. Two late Detaches started two independent timers.

The CI trace shows this sequence for one nested engine (xxh3:77152e62b84f7668):

  1. 16:59:42.917Z: explicit Service.stop.
  2. 16:59:48.390Z: detach: stopping bindings=-1.
  3. 16:59:54.375Z: the nested engine starts closing its cache.
  4. 16:59:58.391Z, exactly 10s after the Detach: service stop interrupted, then SIGKILL. The engine exits 137.
  5. 17:00:13.847Z: the next engine finds clean_shutdown unset and wipes its store.

The late Detach is routine. In the CI logs we inspected, every detach: stopping record had bindings=-1, including in green runs. It only fails a test when the SIGKILL lands before the shutdown finishes.

It also reproduces locally. On unfixed main, three runs of TestCachePersistence/TestDiskPersistenceAcrossRestart/module_function_directory_list_survives_repeated_restarts all passed, but logged:

  • 12 detach: stopping bindings=-1,
  • 3 stops interrupted exactly 10.000s after the same service's late Detach, each ending in exit 137.

The kills just happened to land after the cache had been synced.

Fix

Each running service counts the stops in flight that own stopping it: a Detach-owned stop and each explicit stop. The count is decided under the manager lock.

  • Detach of a service that is already stopping (bindings 0) does nothing. Bindings never go negative, and repeated Detaches don't start timers.
  • An explicit graceful stop joins a stop already in flight instead of sending another SIGTERM. Once the service has exited, it collects the stop result like the owning stop does, so cleanup failures (e.g. a cgroup that can't be removed) are still reported to it.
  • An explicit graceful stop with nothing in flight works as before: SIGTERM, then wait for exit. Only the caller's context bounds the wait.
  • kill=true and session teardown still SIGKILL immediately, even while a graceful stop is in flight.
  • An explicit stop that returns before the service exits (e.g. its context is canceled), with no other stop in flight, hands the service to the Detach policy: SIGTERM, then SIGKILL after TerminateGracePeriod.
    • Without this, a late Detach that joined the stop would leave the service running with no binders, and new starters of the same service would wait on it forever.
    • Previously, that late Detach stopped the service by accident.

One behavior changes on purpose. A service that ignores SIGTERM during an explicit graceful stop now keeps waiting, the same as it always did when no late Detach arrived. Previously the late Detach's 10s timer killed it. The caller can still cancel the stop, which hands the service to the Detach policy, or pass kill: true.

Invariants

  • I1: A graceful stop sends at most one SIGTERM per service, however many Detaches and graceful stops arrive while it's in flight.
  • I2: Bindings never go below 0.
  • I3: Once a running service has no binders, some stop owns stopping it until it exits.
  • I4: kill=true and session teardown request SIGKILL immediately.

Tests

core/services_stop_test.go runs the service manager in synctest bubbles against the real TerminateGracePeriod, so the tests take no wall-clock time. The services record each stop request, and the services in the result tests use the container service's real exit classification.

  • Two late Detaches during an explicit graceful stop: no further stop request, even after twice the grace period. Bindings stay 0, and the service exits cleanly when it finishes.
  • A graceful stop joining one in flight sends no signal. It reports nil for a signal exit and the cleanup error for a cleanup failure, as the owner does.
  • A graceful stop joining one in flight, then canceled: no signal, and the owner still finishes cleanly.
  • An explicit stop abandoned before exit, graceful and kill=true: the Detach policy takes over, with SIGTERM, then SIGKILL after TerminateGracePeriod.
  • Existing behavior: Detach of the last binding sends SIGTERM and escalates at exactly TerminateGracePeriod, and kill=true escalates an in-flight graceful stop immediately.

On unfixed main, every test of new behavior fails on its assertion, and the two existing-behavior tests pass. With this change all of them pass, including -count=30 -race.

On this change, three runs of the same persistence test passed and logged:

  • 13 detach: service already stopping, the late Detaches joining the stop,
  • 0 bindings=-1,
  • 0 interrupted stops,
  • 0 exit 137.

An explicit graceful Service.stop zeroed the service's bindings but left it
running until it exited. A Detach of one of its remaining bindings arriving
meanwhile drove bindings to -1 and started its own graceful stop: a second
SIGTERM, then a SIGKILL after TerminateGracePeriod (10s). So any service that
takes longer than that to shut down cleanly was killed partway through the
clean shutdown its caller asked for. In CI this hit nested engines, which can
take a while to sync their cache on shutdown; the next engine then wiped its
store as uncleanly shut down.

Track the stops in flight for each running service under the manager lock:

- Detach of a service that is already stopping (bindings 0) is a no-op, so
  bindings never go negative and repeated Detaches don't start timers.
- An explicit graceful stop joins a stop already in flight instead of sending
  another SIGTERM. Otherwise it owns the stop and escalates to SIGKILL after
  its own ExplicitStopGracePeriod rather than Detach's 10s.
- A force stop still sends SIGKILL immediately.
- If an explicit stop returns before the service exits, e.g. its context is
  canceled, and no other stop is in flight, the service is stopped as if it
  were detached, so it isn't left running with no binders.
- Detach and stop of an instance that was replaced under the same key no
  longer touch the replacement.

Signed-off-by: Erik Sipsma <[email protected]>
A graceful stop that joined a stop in flight only waited for the service to
exit, so it reported success even when stopping it failed, e.g. its cgroup
couldn't be removed. Once the service has exited, collect the stop result
the way the owning stop does; the service is gone, so no signal is sent.

Signed-off-by: Erik Sipsma <[email protected]>
An explicit graceful stop never escalated to SIGKILL before; it waits for the
service to exit, bounded only by the caller's context, and callers that want
an immediate stop pass kill. Keep that behavior rather than adding a new
escalation deadline. A caller that gives up still hands the service to the
Detach policy.

Signed-off-by: Erik Sipsma <[email protected]>
Detach of an instance that has exited can still decrement a replacement
started under the same key. That is separate from the late-Detach bug and
wasn't seen in CI, so leave it out of this change.

Signed-off-by: Erik Sipsma <[email protected]>
Run the service stop tests in a synctest bubble against the real
TerminateGracePeriod instead of shortening it through a test-only field on
Services, and trim them to the cases this change handles plus the existing
Detach and force-stop escalation.

Signed-off-by: Erik Sipsma <[email protected]>
@grouville
grouville self-requested a review October 1, 2026 00:48
@sipsma
sipsma merged commit 6ed4d3a into dagger:main Oct 1, 2026
90 checks passed
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.

2 participants