mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-12-24 12:58:05 +08:00
Avoid race condition when closing listeners
"Atomic load" followed by "atomic store" is not itself an atomic operation. This commit replaces that sequence with CompareAndSwap instead.
This commit is contained in:
@@ -98,9 +98,7 @@ func (l *HTTPStats) Close(closeClients CloseFunc) {
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
if atomic.LoadUint32(&l.end) == 0 {
|
||||
atomic.StoreUint32(&l.end, 1)
|
||||
|
||||
if atomic.CompareAndSwapUint32(&l.end, 0, 1) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
l.listen.Shutdown(ctx)
|
||||
|
||||
@@ -106,8 +106,7 @@ func (l *TCP) Close(closeClients CloseFunc) {
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
if atomic.LoadUint32(&l.end) == 0 {
|
||||
atomic.StoreUint32(&l.end, 1)
|
||||
if atomic.CompareAndSwapUint32(&l.end, 0, 1) {
|
||||
closeClients(l.id)
|
||||
}
|
||||
|
||||
|
||||
@@ -162,8 +162,7 @@ func (l *Websocket) Close(closeClients CloseFunc) {
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
if atomic.LoadUint32(&l.end) == 0 {
|
||||
atomic.StoreUint32(&l.end, 1)
|
||||
if atomic.CompareAndSwapUint32(&l.end, 0, 1) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
l.listen.Shutdown(ctx)
|
||||
|
||||
Reference in New Issue
Block a user