Skip to content

Commit

Permalink
fix(conpty): close the pseudo console only once (#34)
Browse files Browse the repository at this point in the history
* 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 <caarlos0@users.noreply.github.com>

* fix: review

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored Jan 30, 2024
1 parent 0ee7136 commit bafe6fb
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions exp/term/windows/conpty/conpty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"sync"
"syscall"
"unsafe"

Expand All @@ -28,6 +29,7 @@ type ConPty struct {
inPipe, outPipe *os.File
attrList *windows.ProcThreadAttributeListContainer
size windows.Coord
closeOnce sync.Once
}

var (
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit bafe6fb

Please sign in to comment.