mirror of
https://github.com/fhmq/hmq.git
synced 2026-08-29 14:03:10 +00:00
fix: mutex deadlock when connection close
This commit is contained in:
+8
-6
@@ -828,11 +828,12 @@ func (c *client) Close() {
|
|||||||
Timestamp: time.Now().Unix(),
|
Timestamp: time.Now().Unix(),
|
||||||
})
|
})
|
||||||
|
|
||||||
if c.mu.Lock(); c.conn != nil {
|
c.mu.Lock()
|
||||||
|
if c.conn != nil {
|
||||||
_ = c.conn.Close()
|
_ = c.conn.Close()
|
||||||
c.conn = nil
|
c.conn = nil
|
||||||
c.mu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return
|
return
|
||||||
@@ -905,13 +906,14 @@ func (c *client) WriterPacket(packet packets.ControlPacket) error {
|
|||||||
if packet == nil {
|
if packet == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if c.conn == nil {
|
|
||||||
c.Close()
|
|
||||||
return errors.New("connect lost ....")
|
|
||||||
}
|
|
||||||
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
|
if c.conn == nil {
|
||||||
|
return errors.New("connect lost ....")
|
||||||
|
}
|
||||||
|
|
||||||
return packet.Write(c.conn)
|
return packet.Write(c.conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -202,11 +202,13 @@ func (c *client) retryDelivery() {
|
|||||||
c.inflightMu.RLock()
|
c.inflightMu.RLock()
|
||||||
ilen := len(c.inflight)
|
ilen := len(c.inflight)
|
||||||
|
|
||||||
if c.mu.Lock(); c.conn == nil || ilen == 0 { //Reset timer when client offline OR inflight is empty
|
c.mu.Lock()
|
||||||
|
if c.conn == nil || ilen == 0 { //Reset timer when client offline OR inflight is empty
|
||||||
c.inflightMu.RUnlock()
|
c.inflightMu.RUnlock()
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
// copy the to be retried elements out of the map to only hold the lock for a short time and use the new slice later to iterate
|
// copy the to be retried elements out of the map to only hold the lock for a short time and use the new slice later to iterate
|
||||||
// through them
|
// through them
|
||||||
|
|||||||
Reference in New Issue
Block a user