Skip to content

Commit

Permalink
[FIXED] Data RACE on Unsubscribe when client connection is closed
Browse files Browse the repository at this point in the history
Resolves #331
  • Loading branch information
kozlovic committed Aug 17, 2016
1 parent 14f5d09 commit 811e086
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion server/client.go
Expand Up @@ -882,10 +882,12 @@ func (c *client) deliverMsg(sub *subscription, mh, msg []byte) {
// unsubscribe and drop message on the floor.
if sub.nm == sub.max {
c.Debugf("Auto-unsubscribe limit of %d reached for sid '%s'\n", sub.max, string(sub.sid))
defer client.unsubscribe(sub)
// Due to defer, reverse the code order so that execution
// is consistent with other cases where we unsubscribe.
if shouldForward {
defer client.srv.broadcastUnSubscribe(sub)
}
defer client.unsubscribe(sub)
} else if sub.nm > sub.max {
c.Debugf("Auto-unsubscribe limit [%d] exceeded\n", sub.max)
client.mu.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions server/route.go
Expand Up @@ -575,10 +575,12 @@ func (s *Server) broadcastUnSubscribe(sub *subscription) {
}
rsid := routeSid(sub)
maxStr := _EMPTY_
sub.client.mu.Lock()
// Set max if we have it set and have not tripped auto-unsubscribe
if sub.max > 0 && sub.nm < sub.max {
maxStr = fmt.Sprintf(" %d", sub.max)
}
sub.client.mu.Unlock()
proto := fmt.Sprintf(unsubProto, rsid, maxStr)
s.broadcastInterestToRoutes(proto)
}
Expand Down

0 comments on commit 811e086

Please sign in to comment.