Skip to content

Commit

Permalink
read: Fix CloseRead to have its own done channel
Browse files Browse the repository at this point in the history
Context can be cancelled by parent. Doesn't indicate the CloseRead goroutine
has exited.
  • Loading branch information
nhooyr committed Apr 7, 2024
1 parent 7c5b012 commit 36cc06c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions close.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ func (c *Conn) waitGoroutines() error {
}

c.closeReadMu.Lock()
ctx := c.closeReadCtx
closeRead := c.closeReadCtx != nil
c.closeReadMu.Unlock()
if ctx != nil {
if closeRead {
select {
case <-ctx.Done():
case <-c.closeReadDone:
case <-t.C:
return errors.New("failed to wait for close read goroutine to exit")
}
Expand Down
13 changes: 7 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ type Conn struct {
timeoutLoopDone chan struct{}

// Read state.
readMu *mu
readHeaderBuf [8]byte
readControlBuf [maxControlPayload]byte
msgReader *msgReader
readMu *mu
readHeaderBuf [8]byte
readControlBuf [maxControlPayload]byte
msgReader *msgReader

// Write state.
msgWriter *msgWriter
Expand All @@ -69,8 +69,9 @@ type Conn struct {
writeHeaderBuf [8]byte
writeHeader header

closeReadMu sync.Mutex
closeReadCtx context.Context
closeReadMu sync.Mutex
closeReadCtx context.Context
closeReadDone chan struct{}

closed chan struct{}
closeMu sync.Mutex
Expand Down
2 changes: 1 addition & 1 deletion mask_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package websocket

func mask(b []byte, key uint32) uint32 {
// TODO: Will enable in v1.9.0.
// TODO: Will enable in v1.9.0.
return maskGo(b, key)
if len(b) > 0 {

Check failure on line 8 in mask_asm.go

View workflow job for this annotation

GitHub Actions / lint

unreachable code
return maskAsm(&b[0], len(b), key)
Expand Down
2 changes: 2 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
}
ctx, cancel := context.WithCancel(ctx)
c.closeReadCtx = ctx
c.closeReadDone = make(chan struct{})
c.closeReadMu.Unlock()

go func() {
defer close(c.closeReadDone)
defer cancel()
defer c.close()
_, _, err := c.Reader(ctx)
Expand Down

0 comments on commit 36cc06c

Please sign in to comment.