From 0b1a2ef5c7003385f6a8468c246aa387aa91a386 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Thu, 10 Dec 2015 15:44:50 +0000 Subject: [PATCH] Wait for pipes to close in Close(). --- xfer/pipes.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/xfer/pipes.go b/xfer/pipes.go index 25a061424..8e85515ff 100644 --- a/xfer/pipes.go +++ b/xfer/pipes.go @@ -20,6 +20,7 @@ type Pipe interface { type pipe struct { mtx sync.Mutex + wg sync.WaitGroup port, starboard io.ReadWriter quit chan struct{} closed bool @@ -60,6 +61,7 @@ func (p *pipe) Close() error { onClose = p.onClose } p.mtx.Unlock() + p.wg.Wait() // Don't run onClose under lock. if onClose != nil { @@ -82,6 +84,15 @@ func (p *pipe) OnClose(f func()) { // CopyToWebsocket copies pipe data to/from a websocket. It blocks. func (p *pipe) CopyToWebsocket(end io.ReadWriter, conn *websocket.Conn) error { + p.mtx.Lock() + if p.closed { + p.mtx.Unlock() + return nil + } + p.wg.Add(1) + p.mtx.Unlock() + defer p.wg.Done() + errors := make(chan error, 1) // Read-from-UI loop