File tree Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -47,9 +47,10 @@ type Conn struct {
4747 // read limit for a message in bytes.
4848 msgReadLimit xsync.Int64
4949
50- wg sync.WaitGroup
50+ closeReadMu sync.Mutex
51+ closeReadCtx context.Context
52+
5153 closingMu sync.Mutex
52- isReadClosed xsync.Int64
5354 closeOnce sync.Once
5455 closed chan struct {}
5556 closeErrOnce sync.Once
@@ -130,7 +131,10 @@ func (c *Conn) closeWithInternal() {
130131// Read attempts to read a message from the connection.
131132// The maximum time spent waiting is bounded by the context.
132133func (c * Conn ) Read (ctx context.Context ) (MessageType , []byte , error ) {
133- if c .isReadClosed .Load () == 1 {
134+ c .closeReadMu .Lock ()
135+ closedRead := c .closeReadCtx != nil
136+ c .closeReadMu .Unlock ()
137+ if closedRead {
134138 return 0 , nil , errors .New ("WebSocket connection read closed" )
135139 }
136140
@@ -387,14 +391,18 @@ func (w *writer) Close() error {
387391
388392// CloseRead implements *Conn.CloseRead for wasm.
389393func (c * Conn ) CloseRead (ctx context.Context ) context.Context {
390- c .isReadClosed .Store (1 )
391-
394+ c .closeReadMu .Lock ()
395+ if c .closeReadCtx != nil {
396+ c .closeReadMu .Unlock ()
397+ return c .closeReadCtx
398+ }
392399 ctx , cancel := context .WithCancel (ctx )
393- c .wg .Add (1 )
400+ c .closeReadCtx = ctx
401+ c .closeReadMu .Unlock ()
402+
394403 go func () {
395- defer c .CloseNow ()
396- defer c .wg .Done ()
397404 defer cancel ()
405+ defer c .CloseNow ()
398406 _ , _ , err := c .read (ctx )
399407 if err != nil {
400408 c .Close (StatusPolicyViolation , "unexpected data message" )
You can’t perform that action at this time.
0 commit comments