Skip to content

Commit 9a9b1f6

Browse files
authored
Fix npe xiantang (#247)
⭐️ close #246 * fix NPE for find process * add test for no such process
1 parent 1c27eff commit 9a9b1f6

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

go.mod

+9-12
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ module github.com/cosmtrek/air
33
go 1.17
44

55
require (
6-
github.com/fatih/color v1.10.0
7-
github.com/fsnotify/fsnotify v1.4.9
8-
github.com/imdario/mergo v0.3.12
9-
github.com/pelletier/go-toml v1.8.1
10-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
6+
github.com/fatih/color v1.10.0
7+
github.com/fsnotify/fsnotify v1.4.9
8+
github.com/imdario/mergo v0.3.12
9+
github.com/pelletier/go-toml v1.8.1
10+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
1111
)
1212

13-
require (
14-
github.com/mitchellh/go-ps v1.0.0
15-
github.com/pkg/errors v0.9.1
16-
)
13+
require github.com/pkg/errors v0.9.1
1714

1815
require (
19-
github.com/mattn/go-colorable v0.1.8 // indirect
20-
github.com/mattn/go-isatty v0.0.12 // indirect
21-
)
16+
github.com/mattn/go-colorable v0.1.8 // indirect
17+
github.com/mattn/go-isatty v0.0.12 // indirect
18+
)

go.sum

-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ
1010
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
1111
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
1212
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
13-
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
14-
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
1513
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
1614
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
1715
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -21,7 +19,6 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
2119
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2220
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
2321
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2522
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2623
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
2724
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

runner/pid

-6
This file was deleted.

runner/util_darwin.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"syscall"
88
"time"
99

10-
"github.com/mitchellh/go-ps"
1110
"github.com/pkg/errors"
1211
)
1312

@@ -22,15 +21,18 @@ func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) {
2221
}
2322
time.Sleep(e.config.Build.KillDelay * time.Millisecond)
2423
}
25-
26-
// find process by pid and kill it and its children by group id
27-
proc, err := ps.FindProcess(pid)
24+
pgid, err := syscall.Getpgid(cmd.Process.Pid)
25+
if err != nil {
26+
return pgid, errors.Wrapf(err, "failed to get pgid, pid %v", pid)
27+
}
28+
err = syscall.Kill(-pgid, syscall.SIGKILL)
2829
if err != nil {
29-
return pid, errors.Wrapf(err, "failed to find process %d", pid)
30+
return pid, errors.Wrapf(err, "failed to kill process by pgid %v", pgid)
3031
}
31-
err = syscall.Kill(-proc.Pid(), syscall.SIGKILL)
32+
// Wait releases any resources associated with the Process.
33+
_, err = cmd.Process.Wait()
3234
if err != nil {
33-
return pid, errors.Wrapf(err, "failed to kill process %d", pid)
35+
return pid, err
3436
}
3537

3638
e.mainDebug("killed process pid %d successed", pid)

runner/util_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package runner
22

33
import (
4+
"errors"
45
"io/ioutil"
56
"os"
67
"os/exec"
78
"path/filepath"
89
"runtime"
910
"strconv"
1011
"strings"
12+
"syscall"
1113
"testing"
1214
"time"
1315
)
@@ -143,6 +145,27 @@ func TestAdaptToVariousPlatforms(t *testing.T) {
143145
}
144146
}
145147

148+
func Test_killCmd_no_process(t *testing.T) {
149+
e := Engine{
150+
config: &config{
151+
Build: cfgBuild{
152+
SendInterrupt: false,
153+
},
154+
},
155+
}
156+
_, err := e.killCmd(&exec.Cmd{
157+
Process: &os.Process{
158+
Pid: 9999,
159+
},
160+
})
161+
if err == nil {
162+
t.Errorf("expected error but got none")
163+
}
164+
if !errors.Is(err, syscall.ESRCH) {
165+
t.Errorf("expected '%s' but got '%s'", syscall.ESRCH, errors.Unwrap(err))
166+
}
167+
}
168+
146169
func Test_killCmd_SendInterrupt_false(t *testing.T) {
147170
_, b, _, _ := runtime.Caller(0)
148171

0 commit comments

Comments
 (0)