Skip to content

Commit

Permalink
Prevent concurrent writes to a websocket connection (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj authored Nov 22, 2023
1 parent 4321483 commit e8b7b50
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tools/walletextension/userconn/user_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"

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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit e8b7b50

Please sign in to comment.