Skip to content

Commit 6801721

Browse files
authored
Resolve potential goroutine leak wien Disconnect called
If `Disconnect` was called whilst a connection attempt was in progress a goroutine leak occurred. This change allows the connection attempt to complete as normal (including calling the `OnConnect` callback) before the Disconnect is handled. closes #675
2 parents fe38f80 + 71f9814 commit 6801721

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

client.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -601,17 +601,14 @@ func (c *client) startCommsWorkers(conn net.Conn, connectionUp connCompletedFn,
601601
c.workers.Add(1) // Done will be called when ackOut is closed
602602
ackOut := c.msgRouter.matchAndDispatch(incomingPubChan, c.options.Order, c)
603603

604-
// The connection is now ready for use (we spin up a few go routines below). It is possible that
605-
// Disconnect has been called in the interim...
604+
// The connection is now ready for use (we spin up a few go routines below).
605+
// It is possible that Disconnect has been called in the interim...
606+
// issue 675:we will allow the connection to complete before the Disconnect is allowed to proceed
607+
// as if a Disconnect event occurred immediately after connectionUp(true) completed.
606608
if err := connectionUp(true); err != nil {
607-
DEBUG.Println(CLI, err)
608-
close(c.stop) // Tidy up anything we have already started
609-
close(incomingPubChan)
610-
c.workers.Wait()
611-
c.conn.Close()
612-
c.conn = nil
613-
return false
609+
ERROR.Println(CLI, err)
614610
}
611+
615612
DEBUG.Println(CLI, "client is connected/reconnected")
616613
if c.options.OnConnect != nil {
617614
go c.options.OnConnect(c)

0 commit comments

Comments
 (0)