beam: Fix double close of fds in SendUnix

Instead of calling syscall.Close() on the fds in sendUnix() we call
Close() on the *os.File in Send(). Otherwise the fd will be closed, but
the *os.File will continue to live, and when it is finalized the
fd will be closed (which by then may be reused and can be anything).

This also adds a note to Send() the the file is closed.

Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)
This commit is contained in:
Alexander Larsson 2014-03-25 13:32:44 +01:00 committed by Solomon Hykes
parent 8104b14a8d
commit cd911b83ce

View file

@ -39,6 +39,7 @@ func FileConn(f *os.File) (*UnixConn, error) {
// Send sends a new message on conn with data and f as payload and // Send sends a new message on conn with data and f as payload and
// attachment, respectively. // attachment, respectively.
// On success, f is closed
func (conn *UnixConn) Send(data []byte, f *os.File) error { func (conn *UnixConn) Send(data []byte, f *os.File) error {
{ {
var fd int = -1 var fd int = -1
@ -51,7 +52,14 @@ func (conn *UnixConn) Send(data []byte, f *os.File) error {
if f != nil { if f != nil {
fds = append(fds, int(f.Fd())) fds = append(fds, int(f.Fd()))
} }
return sendUnix(conn.UnixConn, data, fds...) if err := sendUnix(conn.UnixConn, data, fds...); err != nil {
return err
}
if f != nil {
f.Close()
}
return nil
} }
// Receive waits for a new message on conn, and receives its payload // Receive waits for a new message on conn, and receives its payload
@ -100,11 +108,6 @@ func receiveUnix(conn *net.UnixConn) ([]byte, []int, error) {
func sendUnix(conn *net.UnixConn, data []byte, fds ...int) error { func sendUnix(conn *net.UnixConn, data []byte, fds ...int) error {
_, _, err := conn.WriteMsgUnix(data, syscall.UnixRights(fds...), nil) _, _, err := conn.WriteMsgUnix(data, syscall.UnixRights(fds...), nil)
if err == nil {
for _, fd := range fds {
syscall.Close(fd)
}
}
return err return err
} }