Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conn.go: default close handler should not return ErrCloseSent. #865

Merged
merged 2 commits into from
Nov 13, 2023
Merged
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
3 changes: 2 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,8 @@ func (c *Conn) SetCloseHandler(h func(code int, text string) error) {
if h == nil {
h = func(code int, text string) error {
message := FormatCloseMessage(code, "")
if err := c.WriteControl(CloseMessage, message, time.Now().Add(writeWait)); err != nil {
err := c.WriteControl(CloseMessage, message, time.Now().Add(writeWait))
if err != nil && err != ErrCloseSent {
return err
}
return nil
Expand Down
20 changes: 20 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,26 @@ func TestWriteAfterMessageWriterClose(t *testing.T) {
}
}

func TestWriteHandlerDoesNotReturnErrCloseSent(t *testing.T) {
var b1, b2 bytes.Buffer

client := newTestConn(&b2, &b1, false)
server := newTestConn(&b1, &b2, true)

msg := FormatCloseMessage(CloseNormalClosure, "")
if err := client.WriteMessage(CloseMessage, msg); err != nil {
t.Fatalf("unexpected error when writing close message, %v", err)
}

if _, _, err := server.NextReader(); !IsCloseError(err, 1000) {
t.Fatalf("server expects a close message, %v returned", err)
}

if _, _, err := client.NextReader(); !IsCloseError(err, 1000) {
t.Fatalf("client expects a close message, %v returned", err)
}
}

func TestReadLimit(t *testing.T) {
t.Run("Test ReadLimit is enforced", func(t *testing.T) {
const readLimit = 512
Expand Down
Loading