Skip to content

Commit 6f5d213

Browse files
pnxcoreydaley
andauthored
conn.go: default close handler should not return ErrCloseSent. (#865)
<!-- For Work In Progress Pull Requests, please use the Draft PR feature, see https://github.blog/2019-02-14-introducing-draft-pull-requests/ for further details. For a timely review/response, please avoid force-pushing additional commits if your PR already received reviews or comments. Before submitting a Pull Request, please ensure that you have: - 📖 Read the Contributing guide: https://github.com/gorilla/.github/blob/main/CONTRIBUTING.md - 📖 Read the Code of Conduct: https://github.com/gorilla/.github/blob/main/CODE_OF_CONDUCT.md - Provide tests for your changes. - Use descriptive commit messages. - Comment your code where appropriate. - Squash your commits - Update any related documentation. - Add gorilla/pull-request-reviewers as a Reviewer --> ## What type of PR is this? (check all applicable) - [ ] Refactor - [ ] Feature - [x] Bug Fix - [ ] Optimization - [ ] Documentation Update - [ ] Go Version Update - [ ] Dependency Update ## Description Noticed a change in default close handler from 1.5.0 to 1.5.1 1.5.1 Now returns the error from `WriteControl` with `CloseMessage` type. However this results in ErrCloseSent returned if the connection initiated the close handshake. This is normally correct as the connection should not be able to write after a close handshake is initiated, except when calling the close handler. in that case nil should be returned so that `advanceFrame()` can return the actual close message. ## Related Tickets & Documents ## Added/updated tests? - [x] Yes - [ ] No, and this is why: _please replace this line with details on why tests have not been included_ - [ ] I need help with writing tests ## Run verifications and test - [ ] `make verify` is passing (fails with `GO-2023-2186 Incorrect detection of reserved device names on Windows in path/filepath` that is unrelated to this change) - [x] `make test` is passing Co-authored-by: Corey Daley <[email protected]>
1 parent 629990d commit 6f5d213

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

conn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,8 @@ func (c *Conn) SetCloseHandler(h func(code int, text string) error) {
11631163
if h == nil {
11641164
h = func(code int, text string) error {
11651165
message := FormatCloseMessage(code, "")
1166-
if err := c.WriteControl(CloseMessage, message, time.Now().Add(writeWait)); err != nil {
1166+
err := c.WriteControl(CloseMessage, message, time.Now().Add(writeWait))
1167+
if err != nil && err != ErrCloseSent {
11671168
return err
11681169
}
11691170
return nil

conn_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,26 @@ func TestWriteAfterMessageWriterClose(t *testing.T) {
477477
}
478478
}
479479

480+
func TestWriteHandlerDoesNotReturnErrCloseSent(t *testing.T) {
481+
var b1, b2 bytes.Buffer
482+
483+
client := newTestConn(&b2, &b1, false)
484+
server := newTestConn(&b1, &b2, true)
485+
486+
msg := FormatCloseMessage(CloseNormalClosure, "")
487+
if err := client.WriteMessage(CloseMessage, msg); err != nil {
488+
t.Fatalf("unexpected error when writing close message, %v", err)
489+
}
490+
491+
if _, _, err := server.NextReader(); !IsCloseError(err, 1000) {
492+
t.Fatalf("server expects a close message, %v returned", err)
493+
}
494+
495+
if _, _, err := client.NextReader(); !IsCloseError(err, 1000) {
496+
t.Fatalf("client expects a close message, %v returned", err)
497+
}
498+
}
499+
480500
func TestReadLimit(t *testing.T) {
481501
t.Run("Test ReadLimit is enforced", func(t *testing.T) {
482502
const readLimit = 512

0 commit comments

Comments
 (0)