@@ -180,7 +180,7 @@ func (c *Conn) readTillMsg(ctx context.Context) (header, error) {
180
180
if h .opcode .controlOp () {
181
181
err = c .handleControl (ctx , h )
182
182
if err != nil {
183
- return header {}, err
183
+ return header {}, xerrors . Errorf ( "failed to handle control frame: %w" , err )
184
184
}
185
185
continue
186
186
}
@@ -274,15 +274,10 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
274
274
case opClose :
275
275
ce , err := parseClosePayload (b )
276
276
if err != nil {
277
- err = xerrors .Errorf ("received invalid close payload: %w" , err )
278
- c .close (err )
279
- return err
280
- }
281
- if ce .Code == StatusNoStatusRcvd {
282
- c .writeClose (nil , ce )
283
- } else {
284
- c .Close (ce .Code , ce .Reason )
277
+ c .Close (StatusProtocolError , "received invalid close payload" )
278
+ return xerrors .Errorf ("received invalid close payload: %w" , err )
285
279
}
280
+ c .writeClose (b , ce , false )
286
281
return c .closeErr
287
282
default :
288
283
panic (fmt .Sprintf ("websocket: unexpected control opcode: %#v" , h ))
@@ -398,7 +393,7 @@ func (r *messageReader) read(p []byte) (int, error) {
398
393
}
399
394
400
395
if h .opcode != opContinuation {
401
- err := xerrors .Errorf ("received new data frame without finishing the previous frame " )
396
+ err := xerrors .Errorf ("received new data message without finishing the previous message " )
402
397
r .c .Close (StatusProtocolError , err .Error ())
403
398
return 0 , err
404
399
}
@@ -461,7 +456,7 @@ func (c *Conn) readFramePayload(ctx context.Context, p []byte) (int, error) {
461
456
err = ctx .Err ()
462
457
default :
463
458
}
464
- err = xerrors .Errorf ("failed to read from connection : %w" , err )
459
+ err = xerrors .Errorf ("failed to read frame payload : %w" , err )
465
460
c .releaseLock (c .readFrameLock )
466
461
c .close (err )
467
462
return n , err
@@ -651,7 +646,7 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte
651
646
default :
652
647
}
653
648
654
- err = xerrors .Errorf ("failed to write to connection : %w" , err )
649
+ err = xerrors .Errorf ("failed to write frame : %w" , err )
655
650
// We need to release the lock first before closing the connection to ensure
656
651
// the lock can be acquired inside close to ensure no one can access c.bw.
657
652
c .releaseLock (c .writeFrameLock )
@@ -764,20 +759,27 @@ func (c *Conn) exportedClose(code StatusCode, reason string) error {
764
759
p , _ = ce .bytes ()
765
760
}
766
761
767
- return c .writeClose (p , ce )
762
+ return c .writeClose (p , ce , true )
768
763
}
769
764
770
- func (c * Conn ) writeClose (p []byte , cerr CloseError ) error {
765
+ func (c * Conn ) writeClose (p []byte , err error , us bool ) error {
771
766
ctx , cancel := context .WithTimeout (context .Background (), time .Second * 5 )
772
767
defer cancel ()
773
768
774
- err := c .writeControl (ctx , opClose , p )
769
+ // If this fails, the connection had to have died.
770
+ err = c .writeControl (ctx , opClose , p )
775
771
if err != nil {
776
772
return err
777
773
}
778
774
779
- c .close (cerr )
780
- if ! xerrors .Is (c .closeErr , cerr ) {
775
+ if us {
776
+ err = xerrors .Errorf ("sent close frame: %w" , err )
777
+ } else {
778
+ err = xerrors .Errorf ("received close frame: %w" , err )
779
+ }
780
+
781
+ c .close (err )
782
+ if ! xerrors .Is (c .closeErr , err ) {
781
783
return c .closeErr
782
784
}
783
785
0 commit comments