File tree 1 file changed +16
-8
lines changed
1 file changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -47,9 +47,10 @@ type Conn struct {
47
47
// read limit for a message in bytes.
48
48
msgReadLimit xsync.Int64
49
49
50
- wg sync.WaitGroup
50
+ closeReadMu sync.Mutex
51
+ closeReadCtx context.Context
52
+
51
53
closingMu sync.Mutex
52
- isReadClosed xsync.Int64
53
54
closeOnce sync.Once
54
55
closed chan struct {}
55
56
closeErrOnce sync.Once
@@ -130,7 +131,10 @@ func (c *Conn) closeWithInternal() {
130
131
// Read attempts to read a message from the connection.
131
132
// The maximum time spent waiting is bounded by the context.
132
133
func (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 {
134
138
return 0 , nil , errors .New ("WebSocket connection read closed" )
135
139
}
136
140
@@ -387,14 +391,18 @@ func (w *writer) Close() error {
387
391
388
392
// CloseRead implements *Conn.CloseRead for wasm.
389
393
func (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
+ }
392
399
ctx , cancel := context .WithCancel (ctx )
393
- c .wg .Add (1 )
400
+ c .closeReadCtx = ctx
401
+ c .closeReadMu .Unlock ()
402
+
394
403
go func () {
395
- defer c .CloseNow ()
396
- defer c .wg .Done ()
397
404
defer cancel ()
405
+ defer c .CloseNow ()
398
406
_ , _ , err := c .read (ctx )
399
407
if err != nil {
400
408
c .Close (StatusPolicyViolation , "unexpected data message" )
You can’t perform that action at this time.
0 commit comments