Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os/exec"
"strings"
"testing"
"testing/synctest"
"time"

"github.com/coder/websocket"
Expand Down Expand Up @@ -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()

Expand Down
14 changes: 12 additions & 2 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading