-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[question] concurrent write to websocket connection with mutex.Lock() #828
Comments
The stack trace points to the |
@Jrenk hello, could you provide a reproducing example/test/benchmark then to make it clear that it's an issue of this package? |
@Jrenk I'm running into a similar situation. I have a loop in a goroutine that writes to an incoming socket connection for the heartbeat response. Before each write, I lock a common mutex and unlock after the write. The issue seems like it may be tied to a race condition as the issue doesn't happen consistently. Did you figure out what was causing your issue? |
@NomNomCameron No I didn't figure it out. The problem happened very rarely and it didn't occur for a very long time. I'm still not sure what causes this sadly. |
Could it be someone pinged your websocket server and the default ping handler tried to write a pong while it was locked? edit:I might test this later today, we'll see. |
I'm imagining a pull request where configuring the websockets server would ask if you want to stick a lock or mutex or something a couple layers deep. Any thoughts on that? |
@ayjayt that could actually be the problem. Let me know if you tested this. A lock in the package itself could resolve this as you said. |
Yeah, I mean it has to be it. I might test it, but you should use
Here is what the default handler looks like:
from: Line 1158 in 9111bb8
|
@coreydaley is this not a bug? If I'm correct, anyone who uses any edit: especially since most people are going to run their read loops and write loops in separate go routines, and the default handler is actually called automatically by |
From Gorilla WebSocket docs:
As I mentioned above - if you think it's a bug in a package you better provide a reproducer instead of guessing. You can use https://pkg.go.dev/golang.org/x/tools/cmd/stress tool to increase the chance of finding the reliable reproducer. |
If you want me to spend my time working on this project, and building something to reproduce this bug, you better be nice to me! We're having a discussion before spending time writing code. [contributors note: redaction was not needed] |
The websocket connection uses a mutex (implemented using a channel) to prevent (Why the websocket connection uses a channel instead of a sync.Mutex is a puzzle for another day.) |
Please keep all comments and conversations civil, please keep in mind that Github users are from all over the world and speak hundred of different languages, and their native languages are not english, but they are being gracious enough to use english here. So do not assume that anything is being said with a specific tone or meaning. Disrespectful language will not be tolerated in this community. We are almost done with the project transition and then we will begin triaging and working on issues, but we are not quite to that point yet. If you think that you have found a bug and would like to submit a pull request, please do so and we will review it as part of triaging/addressing this issue. |
If I'm going to be chastised for asking someone to speak respectfully to me, I will simply not participate here further. |
That is your prerogative. This is not the first comment within your short time of contributing to this project that could be deemed As far as I am concerned that is two strikes. One more and we will have to discuss blocking you from interacting with the organziation. |
@e9x That sort of comment will not be tolerated. I sincerely hope that you are able to find help that allows you to be a more civil member of society. |
Not that it will do any good but I reported the comment from @e9x, not sure what I did to elicit such a comment. |
Alright, I'm very tired, but I think I've reviewed this ticket enough to come to the conclusion that this should technically be considered a bug. While the documentation does specify that it supports only one reader/writer doing so concurrently at a time, it doesn't make it clear that if you're doing your own concurrent writes you'd also have to overwrite the ping handler. In this case the appropriate solution is likely to document the need for the ping handler being specified if performing your own concurrent writing and to have this resolved in what ever future version of this library. |
That's not true. WriteControl, the method used by the ping handler, can be called concurrently with the other write methods. The connection is implemented to support the concurrent call. The documentation states that the concurrent call is allowed. The Edit: Previous comments (1, 2) also explain why the ping handler is not a problem. |
Describe the problem you're having
Using the gorilla/websocket package I sometimes get the following error, despite using mutex.Lock:
concurrent write to websocket connection
Versions
1.18
v1.5.0
"Show me the code!"
In an http handler the websocket clients get created:
The
sendMessage
function gets called from multiple go-routines and theWriteJSON
never gets called directly anywhere else. From my understanding themutex.Lock()
should prevent concurrent websocket writes.The text was updated successfully, but these errors were encountered: