uNetworking / uWebSockets

Simple, secure & standards compliant web server for the most demanding of applications
Apache License 2.0
17.24k stars 1.75k forks source link

Common threading issues #1199

Closed scalpel4k closed 3 years ago

scalpel4k commented 3 years ago

Hi,

I have a single threaded uWebSockets server running that puts incoming messages into a per-connection message queue. The application itself is multi-threaded, but sending is sequential and receiving is sequential as per client connection. Now, when I serve multiple connections and I send messages in parallel to all clients it happens that one of the clients closes the connection with a 1002 error, saying that RSV bit(s) are incorrectly set (that application uses tyrus). The sending is triggered from the same event, so the send requests are pretty close to each other. I can subscribe to the event at runtime, so when I'm not subscribed, the client that has the problem runs without issue.

I can't really say that it's a server problem or a client problem.

Is there anything that I can do to analyse the root cause, or am I using uWebSockets incorrectly?

thx Michi

ghost commented 3 years ago

It sounds like you are assuming every connection would be isolated with no side effects other than for itself. That's not the case and I'm almost certain you're not doing things correctly. There is a user manual you can read

ghost commented 3 years ago

The library is single threaded. Meaning, you can absolutely not mix threads. The only function that can be called from any thread is defer

scalpel4k commented 3 years ago

Alex, sorry for my ignorance but I want to make sure I understand this correctly. Let's say I have 2 socket from the http upgrade, ws1 and ws2 (of type uWS::WebSocket<> which is a subclass of AsyncSocket). I can't s1->send(msg) and s2->send(msg) from 2 different threads?

sorry to have bothered you again Michi

ghost commented 3 years ago

can't s1->send(msg) and s2->send(msg) from 2 different threads?

Nope. That definitely will wreck havoc. The entire App and Loop has to be entirely isolated to only one thread. There are Discussions about this you can read about, it is a common question so I have closed this one here.