From 45daa44a99b72689bb63664a30f227dadbb7ab12 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 8 Mar 2022 11:48:58 -0600 Subject: [PATCH] fix: TTY being GC'd before command is ran (#412) * fix: TTY being GC'd before command is ran * Fix reference to tty --- pty/start_other.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pty/start_other.go b/pty/start_other.go index a527869e99..197f3631a5 100644 --- a/pty/start_other.go +++ b/pty/start_other.go @@ -6,6 +6,7 @@ package pty import ( "os" "os/exec" + "runtime" "syscall" "github.com/creack/pty" @@ -29,6 +30,13 @@ func startPty(cmd *exec.Cmd) (PTY, *os.Process, error) { _ = ptty.Close() return nil, nil, xerrors.Errorf("start: %w", err) } + go func() { + // The GC can garbage collect the TTY FD before the command + // has finished running. See: + // https://github.com/creack/pty/issues/127#issuecomment-932764012 + _ = cmd.Wait() + runtime.KeepAlive(ptty) + }() oPty := &otherPty{ pty: ptty, tty: tty,