From c712fdcbd9a187c21cbc7c2291cb696d526a0df8 Mon Sep 17 00:00:00 2001 From: Danielle Lancashire Date: Tue, 2 Jul 2019 13:12:54 +0200 Subject: [PATCH] fifo: Safer access to Conn --- client/lib/fifo/fifo_windows.go | 34 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/client/lib/fifo/fifo_windows.go b/client/lib/fifo/fifo_windows.go index 08396c50a..b8ea6b4b9 100644 --- a/client/lib/fifo/fifo_windows.go +++ b/client/lib/fifo/fifo_windows.go @@ -21,21 +21,29 @@ type winFIFO struct { connLock sync.Mutex } -func (f *winFIFO) Read(p []byte) (n int, err error) { +func (f *winFIFO) ensureConn() (net.Conn, error) { f.connLock.Lock() + defer f.connLock.Unlock() if f.conn == nil { c, err := f.listener.Accept() if err != nil { - return 0, err + return nil, err } - f.conn = c } - f.connLock.Unlock() + + return f.conn, nil +} + +func (f *winFIFO) Read(p []byte) (n int, err error) { + conn, err := f.ensureConn() + if err != nil { + return 0, err + } // If the connection is closed then we need to close the listener // to emulate unix fifo behavior - n, err = f.conn.Read(p) + n, err = conn.Read(p) if err == io.EOF { f.listener.Close() } @@ -43,22 +51,16 @@ func (f *winFIFO) Read(p []byte) (n int, err error) { } func (f *winFIFO) Write(p []byte) (n int, err error) { - f.connLock.Lock() - if f.conn == nil { - c, err := f.listener.Accept() - if err != nil { - return 0, err - } - - f.conn = c + conn, err := f.ensureConn() + if err != nil { + return 0, err } - f.connLock.Unlock() // If the connection is closed then we need to close the listener // to emulate unix fifo behavior - n, err = f.conn.Write(p) + n, err = conn.Write(p) if err == io.EOF { - f.conn.Close() + conn.Close() f.listener.Close() } return n, err