Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit 5ca5b7c

Browse files
committed
fixed: correctly cancel context during retries
The loop handling reconnection was using the main context, never canceling pending requests, that could end up in a connection storm and filling of the incoming message channel.
1 parent f18c2ce commit 5ca5b7c

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

client/subscribe_ws.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ func SubscribeWS(ctx context.Context, url string, tlsConfig *tls.Config) (chan [
4040
}
4141
isReconnect = true
4242

43+
wsctx, cancel := context.WithCancel(ctx)
44+
defer cancel()
45+
4346
conn, resp, err := wsc.Connect(
44-
ctx,
47+
wsctx,
4548
strings.Replace(url+"/subscribe/ws", "https", "wss", 1),
4649
wsc.Config{
4750
TLSConfig: tlsConfig,
@@ -52,11 +55,13 @@ func SubscribeWS(ctx context.Context, url string, tlsConfig *tls.Config) (chan [
5255
)
5356
if err != nil {
5457
log.Printf("unable to connect to ws (retrying): %s", err)
58+
cancel()
5559
continue
5660
}
5761

5862
if resp.StatusCode != http.StatusSwitchingProtocols {
5963
log.Printf("server rejected ws connection (retrying): %s", err)
64+
cancel()
6065
continue
6166
}
6267

@@ -71,6 +76,7 @@ func SubscribeWS(ctx context.Context, url string, tlsConfig *tls.Config) (chan [
7176
} else {
7277
log.Println("ws server gone. reconnecting...")
7378
}
79+
cancel()
7480
continue MAINWS
7581

7682
case data := <-conn.Read():

0 commit comments

Comments
 (0)