From 14afcb3dc567d31857c6d1f2a46ab579560f3b2e Mon Sep 17 00:00:00 2001 From: winter-hu Date: Tue, 9 Jul 2024 12:30:38 +0800 Subject: [PATCH] Update server.go The original writing method may cause deadlocks as it affects lock copies --- src/net/http/server.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/net/http/server.go b/src/net/http/server.go index 1ff72a04550..6c0d94ad1df 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -315,6 +315,8 @@ func (c *conn) hijacked() bool { // c.mu must be held. func (c *conn) hijackLocked() (rwc net.Conn, buf *bufio.ReadWriter, err error) { + c.mu.Lock() + defer c.mu.Unlock() if c.hijackedv { return nil, nil, ErrHijacked } @@ -2161,13 +2163,9 @@ func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) { w.cw.flush() } - c := w.conn - c.mu.Lock() - defer c.mu.Unlock() - // Release the bufioWriter that writes to the chunk writer, it is not // used after a connection has been hijacked. - rwc, buf, err = c.hijackLocked() + rwc, buf, err = w.conn.hijackLocked() if err == nil { putBufioWriter(w.w) w.w = nil