Skip to content

Commit

Permalink
Send interrupt on Close on GOOS != windows
Browse files Browse the repository at this point in the history
Updates #19
  • Loading branch information
bep committed Jun 8, 2023
1 parent 5856bf0 commit 4d97c01
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"bytes"
"errors"
"io"
"os"
"os/exec"
"regexp"
"runtime"
"time"
)

Expand Down Expand Up @@ -55,8 +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 != "windows" {
// See https://github.com/bep/godartsass/issues/19
interruptErr = c.cmd.Process.Signal(os.Interrupt)
if interruptErr == os.ErrProcessDone {
interruptErr = nil
}
}

cmdErr := c.waitWithTimeout()

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

if interruptErr != nil {
return interruptErr
}

return cmdErr
}

Expand All @@ -79,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 4d97c01

Please sign in to comment.