Skip to content

Commit

Permalink
Ensure clenup is run even when ssh child is killed by interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 6, 2023
1 parent a6f13a6 commit 8dfe1fc
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions kittens/ssh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,16 @@ func run_ssh(ssh_args, server_args, found_extra_args []string) (rc int, err erro
}
sigs := make(chan os.Signal, 8)
signal.Notify(sigs, unix.SIGINT, unix.SIGTERM)
defer func() {
_ = term.WriteAllString(restore_escape_codes)
term.RestoreAndClose()
signal.Reset()
}()
cleaned_up := false
cleanup := func() {
if !cleaned_up {
_ = term.WriteAllString(restore_escape_codes)
term.RestoreAndClose()
signal.Reset()
cleaned_up = true
}
}
defer cleanup()
err = get_remote_command(&cd)
if err != nil {
return 1, err
Expand Down Expand Up @@ -780,6 +785,7 @@ func run_ssh(ssh_args, server_args, found_extra_args []string) (rc int, err erro
var exit_err *exec.ExitError
if errors.As(err, &exit_err) {
if state := exit_err.ProcessState.String(); state == "signal: interrupt" {
cleanup()
_ = unix.Kill(os.Getpid(), unix.SIGINT)
// Give the signal time to be delivered
time.Sleep(20 * time.Millisecond)
Expand Down

0 comments on commit 8dfe1fc

Please sign in to comment.