🐛 修复 WebSSH 连接断开后遗留僵尸进程

This commit is contained in:
naiba
2021-09-30 12:01:01 +08:00
parent a25a1b128d
commit 7422de1269
4 changed files with 49 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
//go:build !windows
//+build !windows
// +build !windows
package pty
@@ -7,6 +7,7 @@ import (
"errors"
"os"
"os/exec"
"syscall"
opty "github.com/creack/pty"
)
@@ -53,9 +54,20 @@ func (pty *Pty) Setsize(cols, rows uint32) error {
})
}
func (pty *Pty) killChildProcess(c *exec.Cmd) error {
pgid, err := syscall.Getpgid(c.Process.Pid)
if err != nil {
// Fall-back on error. Kill the main process only.
c.Process.Kill()
}
// Kill the whole process group.
syscall.Kill(-pgid, syscall.SIGTERM)
return c.Wait()
}
func (pty *Pty) Close() error {
if err := pty.tty.Close(); err != nil {
return err
}
return pty.cmd.Process.Kill()
return pty.killChildProcess(pty.cmd)
}