From e8aaca27d3f6cb4c36c52ca55a0776bfb716e07e Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 6 Sep 2019 16:57:17 +0100 Subject: [PATCH] unlock before sending the message to avoid deadlock --- network/default.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/network/default.go b/network/default.go index 31fd57f2..23c924f6 100644 --- a/network/default.go +++ b/network/default.go @@ -963,26 +963,32 @@ func (n *network) close() error { // Close closes network connection func (n *network) Close() error { + // lock this operation n.Lock() - defer n.Unlock() if !n.connected { + n.Unlock() return nil } select { case <-n.closed: + n.Unlock() return nil default: + // TODO: send close message to the network channel + close(n.closed) + // set connected to false + n.connected = false + + // unlock the lock otherwise we'll deadlock sending the close + n.Unlock() + // send close message only if we managed to connect to NetworkChannel log.Debugf("Sending close message from: %s", n.options.Id) if err := n.sendMsg("close", NetworkChannel); err != nil { log.Debugf("Network failed to send close message: %s", err) } - // TODO: send close message to the network channel - close(n.closed) - // set connected to false - n.connected = false } return n.close()