Skip to content

Commit 5a03e10

Browse files
committed
read: Fix CloseRead to have its own done channel
Context can be cancelled by parent. Doesn't indicate the CloseRead goroutine has exited.
1 parent c63f0c1 commit 5a03e10

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

close.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ func (c *Conn) waitGoroutines() error {
239239
}
240240

241241
c.closeReadMu.Lock()
242-
ctx := c.closeReadCtx
242+
closeRead := c.closeReadCtx != nil
243243
c.closeReadMu.Unlock()
244-
if ctx != nil {
244+
if closeRead {
245245
select {
246-
case <-ctx.Done():
246+
case <-c.closeReadDone:
247247
case <-t.C:
248248
return errors.New("failed to wait for close read goroutine to exit")
249249
}

conn.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ type Conn struct {
5757
timeoutLoopDone chan struct{}
5858

5959
// Read state.
60-
readMu *mu
61-
readHeaderBuf [8]byte
62-
readControlBuf [maxControlPayload]byte
63-
msgReader *msgReader
60+
readMu *mu
61+
readHeaderBuf [8]byte
62+
readControlBuf [maxControlPayload]byte
63+
msgReader *msgReader
6464

6565
// Write state.
6666
msgWriter *msgWriter
@@ -69,8 +69,9 @@ type Conn struct {
6969
writeHeaderBuf [8]byte
7070
writeHeader header
7171

72-
closeReadMu sync.Mutex
73-
closeReadCtx context.Context
72+
closeReadMu sync.Mutex
73+
closeReadCtx context.Context
74+
closeReadDone chan struct{}
7475

7576
closed chan struct{}
7677
closeMu sync.Mutex

mask_asm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package websocket
44

55
func mask(b []byte, key uint32) uint32 {
6-
// TODO: Will enable in v1.9.0.
6+
// TODO: Will enable in v1.9.0.
77
return maskGo(b, key)
88
if len(b) > 0 {
99
return maskAsm(&b[0], len(b), key)

read.go

+2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
7171
}
7272
ctx, cancel := context.WithCancel(ctx)
7373
c.closeReadCtx = ctx
74+
c.closeReadDone = make(chan struct{})
7475
c.closeReadMu.Unlock()
7576

7677
go func() {
78+
defer close(c.closeReadDone)
7779
defer cancel()
7880
defer c.close()
7981
_, _, err := c.Reader(ctx)

0 commit comments

Comments
 (0)