From be03962d64eab5745ba8835cd0adf3c497833ea3 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 18 Mar 2014 17:09:09 -0700 Subject: [PATCH] Beam: fix file descriptor leaks * Close all file descriptors successfully sent as attachment * Close duplicate file descriptors created by net.FileCon and net.UnixConn.File Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- beam/unix.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/beam/unix.go b/beam/unix.go index 15e8ff0..7f7a73b 100644 --- a/beam/unix.go +++ b/beam/unix.go @@ -111,6 +111,12 @@ 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 { + fmt.Printf("Closing sent fd %v\n", fd) + syscall.Close(fd) + } + } return err } @@ -154,8 +160,10 @@ func FdConn(fd int) (*net.UnixConn, error) { if err != nil { return nil, err } + f.Close() uconn, ok := conn.(*net.UnixConn) if !ok { + conn.Close() return nil, fmt.Errorf("%d: not a unix connection", fd) } return uconn, nil