@@ -180,7 +180,7 @@ func (c *Conn) readTillMsg(ctx context.Context) (header, error) {
180180 if h .opcode .controlOp () {
181181 err = c .handleControl (ctx , h )
182182 if err != nil {
183- return header {}, err
183+ return header {}, xerrors . Errorf ( "failed to handle control frame: %w" , err )
184184 }
185185 continue
186186 }
@@ -274,15 +274,10 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
274274 case opClose :
275275 ce , err := parseClosePayload (b )
276276 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 )
285279 }
280+ c .writeClose (b , ce , false )
286281 return c .closeErr
287282 default :
288283 panic (fmt .Sprintf ("websocket: unexpected control opcode: %#v" , h ))
@@ -398,7 +393,7 @@ func (r *messageReader) read(p []byte) (int, error) {
398393 }
399394
400395 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 " )
402397 r .c .Close (StatusProtocolError , err .Error ())
403398 return 0 , err
404399 }
@@ -461,7 +456,7 @@ func (c *Conn) readFramePayload(ctx context.Context, p []byte) (int, error) {
461456 err = ctx .Err ()
462457 default :
463458 }
464- err = xerrors .Errorf ("failed to read from connection : %w" , err )
459+ err = xerrors .Errorf ("failed to read frame payload : %w" , err )
465460 c .releaseLock (c .readFrameLock )
466461 c .close (err )
467462 return n , err
@@ -651,7 +646,7 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte
651646 default :
652647 }
653648
654- err = xerrors .Errorf ("failed to write to connection : %w" , err )
649+ err = xerrors .Errorf ("failed to write frame : %w" , err )
655650 // We need to release the lock first before closing the connection to ensure
656651 // the lock can be acquired inside close to ensure no one can access c.bw.
657652 c .releaseLock (c .writeFrameLock )
@@ -764,20 +759,27 @@ func (c *Conn) exportedClose(code StatusCode, reason string) error {
764759 p , _ = ce .bytes ()
765760 }
766761
767- return c .writeClose (p , ce )
762+ return c .writeClose (p , ce , true )
768763}
769764
770- func (c * Conn ) writeClose (p []byte , cerr CloseError ) error {
765+ func (c * Conn ) writeClose (p []byte , err error , us bool ) error {
771766 ctx , cancel := context .WithTimeout (context .Background (), time .Second * 5 )
772767 defer cancel ()
773768
774- err := c .writeControl (ctx , opClose , p )
769+ // If this fails, the connection had to have died.
770+ err = c .writeControl (ctx , opClose , p )
775771 if err != nil {
776772 return err
777773 }
778774
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 ) {
781783 return c .closeErr
782784 }
783785
0 commit comments