fix: 🐛 fixes race condition (#155)

- this race condition occurs when a client is disconnected or when hmq checks
if client still alive

Signed-off-by: Lucas Vieira <lucas.engen.cc@gmail.com>
This commit is contained in:
Lucas Vieira
2022-04-04 09:32:38 -03:00
committed by GitHub
parent 94ff8e8405
commit 31864cdf2b
2 changed files with 5 additions and 2 deletions

View File

@@ -808,9 +808,10 @@ func (c *client) Close() {
Timestamp: time.Now().Unix(),
})
if c.conn != nil {
if c.mu.Lock(); c.conn != nil {
_ = c.conn.Close()
c.conn = nil
c.mu.Unlock()
}
if b == nil {

View File

@@ -195,8 +195,10 @@ func (c *client) retryDelivery() {
c.resetRetryTimer()
c.inflightMu.RLock()
ilen := len(c.inflight)
if c.conn == nil || ilen == 0 { //Reset timer when client offline OR inflight is empty
if c.mu.Lock(); c.conn == nil || ilen == 0 { //Reset timer when client offline OR inflight is empty
c.inflightMu.RUnlock()
c.mu.Unlock()
return
}