From b1090576ae94af7723eab7b96b531ae6ffef1038 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sun, 23 Mar 2014 20:45:44 -0700 Subject: [PATCH] Beam: fix bug in beamsh which 'swapped' FDs because of underlying implementation of net.FileConn Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- beam/examples/beamsh/beamsh.go | 2 ++ beam/unix.go | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/beam/examples/beamsh/beamsh.go b/beam/examples/beamsh/beamsh.go index d83fbe7..6f88914 100644 --- a/beam/examples/beamsh/beamsh.go +++ b/beam/examples/beamsh/beamsh.go @@ -67,8 +67,10 @@ func CmdCat(args []string, f *os.File) { func CmdEcho(args []string, f *os.File) { resp, err := beam.FdConn(int(f.Fd())) if err != nil { + Fatal(err) return } + defer resp.Close() r, w, err := os.Pipe() if err != nil { return diff --git a/beam/unix.go b/beam/unix.go index 7dfff8a..14326ac 100644 --- a/beam/unix.go +++ b/beam/unix.go @@ -199,13 +199,14 @@ func USocketPair() (*net.UnixConn, *net.UnixConn, error) { // FdConn wraps a file descriptor in a standard *net.UnixConn object, or // returns an error if the file descriptor does not point to a unix socket. +// This creates a duplicate file descriptor. It's the caller's responsibility +// to close both. func FdConn(fd int) (*net.UnixConn, error) { f := os.NewFile(uintptr(fd), fmt.Sprintf("%d", fd)) conn, err := net.FileConn(f) if err != nil { return nil, err } - f.Close() uconn, ok := conn.(*net.UnixConn) if !ok { conn.Close()