Skip to content

Commit

Permalink
Fix error returned when closing VoiceClient
Browse files Browse the repository at this point in the history
* fix handling of error in nhooyr websocket wrapper (mentioned in andersfylling#298)
	* closing context given to g.c.Read(ctx) leads to WebSocket being closed
  	  coder/websocket#242
  • Loading branch information
oidq committed May 31, 2020
1 parent 6e97d75 commit 8da3c4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,9 @@ func (c *client) receiver(ctx context.Context) {
var packet []byte
var err error
if packet, err = c.conn.Read(ctx); err != nil {
c.log.Debug(c.getLogPrefix(), "read error: ", err.Error())
if !errors.Is(err, context.Canceled) && ctx.Err() != nil {
c.log.Debug(c.getLogPrefix(), "read error: ", err.Error())
}
reconnect := true
var closeErr *CloseErr
isCloseErr := errors.As(err, &closeErr)
Expand Down
6 changes: 6 additions & 0 deletions internal/gateway/websocket_nhooyr.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func (g *nhooyr) Read(ctx context.Context) (packet []byte, err error) {
var messageType websocket.MessageType
messageType, packet, err = g.c.Read(ctx)
if err != nil {
// Cancelling Read by ctx results in closed WS, see issue
// https://github.com/nhooyr/websocket/issues/242
if ctx.Err() != nil && errors.Is(err, context.Canceled) {
g.isConnected.Store(false)
return nil, context.Canceled
}
var closeErr websocket.CloseError
if errors.As(err, &closeErr) {
g.isConnected.Store(false)
Expand Down

0 comments on commit 8da3c4e

Please sign in to comment.