From eb435b24b852e7dc258e2ca67bc98917860a4857 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 17 Nov 2026 14:01:07 +0000 Subject: [PATCH] fix: stop CloseRead from waiting on itself When the CloseRead goroutine reads a data message it calls Close, and Close waits for that same goroutine to exit. The wait ended only on its 15 second timeout, so the CloseRead context was cancelled 15 seconds late and a concurrent Close stalled for as long. The goroutine now runs the close handshake itself. When another Close is already running, it waits for that Close to close the connection, because exiting earlier closes the connection before that Close has finished its handshake. --- conn_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ read.go | 14 +++++++++-- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/conn_test.go b/conn_test.go index c03c02b3..3de02e89 100644 --- a/conn_test.go +++ b/conn_test.go @@ -15,6 +15,7 @@ import ( "os/exec" "strings" "testing" + "testing/synctest" "time" "github.com/coder/websocket" @@ -837,6 +838,75 @@ func TestConcurrentClosePing(t *testing.T) { } } +func TestCloseReadDataMessage(t *testing.T) { + t.Parallel() + + // In a synctest bubble the clock advances only while every goroutine is + // blocked, so a close that is still pending after synctest.Wait is + // waiting on a timer. + + t.Run("closesConn", func(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + c1, c2 := wstest.Pipe(nil, nil) + defer c1.CloseNow() + defer c2.CloseNow() + + closeReadCtx := c1.CloseRead(context.Background()) + readErr := xsync.Go(func() error { + _, _, err := c2.Read(context.Background()) + return err + }) + err := c2.Write(context.Background(), websocket.MessageText, []byte("x")) + assert.Success(t, err) + assert.Success(t, assertCloseStatus(websocket.StatusPolicyViolation, <-readErr)) + + synctest.Wait() + select { + case <-closeReadCtx.Done(): + default: + t.Fatal("CloseRead context is not done after the close handshake") + } + }) + }) + + t.Run("duringClose", func(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + c1, c2 := wstest.Pipe(nil, nil) + defer c1.CloseNow() + defer c2.CloseNow() + + closeReadCtx := c1.CloseRead(context.Background()) + closeErr := xsync.Go(func() error { + return c1.Close(websocket.StatusNormalClosure, "") + }) + // Close stays blocked writing its close frame until c2 reads, + // so the CloseRead goroutine reads the data message during + // Close. + synctest.Wait() + + err := c2.Write(context.Background(), websocket.MessageText, []byte("x")) + assert.Success(t, err) + _, _, err = c2.Read(context.Background()) + assert.Success(t, assertCloseStatus(websocket.StatusNormalClosure, err)) + + synctest.Wait() + select { + case err := <-closeErr: + assert.Success(t, err) + default: + t.Fatal("Close has not returned after the close handshake") + } + select { + case <-closeReadCtx.Done(): + default: + t.Fatal("CloseRead context is not done after Close returned") + } + }) + }) +} + func TestConnClosePropagation(t *testing.T) { t.Parallel() diff --git a/read.go b/read.go index 5ef42734..4433e7f1 100644 --- a/read.go +++ b/read.go @@ -78,9 +78,19 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { defer cancel() defer c.close() _, _, err := c.Reader(ctx) - if err == nil { - c.Close(StatusPolicyViolation, "unexpected data message") + if err != nil { + return + } + // Close cannot be used here, because it waits for this goroutine to + // exit. + if c.casClosing() { + // A concurrent Close is closing the connection. The deferred + // close must not run before that Close's handshake is done. + <-c.closed + return } + ce, _ := wireCloseError(StatusPolicyViolation, "unexpected data message") + _ = c.closeHandshake(ce) }() return ctx }