Repository navigation
core: don't let a late Detach force-kill a service being stopped - #14440
Merged
Merged
Conversation
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]>
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
self-requested a review
October 1, 2026 00:48
grouville
approved these changes
Oct 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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
TestCachePersistencein trace 70fbaf5a. The test fails atengine_persistence_test.go:533because a nested engine's cache didn't survive a restart.Root cause
An explicit graceful
Service.stop(Services.stopwithforce=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: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):Service.stop.detach: stopping bindings=-1.service stop interrupted, then SIGKILL. The engine exits 137.clean_shutdownunset and wipes its store.The late Detach is routine. In the CI logs we inspected, every
detach: stoppingrecord hadbindings=-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_restartsall passed, but logged:detach: stopping bindings=-1,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.
kill=trueand session teardown still SIGKILL immediately, even while a graceful stop is in flight.TerminateGracePeriod.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
kill=trueand session teardown request SIGKILL immediately.Tests
core/services_stop_test.goruns the service manager insynctestbubbles against the realTerminateGracePeriod, 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.kill=true: the Detach policy takes over, with SIGTERM, then SIGKILL afterTerminateGracePeriod.TerminateGracePeriod, andkill=trueescalates 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:
detach: service already stopping, the late Detaches joining the stop,bindings=-1,