Repository navigation
fix(gateway): stop an exec when its caller goes away - #26
Conversation
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>
|
Local verification of the interactive path (
So the regression fix shows up live, and the case this PR mainly fixes, a disconnect of a non-interactive |
Summary
ExecSandboxandExecSandboxInteractivenever 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 ortimeout_secondsfired. 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 singletokio::select!(biased toward a finished exec) over the exec, thetimeout_secondsdeadline (none when 0), andtx.closed(), which resolves once every receiver of the response stream is gone. Bothstream_exec_over_relayandstream_interactive_exec_over_relayuse it.AbortOnDrop). Its write half kept the SSH connection open after the exec was dropped, which also leaked on the existing timeout path.eofonly, and the channel closes after the exit. Before, it senteofand thenclose; 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.Not changed, but found while checking similar paths:
ForwardTcphas 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 sametx.closed()arm.Testing
New
relay_exec_cancel_testsuses 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.ExecSandboxandExecSandboxInteractive.race_relay_exec: finished beats a departed caller; a dropped receiver gives CallerGone; a deadline gives TimedOut.tx.closed()arm disabled, the disconnect tests fail. With the old unconditionalclose()restored, the interactive stdin test fails.cargo fmt --all -- --checkOK, andcargo clippy -p openshell-server --all-targets --features test-support -- -D warningsclean on macOS and Linux.cargo test -p openshell-server --features test-support --lib --testson 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.sleepwhose client disconnected at 10s was gone within 2s. The gateway loggedExecSandbox (relay): caller went away, abandoning command. On the previous gateway, the same process was still running 40s after the disconnect.nohup … &background job still survives its command's normal exit.Checklist
architecture/sandbox.mdalready says a channel close terminates the group, and this PR makes a caller departure close the channel.