Skip to content

Commit

Permalink
fix: fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Aug 2, 2024
1 parent 49e69ab commit ffb4cd2
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions node/pkg/dal/api/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,21 @@ func (c *Hub) handleClientRegistration() {
}

func (c *Hub) addClient(client *ThreadSafeClient) {
c.mu.RLock()
_, ok := c.clients[client]
c.mu.RUnlock()
if ok {
c.mu.Lock() // Use write lock for both checking and insertion
defer c.mu.Unlock()
if _, ok := c.clients[client]; ok {
return
}

c.mu.Lock()
defer c.mu.Unlock()
c.clients[client] = make(map[string]bool)
}

func (c *Hub) removeClient(client *ThreadSafeClient) {
c.mu.RLock()
c.mu.Lock() // Use write lock for both checking and removal
defer c.mu.Unlock()
subscriptions, ok := c.clients[client]
c.mu.RUnlock()
if !ok {
return
}

c.mu.Lock()
delete(c.clients, client)
for symbol := range subscriptions {
delete(subscriptions, symbol)
Expand Down

0 comments on commit ffb4cd2

Please sign in to comment.