Skip to content

Commit

Permalink
Fix npe xiantang (#247)
Browse files Browse the repository at this point in the history
⭐️ close #246
* fix NPE for find process
* add test for no such process
  • Loading branch information
xiantang authored Jan 25, 2022
1 parent 1c27eff commit 9a9b1f6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
21 changes: 9 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ module github.com/cosmtrek/air
go 1.17

require (
github.com/fatih/color v1.10.0
github.com/fsnotify/fsnotify v1.4.9
github.com/imdario/mergo v0.3.12
github.com/pelletier/go-toml v1.8.1
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
github.com/fatih/color v1.10.0
github.com/fsnotify/fsnotify v1.4.9
github.com/imdario/mergo v0.3.12
github.com/pelletier/go-toml v1.8.1
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
)

require (
github.com/mitchellh/go-ps v1.0.0
github.com/pkg/errors v0.9.1
)
require github.com/pkg/errors v0.9.1

require (
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
)
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
)
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -21,7 +19,6 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6 changes: 0 additions & 6 deletions runner/pid

This file was deleted.

16 changes: 9 additions & 7 deletions runner/util_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"syscall"
"time"

"github.com/mitchellh/go-ps"
"github.com/pkg/errors"
)

Expand All @@ -22,15 +21,18 @@ func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) {
}
time.Sleep(e.config.Build.KillDelay * time.Millisecond)
}

// find process by pid and kill it and its children by group id
proc, err := ps.FindProcess(pid)
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err != nil {
return pgid, errors.Wrapf(err, "failed to get pgid, pid %v", pid)
}
err = syscall.Kill(-pgid, syscall.SIGKILL)
if err != nil {
return pid, errors.Wrapf(err, "failed to find process %d", pid)
return pid, errors.Wrapf(err, "failed to kill process by pgid %v", pgid)
}
err = syscall.Kill(-proc.Pid(), syscall.SIGKILL)
// Wait releases any resources associated with the Process.
_, err = cmd.Process.Wait()
if err != nil {
return pid, errors.Wrapf(err, "failed to kill process %d", pid)
return pid, err
}

e.mainDebug("killed process pid %d successed", pid)
Expand Down
23 changes: 23 additions & 0 deletions runner/util_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package runner

import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"testing"
"time"
)
Expand Down Expand Up @@ -143,6 +145,27 @@ func TestAdaptToVariousPlatforms(t *testing.T) {
}
}

func Test_killCmd_no_process(t *testing.T) {
e := Engine{
config: &config{
Build: cfgBuild{
SendInterrupt: false,
},
},
}
_, err := e.killCmd(&exec.Cmd{
Process: &os.Process{
Pid: 9999,
},
})
if err == nil {
t.Errorf("expected error but got none")
}
if !errors.Is(err, syscall.ESRCH) {
t.Errorf("expected '%s' but got '%s'", syscall.ESRCH, errors.Unwrap(err))
}
}

func Test_killCmd_SendInterrupt_false(t *testing.T) {
_, b, _, _ := runtime.Caller(0)

Expand Down

0 comments on commit 9a9b1f6

Please sign in to comment.