Skip to content

[backport-0.21] core: don't let a late Detach force-kill a service being stopped - #15069

Merged
sipsma merged 1 commit into
backport-0.21from
sipsma/backport-0.21-late-detach-stop
Nov 7, 2026
Merged

sipsma merged 1 commit into
backport-0.21from
sipsma/backport-0.21-late-detach-stop

Conversation

@sipsma

@sipsma sipsma commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Backport to 0.21 of #14440, as one commit.

What this changes for 0.21 users

When a service is stopped gracefully, for example with Service.stop, and one of its bindings detaches while it is shutting down, that detach starts a second stop: another SIGTERM, then a SIGKILL 10 s later. So a service that takes more than 10 s to shut down cleanly is killed partway through the clean shutdown its caller asked for.

In CI this hits nested engines, which can take a while to sync their cache on shutdown. The next engine then wipes its store as uncleanly shut down. On backport-0.21 it is one of the causes of TestCachePersistence/TestDiskPersistenceAcrossRestart/engine-dev_container_build_survives_restart failing, as it did on #15040.

With this backport, a detach of a service that is already stopping does nothing, and a graceful stop joins a stop already in flight instead of signaling again. A force stop still kills at once, and a service whose stop gives up is still stopped as if it were detached.

Differences from main

The change applied cleanly. One test needed adapting: on main, a container service's stop reports cleanup failures, which 0.21 doesn't have. The test that a joined stop still reports the stop result now uses a stand-in stop error instead of main's container service helpers.

The service tests in core (TestServices*) pass three times in a row under -race on this branch.

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 hits nested engines, which
can take a while to sync their cache on shutdown; the next engine then wipes
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, and still reports the stop result once the service exits.
- 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.

Backported from #14440. On 0.21 a container service's stop doesn't report
cleanup failures, so the test that a joined stop reports the stop result
uses a stand-in stop error instead of main's container service helpers.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit ba94c9c into backport-0.21 Nov 7, 2026
77 checks passed
@sipsma
sipsma deleted the sipsma/backport-0.21-late-detach-stop branch November 7, 2026 07:08
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