You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The garbage collection routine iterates over and modifies s.clients concurrently, potentially leading to a panic due to a concurrent map read and write.
Problem
The s.clients map is not thread-safe. While one goroutine (e.g., the garbage collector) might be deleting clients, another goroutine (e.g., findClientBySessionID) could be iterating or reading from it, leading to undefined behavior or a runtime panic.
Suggestions
Use sync.Map
Replace s.clients with a sync.Map under a type wrapper. This will ensure thread-safe operations for both reading and writing.
Introduce a Mutex
Use a sync.Mutex or sync.RWMutex to explicitly lock the s.clients map during reads and writes. This approach ensures safety and maintains the existing map structure but could introduce performance overhead depending on the frequency of access.
The text was updated successfully, but these errors were encountered:
beka-birhanu
changed the title
concurrency issue
Potential Concurrency Issue with s.clients Access in Server
Dec 12, 2024
@beka-birhanu Hi, thanks for reviewing the code and creating issues, I made some quick changes. It would be great if you could review the changes here:
Description
The
s.clients
map in theServer
structure appears to be accessed concurrently without proper synchronization, which may lead to race conditions.Example 1: Access in
findClientBySessionID
In this method,
s.clients
is iterated over, potentially while other parts of the code modify it.Example 2: Modification in Garbage Collection
The garbage collection routine iterates over and modifies
s.clients
concurrently, potentially leading to a panic due to a concurrent map read and write.Problem
The
s.clients
map is not thread-safe. While one goroutine (e.g., the garbage collector) might be deleting clients, another goroutine (e.g.,findClientBySessionID
) could be iterating or reading from it, leading to undefined behavior or a runtime panic.Suggestions
Use
sync.Map
Replace
s.clients
with async.Map
under a type wrapper. This will ensure thread-safe operations for both reading and writing.Introduce a Mutex
Use a
sync.Mutex
orsync.RWMutex
to explicitly lock thes.clients
map during reads and writes. This approach ensures safety and maintains the existing map structure but could introduce performance overhead depending on the frequency of access.The text was updated successfully, but these errors were encountered: