Fix PTY workdir and process group teardown

This commit is contained in:
2026-05-27 13:19:35 +01:00
parent da46340a82
commit b72a32bbc6
3 changed files with 93 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import (
"io"
"os"
"os/exec"
"syscall"
cpty "github.com/creack/pty"
)
@@ -19,11 +20,13 @@ type PTY struct {
// Start spawns argv with stdin/stdout/stderr attached to a new PTY sized
// (cols, rows). The returned PTY exposes the master fd for the parent to
// read from and write to.
func Start(argv []string, env []string, cols, rows uint16) (*PTY, error) {
func Start(argv []string, env []string, workDir string, cols, rows uint16) (*PTY, error) {
if len(argv) == 0 {
return nil, fmt.Errorf("pty: empty argv")
}
cmd := exec.Command(argv[0], argv[1:]...)
cmd.Dir = workDir
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
if env != nil {
cmd.Env = ensureTerm(env)
} else {
@@ -88,6 +91,10 @@ func (p *PTY) Close() error {
p.master = nil
}
if p.cmd != nil && p.cmd.Process != nil {
pid := p.cmd.Process.Pid
if pid > 0 {
_ = syscall.Kill(-pid, syscall.SIGKILL)
}
_ = p.cmd.Process.Kill()
}
return firstErr