From 36a47f5b5952d7d1fe92679e869869fef2768ade Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 8 Dec 2013 18:39:14 -0800 Subject: [PATCH] packer/rpc: more fine grained lock access on MuxConn --- packer/rpc/muxconn.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packer/rpc/muxconn.go b/packer/rpc/muxconn.go index 91b9b08d0..9907ce102 100644 --- a/packer/rpc/muxconn.go +++ b/packer/rpc/muxconn.go @@ -61,23 +61,27 @@ func (m *MuxConn) Close() error { // point. In a real muxer, we'd probably want a handshake here. func (m *MuxConn) Stream(id byte) (io.ReadWriteCloser, error) { m.mu.Lock() - defer m.mu.Unlock() if _, ok := m.streams[id]; ok { + m.mu.Unlock() return nil, fmt.Errorf("Stream %d already exists", id) } // Create the stream object and channel where data will be sent to dataR, dataW := io.Pipe() + + // Set the data channel so we can write to it. + m.streams[id] = dataW + + // Unlock the lock so that the reader can access the stream writer. + m.mu.Unlock() + stream := &Stream{ id: id, mux: m, reader: dataR, } - // Set the data channel so we can write to it. - m.streams[id] = dataW - return stream, nil }