Skip to content

Commit

Permalink
Remove a deadlock when no pingresp
Browse files Browse the repository at this point in the history
There was a deadlock situation when we didn't receive an expected pingresp, keepalive() called internalConnLost() synchronously, that waits for all workers to exit before it returns, but keepalive() is one of those workers...
Also there was an issue when returning from keepalive() that we'd call the waitgroup broadcasts before c.stop had been closed, causing them to fall through and then get stuck at the Wait(), which would cause keepalive() not to finish returning and call workers.Done(), etc... locked again. Changed this to use a pingstop channel for the timer reset go funcs that is closed at the beginning of the exit func for keepalive() ensuring the following broadcasts will cause them all to exit properly.
resolve#129
  • Loading branch information
alsm committed Aug 1, 2017
1 parent db7be0c commit aff1577
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion net.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func errorWatch(c *client) {
return
case err := <-c.errors:
ERROR.Println(NET, "error triggered, stopping")
c.internalConnLost(err)
go c.internalConnLost(err)
return
}
}
10 changes: 6 additions & 4 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func keepalive(c *client) {
DEBUG.Println(PNG, "keepalive starting")

var condWG sync.WaitGroup
pingStop := make(chan struct{})

defer func() {
close(pingStop)
c.keepaliveReset.Broadcast()
c.pingResp.Broadcast()
c.packetResp.Broadcast()
Expand All @@ -50,7 +52,7 @@ func keepalive(c *client) {
c.pingResp.Wait()
c.pingResp.L.Unlock()
select {
case <-c.stop:
case <-pingStop:
return
default:
}
Expand All @@ -68,7 +70,7 @@ func keepalive(c *client) {
c.packetResp.Wait()
c.packetResp.L.Unlock()
select {
case <-c.stop:
case <-pingStop:
return
default:
}
Expand All @@ -84,7 +86,7 @@ func keepalive(c *client) {
c.keepaliveReset.Wait()
c.keepaliveReset.L.Unlock()
select {
case <-c.stop:
case <-pingStop:
return
default:
}
Expand All @@ -107,7 +109,7 @@ func keepalive(c *client) {
case <-pingRespTimer.C:
pingRespTimer.SetRead(true)
CRITICAL.Println(PNG, "pingresp not received, disconnecting")
c.internalConnLost(errors.New("pingresp not received, disconnecting"))
c.errors <- errors.New("pingresp not received, disconnecting")
pingTimer.Stop()
return
}
Expand Down

0 comments on commit aff1577

Please sign in to comment.