From d8227c275101496ddb0fe1c2f0a132856451bcaf Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 14 Jan 2014 21:45:42 -0500 Subject: [PATCH] Only close websocket disconnected chan if still open. A channel cannot be closed multiple times, so use a select statement to only close the channel if it can not be read from. --- rpcwebsocket.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/rpcwebsocket.go b/rpcwebsocket.go index 19c7fde9..1f3e2a91 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -567,7 +567,14 @@ func (s *rpcServer) walletReqsNotifications(ws *websocket.Conn) { default: var m []byte if err := websocket.Message.Receive(ws, &m); err != nil { - close(disconnected) + // Only close disconnected if not closed yet. + select { + case <-disconnected: + // nothing + + default: + close(disconnected) + } return } msgs <- m @@ -607,8 +614,14 @@ func (s *rpcServer) walletReqsNotifications(ws *websocket.Conn) { continue } if err := websocket.Message.Send(ws, mresp); err != nil { - // Wallet disconnected. - close(disconnected) + // Only close disconnected if not closed yet. + select { + case <-disconnected: + // nothing + + default: + close(disconnected) + } return } @@ -620,8 +633,14 @@ func (s *rpcServer) walletReqsNotifications(ws *websocket.Conn) { continue } if err := websocket.Message.Send(ws, mntfn); err != nil { - // Wallet disconnected. - close(disconnected) + // Only close disconnected if not closed yet. + select { + case <-disconnected: + // nothing + + default: + close(disconnected) + } return } }