diff --git a/beam/service.go b/beam/service.go index 69e82e7..9a21e35 100644 --- a/beam/service.go +++ b/beam/service.go @@ -17,11 +17,7 @@ import ( // not point to a connection, that message will be skipped. // func Listen(conn *net.UnixConn, name string) (net.Listener, error) { - fEndpoint, err := SendPipe(conn, []byte(name)) - if err != nil { - return nil, err - } - endpoint, err := FdConn(int(fEndpoint.Fd())) + endpoint, err := SendPipe(conn, []byte(name)) if err != nil { return nil, err } diff --git a/beam/unix.go b/beam/unix.go index cab8ee2..15e8ff0 100644 --- a/beam/unix.go +++ b/beam/unix.go @@ -78,16 +78,25 @@ func Receive(conn *net.UnixConn) ([]byte, *os.File, error) { // allows for arbitrarily complex service discovery and retry logic to take place, // without complicating application code. // -func SendPipe(conn *net.UnixConn, data []byte) (*os.File, error) { +func SendPipe(conn *net.UnixConn, data []byte) (endpoint *net.UnixConn, err error) { local, remote, err := SocketPair() if err != nil { return nil, err } - if err := Send(conn, data, remote); err != nil { - remote.Close() + defer func() { + if err != nil { + local.Close() + remote.Close() + } + }() + endpoint, err = FdConn(int(local.Fd())) + if err != nil { return nil, err } - return local, nil + if err := Send(conn, data, remote); err != nil { + return nil, err + } + return endpoint, nil } func receiveUnix(conn *net.UnixConn) ([]byte, []int, error) {