-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
failed to close writer: context canceled #474
Comments
Hey @lukaspj, thanks for the user experience report. You are definitely not the first! This is a very common mistake to make. There is one valid use-case for I see this issue is further exacerbated by the readme and examples using it incorrectly (https://github.com/coder/websocket?tab=readme-ov-file#server). We should at least update the readmes and examples, but I'll try to think about some alternative solutions as well. Perhaps the most obvious way would be to put a context on Anyway, if you have anything else to add, how you wish this had worked instead, what lead you down the wrong path, etc, please feel free to share. 1 A valid use-case is when you're running the websocket logic in a separate goroutine, and want it to stop when the HTTP handler exits. Usually the logic runs in the HTTP handler, though, where using |
I think you pin-pointed it, if the readme and examples explained it, it would probably help. Additionally, it would be nice if the function documentation would state this danger of closing the context. Something like "if the context is cancelled, it can cause a deadlock" or just "the context should be the context of the websocket not the context of the message being written". Idk if there's a nice way to write it tbh, but would be nice if it showed up on the documentation somewhere on "how to use this library". |
@mafredri I don't think a docs update alone will resolve this issue. @lukaspj seems to be saying that if the context you pass to |
@spikecurtis I was thinking about the original report, but that's a good callout. We should split the second issue into a new ticket. There may be multiple issues in how connection closure is propagated and context cancellation is handled. There seems to be multiple code-paths depending on scenario. I also noticed while looking at #476 that a read/write context that is cancelled actually ends up closing the connection abruptly, whereas I would expect we just give up on the write. |
I'm using this library to create a zero-trust tunnel into a Kubernetes cluster, when I encountered this error message:
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:
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!
The text was updated successfully, but these errors were encountered: