From c401c43cae4d646a2a7f87850518a1d6ca6ada57 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) --- pkg/beam/unix.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/beam/unix.go b/pkg/beam/unix.go index 15e8ff0e27..7f7a73b186 100644 --- a/pkg/beam/unix.go +++ b/pkg/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