Skip to content

Commit

Permalink
write.go: Fix deadlock in writeFrame (#253)
Browse files Browse the repository at this point in the history
Closes #248

Luckily, due to the 5s timeout on the close handshake, this would have
had very minimal effects on anyone in production.
  • Loading branch information
nhooyr committed Jul 5, 2020
1 parent 493ebbe commit 897a573
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion write.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco
if err != nil {
return 0, err
}
defer c.writeFrameMu.unlock()

// If the state says a close has already been written, we wait until
// the connection is closed and return that error.
Expand All @@ -268,13 +267,15 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco
wroteClose := c.wroteClose
c.closeMu.Unlock()
if wroteClose && opcode != opClose {
c.writeFrameMu.unlock()
select {
case <-ctx.Done():
return 0, ctx.Err()
case <-c.closed:
return 0, c.closeErr
}
}
defer c.writeFrameMu.unlock()

select {
case <-c.closed:
Expand Down

0 comments on commit 897a573

Please sign in to comment.