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 <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-03-18 17:09:09 -07:00
parent 95b759ad16
commit be03962d64

View file

@ -111,6 +111,12 @@ 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 {
fmt.Printf("Closing sent fd %v\n", fd)
syscall.Close(fd)
}
}
return err return err
} }
@ -154,8 +160,10 @@ func FdConn(fd int) (*net.UnixConn, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
f.Close()
uconn, ok := conn.(*net.UnixConn) uconn, ok := conn.(*net.UnixConn)
if !ok { if !ok {
conn.Close()
return nil, fmt.Errorf("%d: not a unix connection", fd) return nil, fmt.Errorf("%d: not a unix connection", fd)
} }
return uconn, nil return uconn, nil