Skip to content

fix(gateway): stop an exec when its caller goes away - #26

Merged
link108 merged 1 commit into
mainfrom
kindo/kill-exec-on-caller-disconnect
Oct 15, 2026
Merged

link108 merged 1 commit into
mainfrom
kindo/kill-exec-on-caller-disconnect

Conversation

@link108

@link108 link108 commented Oct 14, 2026

Copy link
Copy Markdown

Summary

ExecSandbox and ExecSandboxInteractive never noticed their gRPC caller leaving. The exec runs in a detached task whose output loop ignores send failures, so after a cancel or disconnect the SSH exec kept running until the command exited or timeout_seconds fired. The supervisor's process-group termination from #24 triggers on channel close, so it never ran on a disconnect. This PR makes the gateway drop the exec when its caller goes away, so the supervisor stops the command.

It also fixes an interactive-path regression introduced by #24: a caller that finished sending stdin could have its still-running command killed.

Related Issue

No upstream issue. This is a follow-up to #24, found in review: a disconnect, unlike a timeout, didn't stop the command.

Changes

  • race_relay_exec: a single tokio::select! (biased toward a finished exec) over the exec, the timeout_seconds deadline (none when 0), and tx.closed(), which resolves once every receiver of the response stream is gone. Both stream_exec_over_relay and stream_interactive_exec_over_relay use it.
    • Finished and TimedOut: unchanged; a timeout still sends exit 124.
    • CallerGone: drops the exec, which drops the russh client and closes the SSH channel and connection, so the supervisor terminates the process group. It sends no exit event, logs at info, and returns Ok. The relay proxy gets up to 5s to drain and is then aborted, so the task can't hang.
  • Interactive stdin task:
    • It's now aborted with the exec future (AbortOnDrop). Its write half kept the SSH connection open after the exec was dropped, which also leaked on the existing timeout path.
    • When the caller's input ends normally, it now half-closes with eof only, and the channel closes after the exit. Before, it sent eof and then close; since fix(supervisor): terminate an exec's process group when its channel closes #24, that close made the supervisor kill a command still running, e.g. echo data | exec -i -- slow-cmd. A failed input stream still closes the channel, because the caller is gone.
  • The non-interactive path already closes only after the exit, so it's unchanged.

Not changed, but found while checking similar paths: ForwardTcp has the same shape. After the caller leaves, a forward to a silent target lives on and keeps counting toward the per-token and per-sandbox connection limits. Worth a follow-up with the same tx.closed() arm.

Testing

New relay_exec_cancel_tests uses a fake supervisor: a real russh server on the far end of an in-memory relay that records exec, stdin, eof, channel close and disconnect.

  • Disconnect: dropping the caller's stream closes the SSH connection within 3s, below the 5s proxy-abort fallback, for both ExecSandbox and ExecSandboxInteractive.
  • Timeout: a 1s deadline still reports exit 124 and closes the connection.
  • Interactive stdin: stdin is sent and the input ends normally while the command runs. The server sees exec, stdin, eof, then the exit (code 7), with no channel close before the exit, and the caller receives exit 7.
  • race_relay_exec: finished beats a departed caller; a dropped receiver gives CallerGone; a deadline gives TimedOut.
  • The tests catch the bugs: with the tx.closed() arm disabled, the disconnect tests fail. With the old unconditional close() restored, the interactive stdin test fails.
  • QA:
    • cargo fmt --all -- --check OK, and cargo clippy -p openshell-server --all-targets --features test-support -- -D warnings clean on macOS and Linux.
    • cargo test -p openshell-server --features test-support --lib --tests on Linux (rust:1.95-bookworm + libz3): 1617 passed, 0 failed, 8 ignored, plus every integration binary. The new module passed 10 of 10 repeat runs. macOS can't link the tests without Z3.
  • End to end: run on a Kindo k3d cluster with the gateway built from this commit, on a supervisor that includes fix(supervisor): terminate an exec's process group when its channel closes #24.
    • A streamed sleep whose client disconnected at 10s was gone within 2s. The gateway logged ExecSandbox (relay): caller went away, abandoning command. On the previous gateway, the same process was still running 40s after the disconnect.
    • Regressions: a normal exec and a normal stream return full output and exit 0; a 20s timeout returns exit 124 with the process and its grandchild killed; a nohup … & background job still survives its command's normal exit.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable): no change needed. architecture/sandbox.md already says a channel close terminates the group, and this PR makes a caller departure close the channel.

ExecSandbox and ExecSandboxInteractive ran their exec in a detached task
that never noticed the gRPC caller leaving. The output loop ignored send
failures, so after a cancel or disconnect the SSH exec kept running until
the command exited or timeout_seconds fired. The supervisor's process-group
termination, which triggers on channel close, therefore never ran on a
disconnect.

Race the exec against tx.closed() alongside the deadline. When every
receiver is gone, drop the exec. That closes the SSH channel and connection,
so the supervisor terminates the command. No exit event is sent, and the
relay proxy is aborted if it does not drain within 5s.

Two related interactive-path fixes:
- The stdin task held a write half that kept the SSH connection open after
  the exec future was dropped; it is now aborted with the exec.
- When the caller's input ended normally, the stdin task sent eof and then
  close. Since the supervisor terminates a command whose channel closes
  while it runs, that could kill e.g. a piped 'exec -i' command before it
  finished. It now half-closes with eof only, and the channel closes after
  the exit. A failed input stream still closes the channel, since the caller
  is gone.

Signed-off-by: Cameron Motevasselani <cameron@kindo.ai>
@link108
link108 requested a review from mark-kindo October 14, 2026 22:44
@link108

link108 commented Oct 15, 2026

Copy link
Copy Markdown
Author

Local verification of the interactive path (ExecSandboxInteractive), on a Kindo k3d cluster with a supervisor that includes #24. A small grpc-js client calls the gateway directly with the cluster's own mTLS client certificate and issuer token. Each test was run on this PR's gateway (a228cbf) and, as a control, on the previous gateway (#24 without this PR).

Test Previous gateway This PR
Caller sends data\n and ends its input at 0.3s; command sleep 3; cat; echo done ❌ Killed at 0.3s with exec relay closed before the command reported an exit status and no output. This is the #24 regression. ✅ Output data\ndone\n at 3.0s, exit 0
Caller cancels at 5s with input still open; command sleep 916 ✅ Killed. The cancel errors the input stream, and the old stdin task closed the channel. ✅ Killed within 2s. Gateway logged ExecSandboxInteractive (relay): caller went away, abandoning command
Non-interactive stream; caller disconnects at 10s; command sleep (from the earlier run) ❌ Still running 40s later ✅ Gone within 2s

So the regression fix shows up live, and the case this PR mainly fixes, a disconnect of a non-interactive ExecSandbox caller, is the one that changed behavior. An interactive cancel was already handled through the input-stream error, and this PR keeps that path: an input error still closes the channel. A normal exec, a normal stream, a 20s timeout (exit 124, killed) and a background job surviving a normal exit were all re-checked on this PR's gateway as well.

@link108
link108 merged commit b867717 into main Oct 15, 2026
13 of 15 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