diff --git a/beam/unix.go b/beam/unix.go index 94d7b5b..b138934 100644 --- a/beam/unix.go +++ b/beam/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 }