diff --git a/write.go b/write.go
index baa5e6e261f5732b06101cb2bb9d5f5ca513d8a1..2d439de70894790e31bf5cf6d249c6762ee288f5 100644
--- a/write.go
+++ b/write.go
@@ -246,7 +246,13 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco
 	if err != nil {
 		return 0, err
 	}
-	defer c.writeFrameMu.unlock()
+	defer func() {
+		// We leave it locked when writing the close frame to avoid
+		// any other goroutine writing any other frame.
+		if opcode != opClose {
+			c.writeFrameMu.unlock()
+		}
+	}()
 
 	select {
 	case <-c.closed: