os: release process handle at the end of windows (*Process).Wait Fixes #3154. R=golang-dev, bradfitz, rsc CC=golang-dev https://golang.org/cl/5707052
diff --git a/src/pkg/net/http/triv.go b/src/pkg/net/http/triv.go index c88a0fb..269af0c 100644 --- a/src/pkg/net/http/triv.go +++ b/src/pkg/net/http/triv.go
@@ -108,7 +108,6 @@ fmt.Fprintf(rw, "fork/exec: %s\n", err) return } - defer p.Release() io.Copy(rw, r) wait, err := p.Wait(0) if err != nil {
diff --git a/src/pkg/os/doc.go b/src/pkg/os/doc.go index ef857c0..546f864 100644 --- a/src/pkg/os/doc.go +++ b/src/pkg/os/doc.go
@@ -11,6 +11,13 @@ return findProcess(pid) } +// Release releases any resources associated with the Process p, +// rendering it unusable in the future. +// Release only needs to be called if Wait is not. +func (p *Process) Release() error { + return p.release() +} + // Hostname returns the host name reported by the kernel. func Hostname() (name string, err error) { return hostname()
diff --git a/src/pkg/os/exec_plan9.go b/src/pkg/os/exec_plan9.go index 1c9e2b9..a941d12 100644 --- a/src/pkg/os/exec_plan9.go +++ b/src/pkg/os/exec_plan9.go
@@ -94,8 +94,7 @@ return ps, nil } -// Release releases any resources associated with the Process. -func (p *Process) Release() error { +func (p *Process) release() error { // NOOP for Plan 9. p.Pid = -1 // no need for a finalizer anymore
diff --git a/src/pkg/os/exec_unix.go b/src/pkg/os/exec_unix.go index 8d000e9..3f89fe8 100644 --- a/src/pkg/os/exec_unix.go +++ b/src/pkg/os/exec_unix.go
@@ -51,8 +51,7 @@ return nil } -// Release releases any resources associated with the Process. -func (p *Process) Release() error { +func (p *Process) release() error { // NOOP for unix. p.Pid = -1 // no need for a finalizer anymore
diff --git a/src/pkg/os/exec_windows.go b/src/pkg/os/exec_windows.go index dab0dc9..3d07ab7 100644 --- a/src/pkg/os/exec_windows.go +++ b/src/pkg/os/exec_windows.go
@@ -14,6 +14,7 @@ // Wait waits for the Process to exit or stop, and then returns a // ProcessState describing its status and an error, if any. +// Wait releases any resources associated with the Process. func (p *Process) Wait() (ps *ProcessState, err error) { s, e := syscall.WaitForSingleObject(syscall.Handle(p.handle), syscall.INFINITE) switch s { @@ -30,6 +31,7 @@ return nil, NewSyscallError("GetExitCodeProcess", e) } p.done = true + defer p.Release() return &ProcessState{p.Pid, syscall.WaitStatus{Status: s, ExitCode: ec}, new(syscall.Rusage)}, nil } @@ -46,8 +48,7 @@ return syscall.Errno(syscall.EWINDOWS) } -// Release releases any resources associated with the Process. -func (p *Process) Release() error { +func (p *Process) release() error { if p.handle == uintptr(syscall.InvalidHandle) { return syscall.EINVAL }
diff --git a/src/pkg/os/os_test.go b/src/pkg/os/os_test.go index 02f75b2..d1e241f 100644 --- a/src/pkg/os/os_test.go +++ b/src/pkg/os/os_test.go
@@ -530,7 +530,6 @@ if err != nil { t.Fatalf("StartProcess: %v", err) } - defer p.Release() w.Close() var b bytes.Buffer @@ -848,7 +847,6 @@ if err != nil { t.Fatal(err) } - defer p.Release() w.Close() var b bytes.Buffer