Skip to content

Commit

Permalink
Merge pull request #67 from alsm/sema-fix
Browse files Browse the repository at this point in the history
Ensure that go routines finish on disconnect/error
  • Loading branch information
Al S-M authored Jul 2, 2021
2 parents 1aa48ad + 3a4d613 commit a215349
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions paho/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func NewClient(conf ClientConfig) *Client {
}
if c.PingHandler == nil {
c.PingHandler = DefaultPingerWithCustomFailHandler(func(e error) {
c.error(e)
go c.error(e)
})
}
if c.OnClientError == nil {
Expand Down Expand Up @@ -407,13 +407,14 @@ func (c *Client) incoming() {
default:
recv, err := packets.ReadPacket(c.Conn)
if err != nil {
c.error(err)
go c.error(err)
return
}
switch recv.Type {
case packets.CONNACK:
c.debug.Println("received CONNACK")
c.error(fmt.Errorf("received unexpected CONNACK"))
go c.error(fmt.Errorf("received unexpected CONNACK"))
return
case packets.AUTH:
c.debug.Println("received AUTH")
ap := recv.Content.(*packets.Auth)
Expand All @@ -428,7 +429,7 @@ func (c *Client) incoming() {
case 0x18:
if c.AuthHandler != nil {
if _, err := c.AuthHandler.Authenticate(AuthFromPacketAuth(ap)).Packet().WriteTo(c.Conn); err != nil {
c.error(err)
go c.error(err)
return
}
}
Expand Down Expand Up @@ -503,13 +504,14 @@ func (c *Client) incoming() {
if c.raCtx != nil {
c.raCtx.Return <- *recv
}
if c.OnServerDisconnect != nil {
c.close()
c.debug.Println("calling OnDisconnect")
go c.OnServerDisconnect(DisconnectFromPacketDisconnect(recv.Content.(*packets.Disconnect)))
} else {
c.OnClientError(fmt.Errorf("server initiated disconnect"))
}
go func() {
if c.OnServerDisconnect != nil {
go c.serverDisconnect(DisconnectFromPacketDisconnect(recv.Content.(*packets.Disconnect)))
} else {
go c.error(fmt.Errorf("server initiated disconnect"))
}
}()
return
case packets.PINGRESP:
c.debug.Println("received PINGRESP")
c.PingHandler.PingResp()
Expand Down Expand Up @@ -548,9 +550,17 @@ func (c *Client) close() {
func (c *Client) error(e error) {
c.debug.Println("error called:", e)
c.close()
c.workers.Wait()
go c.OnClientError(e)
}

func (c *Client) serverDisconnect(d *Disconnect) {
c.close()
c.workers.Wait()
c.debug.Println("calling OnServerDisconnect")
go c.OnServerDisconnect(d)
}

// Authenticate is used to initiate a reauthentication of credentials with the
// server. This function sends the initial Auth packet to start the reauthentication
// then relies on the client AuthHandler managing any further requests from the
Expand Down

0 comments on commit a215349

Please sign in to comment.