Skip to content

Commit

Permalink
Resolve potential goroutine leak wien Disconnect called
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MattBrittan authored May 20, 2024
2 parents fe38f80 + 71f9814 commit 6801721
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,14 @@ func (c *client) startCommsWorkers(conn net.Conn, connectionUp connCompletedFn,
c.workers.Add(1) // Done will be called when ackOut is closed
ackOut := c.msgRouter.matchAndDispatch(incomingPubChan, c.options.Order, c)

// The connection is now ready for use (we spin up a few go routines below). It is possible that
// Disconnect has been called in the interim...
// The connection is now ready for use (we spin up a few go routines below).
// It is possible that Disconnect has been called in the interim...
// issue 675:we will allow the connection to complete before the Disconnect is allowed to proceed
// as if a Disconnect event occurred immediately after connectionUp(true) completed.
if err := connectionUp(true); err != nil {
DEBUG.Println(CLI, err)
close(c.stop) // Tidy up anything we have already started
close(incomingPubChan)
c.workers.Wait()
c.conn.Close()
c.conn = nil
return false
ERROR.Println(CLI, err)
}

DEBUG.Println(CLI, "client is connected/reconnected")
if c.options.OnConnect != nil {
go c.options.OnConnect(c)
Expand Down

0 comments on commit 6801721

Please sign in to comment.