Skip to content

Commit c5cdb5a

Browse files
rscwheatman
authored andcommitted
os: make Process.Signal 'process finished' error consistent on Unix
While we're here, fix the implementation of Release on both Unix and Windows: Release is supposed to make Signal an error. While we're here, make sure we never Signal pid 0. (Don't try this at home.) Fixes golang#7658. LGTM=r R=golang-codereviews, r CC=golang-codereviews, iant https://golang.org/cl/152240043
1 parent b5c2723 commit c5cdb5a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/os/exec_unix.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,26 @@ func (p *Process) wait() (ps *ProcessState, err error) {
3434
return ps, nil
3535
}
3636

37+
var errFinished = errors.New("os: process already finished")
38+
3739
func (p *Process) signal(sig Signal) error {
38-
if p.done() {
39-
return errors.New("os: process already finished")
40-
}
4140
if p.Pid == -1 {
4241
return errors.New("os: process already released")
4342
}
43+
if p.Pid == 0 {
44+
return errors.New("os: process not initialized")
45+
}
46+
if p.done() {
47+
return errFinished
48+
}
4449
s, ok := sig.(syscall.Signal)
4550
if !ok {
4651
return errors.New("os: unsupported signal type")
4752
}
4853
if e := syscall.Kill(p.Pid, s); e != nil {
54+
if e == syscall.ESRCH {
55+
return errFinished
56+
}
4957
return e
5058
}
5159
return nil

src/os/exec_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func terminateProcess(pid, exitcode int) error {
5353
}
5454

5555
func (p *Process) signal(sig Signal) error {
56+
if p.handle == uintptr(syscall.InvalidHandle) {
57+
return syscall.EINVAL
58+
}
5659
if p.done() {
5760
return errors.New("os: process already finished")
5861
}

0 commit comments

Comments
 (0)