From bafe6fbaee60593ff7e44594da8f19d5013c9770 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 30 Jan 2024 15:01:02 -0300 Subject: [PATCH] fix(conpty): close the pseudo console only once (#34) * fix(conpty): close the pseudo console only once Apparently, for reasons unknown, closing the pseudo console more than once causes the parent process to be killed as well. This should fix it for now, no idea why it happens though. Signed-off-by: Carlos Alexandro Becker * fix: review Signed-off-by: Carlos Alexandro Becker --------- Signed-off-by: Carlos Alexandro Becker --- exp/term/windows/conpty/conpty_windows.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/exp/term/windows/conpty/conpty_windows.go b/exp/term/windows/conpty/conpty_windows.go index 5e51f95e..f8bb3779 100644 --- a/exp/term/windows/conpty/conpty_windows.go +++ b/exp/term/windows/conpty/conpty_windows.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "sync" "syscall" "unsafe" @@ -28,6 +29,7 @@ type ConPty struct { inPipe, outPipe *os.File attrList *windows.ProcThreadAttributeListContainer size windows.Coord + closeOnce sync.Once } var ( @@ -103,12 +105,15 @@ func (p *ConPty) Fd() uintptr { // Close closes the ConPty device. func (p *ConPty) Close() error { - if p.attrList != nil { - p.attrList.Delete() - } - - windows.ClosePseudoConsole(*p.hpc) - return errors.Join(p.inPipe.Close(), p.outPipe.Close()) + var err error + p.closeOnce.Do(func() { + if p.attrList != nil { + p.attrList.Delete() + } + windows.ClosePseudoConsole(*p.hpc) + err = errors.Join(p.inPipe.Close(), p.outPipe.Close()) + }) + return err } // InPipe returns the ConPty input pipe.