Clean up the agent socket and working files when a signal ends the tool #17

Closed
opened 2026-09-21 09:18:00 +02:00 by clawbot · 1 comment
Collaborator

cli.Main runs the command tree with no signal handling. When keyfunc ssh to is ended by SIGINT, SIGTERM or SIGHUP (a closed terminal, a supervising script), the process dies before its deferred cleanup runs: the agent's temporary directory and socket stay behind in the temporary directory, and ssh install leaves its working directory holding a copy of the host's authorized_keys. The README promises that ssh to "removes the socket and directory on the way out".

Implementation

  • In internal/cli/cli.go, run the tree with ExecuteContext on a context from signal.NotifyContext for SIGINT, SIGTERM and SIGHUP. The commands already start ssh, sftp and the mnemonic command with exec.CommandContext(cmd.Context(), ...), so a signal then ends the child and the deferred cleanup runs.
  • Check what exec.CommandContext does on cancel (it kills the child): for ssh to set Cmd.Cancel to send SIGTERM instead, so ssh restores the terminal before it goes.
  • The exit status after a signal is 1 unless ssh reported one of its own.

Definition of done

  • A test starts ssh to against a fake ssh that sleeps, cancels the context, and finds the agent directory gone afterwards.
  • README "ssh to" says what happens on a signal, in one sentence.
  • make check green. Branch cut from next, PR base next. Wait for #16 to land first: it changes the same function in to.go.

Model: fable-5-1

`cli.Main` runs the command tree with no signal handling. When `keyfunc ssh to` is ended by SIGINT, SIGTERM or SIGHUP (a closed terminal, a supervising script), the process dies before its deferred cleanup runs: the agent's temporary directory and socket stay behind in the temporary directory, and `ssh install` leaves its working directory holding a copy of the host's `authorized_keys`. The README promises that `ssh to` "removes the socket and directory on the way out". ## Implementation - In `internal/cli/cli.go`, run the tree with `ExecuteContext` on a context from `signal.NotifyContext` for SIGINT, SIGTERM and SIGHUP. The commands already start `ssh`, `sftp` and the mnemonic command with `exec.CommandContext(cmd.Context(), ...)`, so a signal then ends the child and the deferred cleanup runs. - Check what `exec.CommandContext` does on cancel (it kills the child): for `ssh to` set `Cmd.Cancel` to send SIGTERM instead, so `ssh` restores the terminal before it goes. - The exit status after a signal is 1 unless `ssh` reported one of its own. ## Definition of done - A test starts `ssh to` against a fake `ssh` that sleeps, cancels the context, and finds the agent directory gone afterwards. - README "ssh to" says what happens on a signal, in one sentence. - `make check` green. Branch cut from `next`, PR base `next`. Wait for https://git.eeqj.de/sneak/keyfunc/issues/16 to land first: it changes the same function in `to.go`. Model: fable-5-1
clawbot self-assigned this 2026-09-21 09:18:00 +02:00
Author
Collaborator

PR: #30

Main now runs the command tree on a signal context for SIGINT, SIGTERM and SIGHUP, so a signal ends the child ssh or sftp and the deferred cleanup that removes the agent socket and directory and the install working files still runs; ssh to is ended with SIGTERM so ssh restores the terminal.

Model: opus-4-8

PR: https://git.eeqj.de/sneak/keyfunc/pulls/30 `Main` now runs the command tree on a signal context for SIGINT, SIGTERM and SIGHUP, so a signal ends the child ssh or sftp and the deferred cleanup that removes the agent socket and directory and the install working files still runs; `ssh to` is ended with SIGTERM so ssh restores the terminal. Model: opus-4-8
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/keyfunc#17