Skip to content

Commit 5bbfaf2

Browse files
committed
fix race
1 parent 2b5828d commit 5bbfaf2

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

block/internal/da/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ func (c *client) HasForcedInclusionNamespace() bool {
491491
// channel-based subscriptions (WebSocket). Reads the live IsWebSocket flag
492492
// from the jsonrpc client so transport upgrades are visible immediately.
493493
func (c *client) SupportsSubscribe() bool {
494-
return c.da != nil && c.da.IsWebSocket
494+
return c.da != nil && c.da.IsWebSocket.Load()
495495
}
496496

497497
// Subscribe subscribes to blobs in the given namespace via the celestia-node

pkg/da/jsonrpc/client.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"strings"
88
"sync"
9+
"sync/atomic"
910
"time"
1011

1112
libshare "github.com/celestiaorg/go-square/v3/share"
@@ -17,7 +18,7 @@ import (
1718
type Client struct {
1819
Blob BlobAPI
1920
Header HeaderAPI
20-
IsWebSocket bool
21+
IsWebSocket atomic.Bool
2122

2223
mu sync.Mutex
2324
closer jsonrpc.ClientCloser
@@ -35,9 +36,10 @@ func (c *Client) Close() {
3536
c.retryCancel()
3637
c.retryCancel = nil
3738
}
39+
closer := c.closer
3840
c.mu.Unlock()
39-
if c.closer != nil {
40-
c.closer()
41+
if closer != nil {
42+
closer()
4143
}
4244
}
4345

@@ -100,7 +102,7 @@ func NewWSClient(ctx context.Context, logger zerolog.Logger, addr, token string,
100102
if err != nil {
101103
return nil, err
102104
}
103-
client.IsWebSocket = false
105+
client.IsWebSocket.Store(false)
104106

105107
// Retry WS in the background so transient outages don't force a permanent downgrade.
106108
retryCtx, retryCancel := context.WithCancel(context.Background())
@@ -110,7 +112,7 @@ func NewWSClient(ctx context.Context, logger zerolog.Logger, addr, token string,
110112
return client, nil
111113
}
112114

113-
client.IsWebSocket = true
115+
client.IsWebSocket.Store(true)
114116
return client, nil
115117
}
116118

@@ -147,7 +149,7 @@ func (c *Client) tryUpgradeWS(ctx context.Context, logger zerolog.Logger, addr,
147149
defer c.mu.Unlock()
148150

149151
// Another goroutine may have already upgraded.
150-
if c.IsWebSocket {
152+
if c.IsWebSocket.Load() {
151153
wsClient.Close()
152154
return true
153155
}
@@ -165,7 +167,7 @@ func (c *Client) tryUpgradeWS(ctx context.Context, logger zerolog.Logger, addr,
165167
}
166168
}
167169

168-
c.IsWebSocket = true
170+
c.IsWebSocket.Store(true)
169171
logger.Info().Msg("DA websocket connection restored, switching back from HTTP polling")
170172
return true
171173
}

0 commit comments

Comments
 (0)