Repository navigation
fix(supervisor): terminate an exec's process group when its channel closes - #24
Merged
Merged
Conversation
…loses When the gateway's exec deadline expires, or a caller cancels an exec stream, the gateway closes the SSH channel. The supervisor did nothing to the child, so the command kept running until it exited on its own or the pod went away. Each exec, shell, and sftp channel now runs its command as the leader of its own process group. If the channel or the SSH connection closes before the leader exits, the supervisor sends SIGTERM to the group, then SIGKILL after a 5s grace period if any member is still running. The leader stays unreaped until the escalation finishes, so a signal never reaches a recycled PID or process group. Canonical main-session attachments are unaffected. Signed-off-by: Cameron Motevasselani <cameron@kindo.ai>
craig-kindo
approved these changes
Oct 14, 2026
mark-kindo
approved these changes
Oct 14, 2026
2 of 3 tasks
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
When the gateway's exec deadline (
timeout_seconds) expires, or a caller cancels an exec stream, the gateway sends exit 124 and closes the SSH channel, but the supervisor never stops the child. The command keeps running until it exits on its own or the pod goes away. This PR terminates the exec's process group when its channel or SSH connection closes before the command has exited.Related Issue
No upstream issue. Kindo needs this so that exec deadlines actually bound execution in sandboxes. Downstream discussion: sandbox-api exec deadlines (UsableMachines/mono#14836).
Changes
process_group(0); std callssetpgidbefore the existingpre_exechooks). PTY execs already callsetsid.ExecProcessGroup(exec_group.rs): on channel close (channel_close) or connection drop (Drop for SshHandler) before the leader exits, it sendsSIGTERMto the group. A background thread then waits up toEXEC_TERMINATION_GRACE(5s) and sendsSIGKILLif any member is still running. On Linux it checks/procfor group members; elsewhere it falls back to whether the leader is alive. The async handler never blocks.waitid(EXITED | NOWAIT)and records it under the same lock that signals are sent under; only then does it reap.SIGKILLstep.managed_children, so the orphan reaper never reaps it first.ProcessActivityevents for the SIGTERM, the SIGKILL escalation, and anykillpgfailure other thanESRCH.architecture/sandbox.md.Known limits:
setsid) are not killed./proc.Testing
cargo fmt --all -- --checkpasses.cargo clippy -p openshell-supervisor-process --all-targets -- -D warningsis clean on macOS and Linux.cargo test -p openshell-supervisor-processpasses: 169 passed on macOS, and 271 passed on Linux (rust:1.95-bookworm container).exec_grouptests passed 25 of 25 repeated runs on Linux. They cover:cargo check -p openshell-sandboxpasses.mise run pre-commitwas not run. cargo fmt, clippy, and tests were run directly instead.mise run e2e). The tests driveExecProcessGroupwith the same spawn setup asssh.rs. The russh spawn paths can't be unit-tested without a russhHandle.Checklist