Repository navigation
[ENG-12545] fix(gateway): let a terminating gateway finish in-flight execs before it exits - #30
Merged
Merged
Conversation
… it exits An exec is relayed over the supervisor session the replica holds, so it cannot fail over. On SIGTERM the gateway stopped accepting connections, withdrew its liveness records and returned, and the runtime dropped the detached connection tasks still relaying execs, failing each one partway through. Shutdown now drains. The session registry stops opening relays and answers a new exec with the same refusal a replica without the session gives, naming the peers that will outlast it, so callers route the work elsewhere and the refusal still proves nothing ran. Liveness keepers stop renewing, so the one-pass withdrawal at shutdown is not undone. Each exec handler takes a guard before it asks for a relay, and shutdown waits for the guards to drop, up to OPENSHELL_SHUTDOWN_DRAIN_SECONDS (default 0, which keeps the old behaviour). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cameron motevasselani <cameron@kindo.ai>
A departed subset member is held for the drain window so the execs riding its session can finish. With the window sized to a gateway's shutdown drain, which can be hours, the session loop kept redialing a replica that had exited for that whole time, on a 30-second backoff. The hold now lasts only while the session lives. The fleet loop marks a departed member's session, its loop stops instead of reconnecting once that session fails, and the reaper drops the entry. A member that returns after its session ended is redialed rather than left with a dead entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cameron motevasselani <cameron@kindo.ai>
markddavidoff
approved these changes
Nov 5, 2026
Aashman-Rastogi
approved these changes
Nov 5, 2026
mark-kindo
approved these changes
Nov 5, 2026
1 task done
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.
Summary
A terminating gateway now finishes the execs it is relaying before it exits, instead of dropping them partway through. A second commit stops supervisors from redialing a departed gateway once its session has ended, which matters once the supervisor's hold on a departed gateway is sized to that drain. Kindo ticket: ENG-12545.
Related Issue
No issue required: fork-only fix, tracked in Kindo's Linear as ENG-12545.
Why
An exec is relayed over the supervisor session held by one replica, so it cannot fail over. On SIGTERM the gateway stopped its listeners, withdrew its liveness records and returned. The connection tasks still relaying execs are detached, so the runtime dropped them, and every in-flight exec failed after the command might already have had side effects. Every gateway rollout and node drain did this.
Changes
Refuse new relays while draining.
SupervisorSessionRegistry::begin_drainmakessession_or_unavailableanswer with the existing no-session refusal, carrying the serving-replicas hint for the peers. Callers already route that refusal elsewhere, and it is still reached before dispatch, so it still proves nothing ran.Stop renewing liveness. Keeper loops exit once draining, so a renew failure can't re-announce a record the shutdown just withdrew.
Count in-flight execs (
exec_drain.rs). Both exec handlers take a guard before asking the registry for a relay. Shutdown starts draining the registry first and waits on the guards second, so every exec is either refused or waited for; no interleaving admits one without counting it.Wait, bounded. After withdrawing liveness, shutdown waits for the count to reach 0, up to
OPENSHELL_SHUTDOWN_DRAIN_SECONDS/--shutdown-drain-seconds. The default of 0 keeps the old behaviour. It exits as soon as the last exec finishes, so the value is a ceiling, not a delay.Supervisor: hold a departed gateway only while its session lives (second commit). A departed subset member is held for
OPENSHELL_FLEET_DRAIN_SECSso the execs riding its session can finish. Once that hold is sized to the gateway drain (hours), the old session loop would keep redialing an exited replica on a 30 s backoff for the whole window: about 360 failed dials per sandbox per exited gateway. Now the fleet loop marks the member departed, its session loop stops instead of reconnecting once the session fails, and the reaper drops the entry. A member that returns after its session ended is redialed rather than left with a dead entry.Deployment note: the pod's
terminationGracePeriodSecondsmust exceed preStop plus the drain. Supervisors should hold a departed replica's session (OPENSHELL_FLEET_DRAIN_SECS) for at least as long, or they drop it after the default 120 s and kill the exec anyway. Kindo's chart sets both.Testing
cargo test: openshell-server 1621 passed, openshell-core 495, plus the other suites. 5 new tests:exec_drain: idle at once; returns when the last exec finishes; gives up at the deadline.supervisor_session: a draining replica refuses new relays, names its peers and dispatches nothing; a draining replica does not announce liveness.gateway_fleet: a departed member is dropped once its session ends; a returning member whose session ended is redialed. Both fail without the fix: they time out because the loop keeps redialing.cargo fmt --checkclean. Clippy reports nothing in the changed lines.rollout restartRepeated with the supervisor built from the second commit (a fresh sandbox): the exec survived the drain and all new execs succeeded. The stop-redialing behaviour isn't visible live, because the supervisor logs only warnings to stderr; the unit tests cover it.
The draining replica refused 4 execs in the first ~7 s; each was rerouted and succeeded. Traffic then left it once its records were gone.
Known edge: a relay that is pending at SIGTERM and needs a new TCP connection for its reverse CONNECT can still fail, because the listeners are already stopped. That window is milliseconds.
Checklist
🤖 Generated with Claude Code