Skip to content

Commit 12a35d7

Browse files
Merge pull request #306 from bloXroute-Labs/fix-subscription-panic
Fix race condition that leads to panic
2 parents 870d5e1 + 54a0a58 commit 12a35d7

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

rpc/ws/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ func (c *Client) handleSubscriptionMessage(subID uint64, message []byte) {
254254
return
255255
}
256256

257+
sub.mutex.Lock()
258+
defer sub.mutex.Unlock()
257259
if !sub.closed {
258260
sub.stream <- result
259261
}
260-
return
261262
}
262263

263264
func (c *Client) closeAllSubscription(err error) {

rpc/ws/subscription.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717

1818
package ws
1919

20-
import "context"
20+
import (
21+
"context"
22+
"sync"
23+
)
2124

2225
type Subscription struct {
2326
req *request
2427
subID uint64
2528
stream chan result
2629
err chan error
2730
closeFunc func(err error)
31+
mutex sync.Mutex
2832
closed bool
2933
unsubscribeMethod string
3034
decoderFunc decoderFunc
@@ -65,6 +69,8 @@ func (s *Subscription) Unsubscribe() {
6569
}
6670

6771
func (s *Subscription) unsubscribe(err error) {
72+
s.mutex.Lock()
73+
defer s.mutex.Unlock()
6874
s.closeFunc(err)
6975
s.closed = true
7076
close(s.stream)

0 commit comments

Comments
 (0)