Skip to content

Commit c4d4650

Browse files
committed
Fix bad close handshake logic
This doesn't affect real world applications due to buffering but the testss would occasionally fail on CI due to the code not handling an immediate close after writing the close frame while resetting the write timeout.
1 parent ba35516 commit c4d4650

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

close_notjs.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (c *Conn) closeHandshake(code StatusCode, reason string) (err error) {
4444
if CloseStatus(closeHandshakeErr) == -1 {
4545
return closeHandshakeErr
4646
}
47+
4748
return nil
4849
}
4950

@@ -63,23 +64,28 @@ func (c *Conn) writeClose(code StatusCode, reason string) error {
6364
Reason: reason,
6465
}
6566

66-
// TODO one problem with this is that if the connection is actually closed in the meantime, the error returned below will be this one lol.
67-
c.setCloseErr(fmt.Errorf("sent close frame: %w", ce))
68-
6967
var p []byte
70-
var err error
68+
var marshalErr error
7169
if ce.Code != StatusNoStatusRcvd {
72-
p, err = ce.bytes()
73-
if err != nil {
74-
log.Printf("websocket: %v", err)
70+
p, marshalErr = ce.bytes()
71+
if marshalErr != nil {
72+
log.Printf("websocket: %v", marshalErr)
7573
}
7674
}
7775

78-
werr := c.writeControl(context.Background(), opClose, p)
79-
if err != nil {
80-
return err
76+
writeErr := c.writeControl(context.Background(), opClose, p)
77+
if CloseStatus(writeErr) != -1 {
78+
// Not a real error if it's due to a close frame being received.
79+
writeErr = nil
80+
}
81+
82+
// We do this after in case there was an error writing the close frame.
83+
c.setCloseErr(fmt.Errorf("sent close frame: %w", ce))
84+
85+
if marshalErr != nil {
86+
return marshalErr
8187
}
82-
return werr
88+
return writeErr
8389
}
8490

8591
func (c *Conn) waitCloseHandshake() error {

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
44
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
55
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
66
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
7-
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
8-
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
97
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
108
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
119
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
1210
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1311
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
1412
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
15-
github.com/klauspost/compress v1.10.0 h1:92XGj1AcYzA6UrVdd4qIIBrT8OroryvRvdmg/IfmC7Y=
16-
github.com/klauspost/compress v1.10.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
1713
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
1814
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
1915
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=

0 commit comments

Comments
 (0)