Skip to content

Commit 23476d4

Browse files
committed
Prevent concurrent writes to a websocket connection
1 parent 4321483 commit 23476d4

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

tools/walletextension/userconn/user_conn.go

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/url"
88
"strings"
9+
"sync"
910

1011
"github.com/ten-protocol/go-ten/go/common/log"
1112

@@ -39,6 +40,7 @@ type userConnWS struct {
3940
isClosed bool
4041
logger gethlog.Logger
4142
req *http.Request
43+
mu sync.Mutex
4244
}
4345

4446
func NewUserConnHTTP(resp http.ResponseWriter, req *http.Request, logger gethlog.Logger) UserConn {
@@ -106,6 +108,9 @@ func (w *userConnWS) ReadRequest() ([]byte, error) {
106108
}
107109

108110
func (w *userConnWS) WriteResponse(msg []byte) error {
111+
w.mu.Lock()
112+
defer w.mu.Unlock()
113+
109114
err := w.conn.WriteMessage(websocket.TextMessage, msg)
110115
if err != nil {
111116
if websocket.IsCloseError(err) || strings.Contains(string(msg), "EOF") {

0 commit comments

Comments
 (0)