Skip to content

Commit f14119b

Browse files
urandom2tklauser
authored andcommitted
os: export errFinished as ErrProcessDone
(*Process).Signal returns an error sentinel, previously errFinished, when (*Process).done or syscall.ESRCH. Callers would like the ability to test for this state, so the value has been exported as ErrProcessDone. Fixes #39444 Change-Id: I510e7647cc032af290180de5149f35ab7b09a526 Reviewed-on: https://go-review.googlesource.com/c/go/+/242998 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Trust: Tobias Klauser <[email protected]>
1 parent 12a2e72 commit f14119b

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/os/exec_unix.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func (p *Process) wait() (ps *ProcessState, err error) {
5959
return ps, nil
6060
}
6161

62-
var errFinished = errors.New("os: process already finished")
62+
// ErrProcessDone indicates a Process has finished.
63+
var ErrProcessDone = errors.New("os: process already finished")
6364

6465
func (p *Process) signal(sig Signal) error {
6566
if p.Pid == -1 {
@@ -71,15 +72,15 @@ func (p *Process) signal(sig Signal) error {
7172
p.sigMu.RLock()
7273
defer p.sigMu.RUnlock()
7374
if p.done() {
74-
return errFinished
75+
return ErrProcessDone
7576
}
7677
s, ok := sig.(syscall.Signal)
7778
if !ok {
7879
return errors.New("os: unsupported signal type")
7980
}
8081
if e := syscall.Kill(p.Pid, s); e != nil {
8182
if e == syscall.ESRCH {
82-
return errFinished
83+
return ErrProcessDone
8384
}
8485
return e
8586
}

src/os/exec_unix_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
6+
7+
package os_test
8+
9+
import (
10+
"internal/testenv"
11+
. "os"
12+
"testing"
13+
)
14+
15+
func TestErrProcessDone(t *testing.T) {
16+
testenv.MustHaveGoBuild(t)
17+
path, err := testenv.GoTool()
18+
if err != nil {
19+
t.Errorf("finding go tool: %v", err)
20+
}
21+
p, err := StartProcess(path, []string{"go"}, &ProcAttr{})
22+
if err != nil {
23+
t.Errorf("starting test process: %v", err)
24+
}
25+
p.Wait()
26+
if got := p.Signal(Kill); got != ErrProcessDone {
27+
t.Errorf("got %v want %v", got, ErrProcessDone)
28+
}
29+
}

0 commit comments

Comments
 (0)