isapir / lucee-websocket

Enables server WebSockets for Lucee via JSR-356 compliant servlet containers (e.g. Tomcat 8, Jetty 9.1, etc.)
GNU Lesser General Public License v2.1
17 stars 6 forks source link

Broadcasting to WS via threads results in occasional errror #6

Closed psarin closed 6 years ago

psarin commented 7 years ago

Get the following error when broadcasting via threads: "The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method." Imagine that multiple threads end up broadcasting to WS at same time, and the WS isn't done with a prior request.

Have mitigated by surrounding the broadcast code in an exclusive lock. However, occasionally, I get an "a timeout occurred after 30 seconds trying to acquire a exclusive lock with name" error.

See https://dev.lucee.org/t/websocket-issues-hanging-server/2386 for exact error mesage and trace.

You can reproduce the issue by something like the following, where Application.WebSocketsConnectionManager should exist as using the normal WebsocketRegister("/ws/{channel}", new BroadcastListener())

for (threadNum=1; threadNum<=100; threadNum++){
thread action='run' name='thread_#threadNum#' {
try{
sleep(500);
if (!isNull(Application.WebSocketsConnnectionManager)){
Application.WebSocketsConnnectionManager.broadcast("testChannel", "Test message");
}
}catch (any e){
writelog(file="websockets", text=serializeJson({"error":e}));
}
}
}

isapir commented 6 years ago

Fixed for 2.0.1 -- now all of the calls to sendXxx are synchronized