Skip to content

Commit

Permalink
Work around close issue on Ubuntu
Browse files Browse the repository at this point in the history
Updates #19
  • Loading branch information
bep committed Jun 8, 2023
1 parent 0d7a52f commit 3f114da
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ func (c conn) Start() error {

// Close closes conn's WriteCloser, ReadClosers, and waits for the command to finish.
func (c conn) Close() error {

writeErr := c.WriteCloser.Close()
readErr := c.readerCloser.Close()
var interruptErr error

if runtime.GOOS == "linux" {
// See https://github.com/bep/godartsass/issues/19
c.cmd.Process.Signal(os.Interrupt)
interruptErr = c.cmd.Process.Signal(os.Interrupt)
if interruptErr == os.ErrProcessDone {
interruptErr = nil
}
}

cmdErr := c.waitWithTimeout()

if writeErr != nil {
Expand All @@ -73,6 +80,10 @@ func (c conn) Close() error {
return readErr
}

if interruptErr != nil {
return interruptErr
}

return cmdErr
}

Expand All @@ -85,10 +96,14 @@ func (c conn) waitWithTimeout() error {
go func() { result <- c.cmd.Wait() }()
select {
case err := <-result:
if _, ok := err.(*exec.ExitError); ok {
if eerr, ok := err.(*exec.ExitError); ok {
if eerr.Error() == "signal: interrupt" {
return nil
}
if brokenPipeRe.MatchString(c.stdErr.String()) {
return nil
}

}
return err
case <-time.After(5 * time.Second):
Expand Down

0 comments on commit 3f114da

Please sign in to comment.