From e8b7b50f8607dc63704787ce07c066c8c385ff15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Kokelj?= Date: Wed, 22 Nov 2023 10:54:51 +0100 Subject: [PATCH] Prevent concurrent writes to a websocket connection (#1656) --- tools/walletextension/userconn/user_conn.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/walletextension/userconn/user_conn.go b/tools/walletextension/userconn/user_conn.go index e9bbc640ff..4acb9ebf2f 100644 --- a/tools/walletextension/userconn/user_conn.go +++ b/tools/walletextension/userconn/user_conn.go @@ -6,6 +6,7 @@ import ( "net/http" "net/url" "strings" + "sync" "github.com/ten-protocol/go-ten/go/common/log" @@ -39,6 +40,7 @@ type userConnWS struct { isClosed bool logger gethlog.Logger req *http.Request + mu sync.Mutex } func NewUserConnHTTP(resp http.ResponseWriter, req *http.Request, logger gethlog.Logger) UserConn { @@ -106,6 +108,9 @@ func (w *userConnWS) ReadRequest() ([]byte, error) { } func (w *userConnWS) WriteResponse(msg []byte) error { + w.mu.Lock() + defer w.mu.Unlock() + err := w.conn.WriteMessage(websocket.TextMessage, msg) if err != nil { if websocket.IsCloseError(err) || strings.Contains(string(msg), "EOF") {