From 283ee5696000f4a23944a861b9660f471408965f Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 4 Jul 2020 19:23:09 -0400 Subject: [PATCH] write.go: Fix deadlock in writeFrame Closes #248 Luckily, due to the 5s timeout on the close handshake, this would have had very minimal effects on anyone in production. --- write.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/write.go b/write.go index b1c57c1b..58bfdf9a 100644 --- a/write.go +++ b/write.go @@ -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. @@ -268,6 +267,7 @@ 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() @@ -275,6 +275,7 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco return 0, c.closeErr } } + defer c.writeFrameMu.unlock() select { case <-c.closed: