From c550701dfaf205fb99bc702fc2c59e0d79324ad3 Mon Sep 17 00:00:00 2001 From: Roger Ferdinan Date: Mon, 29 Sep 2025 22:53:29 +0700 Subject: [PATCH] fix: adding lock for cancel function slices --- v1/client/client.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/v1/client/client.go b/v1/client/client.go index 201a52e..04d82b1 100644 --- a/v1/client/client.go +++ b/v1/client/client.go @@ -235,7 +235,10 @@ func (wsClient *SafeWebsocketClient) connect() error { pingCtx, pingCancel := context.WithCancel(context.Background()) go wsClient.startPingTicker(pingCtx) - wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel) + wsClient.mu.WriteHandler(func() error { + wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel) + return nil + }) if wsClient.conn != nil { wsClient.conn.Close() @@ -251,7 +254,10 @@ func (wsClient *SafeWebsocketClient) connect() error { go func() { ctx, cancel := context.WithCancel(context.Background()) - wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel) + wsClient.mu.WriteHandler(func() error { + wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel) + return nil + }) for { select { case <-ctx.Done(): @@ -285,7 +291,10 @@ func (wsClient *SafeWebsocketClient) connect() error { go func() { ctx, cancel := context.WithCancel(context.Background()) - wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel) + wsClient.mu.WriteHandler(func() error { + wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel) + return nil + }) for { select { case <-ctx.Done():