Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(176)

Unified Diff: src/pkg/os/exec/exec.go

Issue 6789043: code review 6789043: os/exec: fix fd leak on error
Patch Set: diff -r 57f70857cf91 https://go.googlecode.com/hg/ Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/pkg/os/exec/exec_test.go » ('j') | src/pkg/os/exec/exec_test.go » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pkg/os/exec/exec.go
===================================================================
--- a/src/pkg/os/exec/exec.go
+++ b/src/pkg/os/exec/exec.go
@@ -87,7 +87,6 @@
finished bool // when Wait was called
childFiles []*os.File
closeAfterStart []io.Closer
- closeAfterWait []io.Closer
goroutine []func() error
errch chan error // one send per goroutine
}
@@ -160,7 +159,6 @@
}
c.closeAfterStart = append(c.closeAfterStart, pr)
- c.closeAfterWait = append(c.closeAfterWait, pw)
c.goroutine = append(c.goroutine, func() error {
_, err := io.Copy(pw, c.Stdin)
if err1 := pw.Close(); err == nil {
@@ -202,9 +200,9 @@
}
c.closeAfterStart = append(c.closeAfterStart, pw)
- c.closeAfterWait = append(c.closeAfterWait, pr)
c.goroutine = append(c.goroutine, func() error {
_, err := io.Copy(w, pr)
+ pr.Close()
return err
})
return pw, nil
@@ -235,6 +233,7 @@
// Start starts the specified command but does not wait for it to complete.
func (c *Cmd) Start() error {
if c.err != nil {
+ c.closeDescriptors(c.closeAfterStart)
return c.err
}
if c.Process != nil {
@@ -246,7 +245,6 @@
fd, err := setupFd(c)
if err != nil {
c.closeDescriptors(c.closeAfterStart)
- c.closeDescriptors(c.closeAfterWait)
return err
}
c.childFiles = append(c.childFiles, fd)
@@ -262,7 +260,6 @@
})
if err != nil {
c.closeDescriptors(c.closeAfterStart)
- c.closeDescriptors(c.closeAfterWait)
return err
}
@@ -315,8 +312,6 @@
}
}
- c.closeDescriptors(c.closeAfterWait)
-
if err != nil {
return err
} else if !state.Success() {
@@ -368,13 +363,11 @@
}
c.Stdin = pr
c.closeAfterStart = append(c.closeAfterStart, pr)
- c.closeAfterWait = append(c.closeAfterWait, pw)
return pw, nil
}
// StdoutPipe returns a pipe that will be connected to the command's
// standard output when the command starts.
-// The pipe will be closed automatically after Wait sees the command exit.
func (c *Cmd) StdoutPipe() (io.ReadCloser, error) {
if c.Stdout != nil {
return nil, errors.New("exec: Stdout already set")
@@ -388,13 +381,11 @@
}
c.Stdout = pw
c.closeAfterStart = append(c.closeAfterStart, pw)
- c.closeAfterWait = append(c.closeAfterWait, pr)
return pr, nil
}
// StderrPipe returns a pipe that will be connected to the command's
// standard error when the command starts.
-// The pipe will be closed automatically after Wait sees the command exit.
func (c *Cmd) StderrPipe() (io.ReadCloser, error) {
if c.Stderr != nil {
return nil, errors.New("exec: Stderr already set")
@@ -408,6 +399,5 @@
}
c.Stderr = pw
c.closeAfterStart = append(c.closeAfterStart, pw)
- c.closeAfterWait = append(c.closeAfterWait, pr)
return pr, nil
}
« no previous file with comments | « no previous file | src/pkg/os/exec/exec_test.go » ('j') | src/pkg/os/exec/exec_test.go » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b