coder / coder/websocket

failed to close writer: context canceled

Ouverte
#474 12 commentaires 0 réactions 1 personne assignée Réclamée par @mafredri Voir sur GitHub
bug
Langage dominant
Go
Étoiles
5.5k
Forks
372
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

I'm using this library to create a zero-trust tunnel into a Kubernetes cluster, when I encountered this error message:

> failed to write msg: failed to close writer: failed to acquire lock: context canceled

So what happened was that when client closed the connection, I would send a "connection closed" message over the WebSocket connection, but I (mistakenly) used the request context for the write command.
This context would get cancelled somewhere around this Write call, sometimes after message was written but before it released the lock.

As a result, the lock would never get released and the websocket was no longer usable.

The solution was to create a new context for the write:
```go
writeCtx, _ := context.WithTimeout(context.Background(), 2*time.Second)

err = c.Websocket.Write(writeCtx, websocket.MessageText, marshal)
```

And I don't think I should have used the request context to begin with, but I do think it's not optimal behaviour that the websocket can be left in a broken state by mistimed context cancel.

In any case, just thought I'd share it. Thanks for maintaining this awesome library!

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.