Skip to content

[ENG-13007] fix(supervisor): hold a displaced gateway only while it is still relaying - #31

Merged
link108 merged 1 commit into
mainfrom
cameron/supervisor-hold-only-departing-gateways
Nov 9, 2026
Merged

link108 merged 1 commit into
mainfrom
cameron/supervisor-hold-only-departing-gateways

Conversation

@link108

@link108 link108 commented Nov 5, 2026

Copy link
Copy Markdown

Summary

Fixes ENG-13007. The supervisor kept a session to a displaced gateway replica for the whole OPENSHELL_FLEET_DRAIN_SECS window. Since #30, Kindo's chart sets that window to the gateway's shutdown drain (about 3 h). So a healthy replica that a scale-out merely pushed out of a sandbox's subset kept its session for hours, and a scale-out freed no sessions on the replicas it displaced. The gateway HPA scales on memory, so it could keep scaling toward maxReplicas.

The opposite bug existed before #30: a displaced replica that was still relaying a long exec was dropped after 120 s, which killed that exec.

Changes

  • Two windows (gateway_fleet.rs):
    • Every departed member is held for a fixed 120 s skew window (SKEW_WINDOW), measured from first absence, as before. This keeps the superset guarantee for callers' lagging views.
    • Past that window, a member is held only while its session carries an in-flight relay, up to the OPENSHELL_FLEET_DRAIN_SECS ceiling.
    • An ended session still drops immediately, and returning to the subset clears both windows.
    • A configured drain below 120 s shortens both windows, so 0 still means "drop at once" for an idle member.
  • Relay counting (supervisor_session.rs):
    • An Arc<AtomicUsize> per member session, with an RAII RelayGuard.
    • The guard is taken on RelayOpen before the spawn and moved into the relay task, so completion, error, panic and abort (even before first poll) all release it.
    • SSH relays (exec and shell) and TCP relays (port forwards) both count.
  • Docs: the "Add before drop" module docs and the doc comments on the env var in openshell-core.

There's no new env var: the skew window is derived from DNS TTL and re-resolve intervals, which operators don't tune per deployment. With the variable unset, stock behaviour is unchanged.

Testing

  • 7 new tests, all on paused tokio time:
    • An idle departed member drops at 120 s, not before.
    • A relaying member is held until its relay ends.
    • The ceiling still applies to a member that is relaying.
    • A relay ending early doesn't shorten the skew window.
    • A ceiling shorter than the skew window wins.
    • A returning member restarts both windows.
    • The count falls on abort, abort before first poll, panic, and drop.
  • The 7 existing fleet tests pass unmodified.
  • cargo fmt --check is clean. cargo clippy shows no new warnings in the changed files.
  • cargo test -p openshell-supervisor-process: 178 passed.
  • cargo test -p openshell-core: 479 passed, 1 failed. The failure is resolve_members_builds_addresses_from_resolved_ips, which resolves localhost through public DNS and fails in that environment. This PR only changes doc comments in that crate.
  • Not yet run on a cluster. ENG-13007's acceptance criteria call for a k3d scale-out with 3+ replicas and subsetSize 2.

Checklist

  • Conventional commit, DCO sign-off

🤖 Generated with Claude Code

…ying

A departed gateway member was held for the whole OPENSHELL_FLEET_DRAIN_SECS
window. With the drain set to a gateway's shutdown drain (hours), a healthy
replica that a scale-out merely displaced kept its session for as long as
the session stayed up.

Split the hold in two. Every departed member is held for a fixed 120 s skew
window (callers' views may lag that much), measured from first absence as
before. Past it, the member is held only while its session carries an
in-flight relay, up to the OPENSHELL_FLEET_DRAIN_SECS ceiling. A drain below
the skew window shortens both, so a short or zero drain keeps its meaning.
An ended session still drops immediately, and returning to the subset
clears the hold.

Relays are counted per session with an RAII guard taken when RelayOpen is
received and moved into the relay task, so completion, error, panic and
abort all release it. SSH and TCP relays both count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cameron motevasselani <cameron@kindo.ai>
@linear-code

linear-code Bot commented Nov 5, 2026

Copy link
Copy Markdown

ENG-13007

@link108

link108 commented Nov 6, 2026

Copy link
Copy Markdown
Author

/ok to test

@link108

link108 commented Nov 6, 2026

Copy link
Copy Markdown
Author

/ok

@link108

link108 commented Nov 6, 2026 •

Copy link
Copy Markdown
Author

Test results

Nothing in CI or the unit tests points at this PR. Cluster results: #31 (comment)

CI on the PR

Branch Checks and Helm Lint, run by hand
I pushed pull-request/31 at this PR's head (5fad9c1), which is what copy-pr-bot would have done.

Unit tests, run locally:

  • cargo test -p openshell-supervisor-process: 178 passed. That includes 7 new tests on paused tokio time:
    • an idle departed member drops at 120 s
    • a relaying member is held until its relay ends
    • the ceiling still applies while relaying
    • a relay that ends early doesn't shorten the skew window
    • a ceiling below 120 s wins
    • a returning member restarts both windows
    • the relay count falls on abort, abort before first poll, panic and drop
  • The 7 existing fleet tests pass unchanged.
  • cargo test -p openshell-core: 479 passed, 1 failed. The failure is resolve_members_builds_addresses_from_resolved_ips, which looks up localhost through DNS and fails in that environment. This PR only changes doc comments in that crate. It fails the same way on ci: run branch checks on PRs and merge queue with hosted runners #25's hosted macOS runner.

Tested on a cluster afterwards: k3d end-to-end results are in #31 (comment).

@link108
link108 merged commit 6c8c452 into main Nov 9, 2026
48 of 62 checks passed
@link108
link108 deleted the cameron/supervisor-hold-only-departing-gateways branch November 9, 2026 21:07
@link108

link108 commented Nov 11, 2026

Copy link
Copy Markdown
Author

k3d end-to-end test

I ran #31 on a local k3d cluster and compared it with a control on main's current pin (#30). Setup: 3 gateway replicas, subsetSize 2, gateway HPA off, 8 sandboxes per phase, driven through sandbox-api. Per-replica session counts come from the gateway's supervisor session: accepted / ended log lines. Supervisor holds and drops come from the supervisor's own logs, with RUST_LOG raised on the sandbox template.

The gateway image is byte-identical between #30 and #31; only the supervisor changes.

Scenario Before #31 (#30) With #31
Scale-out 3→5, idle sandboxes Displaced members still held 11 min later. Old replicas kept all their sessions (20 sessions for 8 sandboxes). All 6 displaced members dropped 120–147 s after they left the subset. Old replicas went 5/6/5 → 4/3/3, and the fleet went back to 16 sessions.
Relaying replica leaves the subset — Held about 5.5 min while relaying. Each exec streamed to completion (exit 0). Each member dropped 11–19 s after its exec ended.
Delete the gateway relaying 3 execs — The gateway waited for in_flight=3 and exited as the last exec ended. 8/8 long execs and 40/40 short execs succeeded.

Notes:

  • An idle member drops on the first refresh pass after the 120 s skew window, so 0–27 s past 120 s. A relaying member drops within one refresh interval after its relay ends.
  • A scale-out rarely displaces the replica relaying a sandbox's exec, because that replica is the sandbox's top-ranked member. To test the relaying case, I failed only that replica's readiness probe, which took it out of DNS. The supervisor sees the same thing as after a displacement: a member missing from the resolved fleet.
  • Not covered: the OPENSHELL_FLEET_DRAIN_SECS ceiling on a relaying member, and a member that returns within the window.

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.

1 participant