From faa83a0fc01af634c4f211bf05dc8228c518c3b8 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 25 Mar 2014 13:32:44 +0100 Subject: [PATCH] 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 (github: alexlarsson) --- unix.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/unix.go b/unix.go index 94d7b5b4fc..b138934084 100644 --- a/unix.go +++ b/unix.go @@ -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 // attachment, respectively. +// On success, f is closed func (conn *UnixConn) Send(data []byte, f *os.File) error { { var fd int = -1 @@ -51,7 +52,14 @@ func (conn *UnixConn) Send(data []byte, f *os.File) error { if f != nil { 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 @@ -100,11 +108,6 @@ func receiveUnix(conn *net.UnixConn) ([]byte, []int, error) { func sendUnix(conn *net.UnixConn, data []byte, fds ...int) error { _, _, err := conn.WriteMsgUnix(data, syscall.UnixRights(fds...), nil) - if err == nil { - for _, fd := range fds { - syscall.Close(fd) - } - } return err }