Atmosphere/atmosphere — pull request #1781 by dmitry-treskunov (2014-11-20 18:03:06+00:00)
Title: #1773: JSR356WebSocket - acquire semaphore with timeout

The issue https://github.com/Atmosphere/atmosphere/issues/1773 is still reproduced using 2.2.4-snapshot.
Trying to fix it using this patch (or at least to get more information about errors). Please merge in into 2.2.4 branch

New details:

One AsyncOp thread is blocked on the semaphore acquire:

```
"Atmosphere-Shared-AsyncOp-679" daemon group="main" prio=5 tid=272,666 nid=272,666 waiting
java.lang.Thread.State: WAITING
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:834)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireShared(AbstractQueuedSynchronizer.java:964)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireShared(AbstractQueuedSynchronizer.java:1282)
at java.util.concurrent.Semaphore.acquireUninterruptibly(Semaphore.java:340)
at org.atmosphere.container.version.JSR356WebSocket.write(JSR356WebSocket.java:61)
at org.atmosphere.websocket.WebSocket.write(WebSocket.java:222)
at org.atmosphere.websocket.WebSocket.write(WebSocket.java:187)
at org.atmosphere.websocket.WebSocket.write(WebSocket.java:42)
at org.atmosphere.cpr.AtmosphereResponse$2.write(AtmosphereResponse.java:509)
at org.atmosphere.handler.AbstractReflectorAtmosphereHandler.onStateChange(AbstractReflectorAtmosphereHandler.java:148)
at org.atmosphere.cpr.DefaultBroadcaster.invokeOnStateChange(DefaultBroadcaster.java:1027)
at org.atmosphere.cpr.DefaultBroadcaster.prepareInvokeOnStateChange(DefaultBroadcaster.java:1047)
at org.atmosphere.cpr.DefaultBroadcaster.executeAsyncWrite(DefaultBroadcaster.java:888)
at org.atmosphere.cpr.DefaultBroadcaster$3.run(DefaultBroadcaster.java:519)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
```
One http thread is blocked also:
```
"http-nio-80-exec-61" daemon group="main" prio=5 tid=100 nid=100 blocked on org.atmosphere.cpr.AtmosphereResourceImpl@79709eca
by "Atmosphere-Shared-AsyncOp-679"
java.lang.Thread.State: BLOCKED
at org.atmosphere.cpr.AsynchronousProcessor.completeLifecycle(AsynchronousProcessor.java:424)
at org.atmosphere.cpr.AsynchronousProcessor.endRequest(AsynchronousProcessor.java:554)
at org.atmosphere.websocket.DefaultWebSocketProcessor.executeClose(DefaultWebSocketProcessor.java:679)
at org.atmosphere.websocket.DefaultWebSocketProcessor.close(DefaultWebSocketProcessor.java:630)
at org.atmosphere.container.JSR356Endpoint.onClose(JSR356Endpoint.java:244)
at org.apache.tomcat.websocket.WsSession.fireEndpointOnClose(WsSession.java:481)
at org.apache.tomcat.websocket.WsSession.onClose(WsSession.java:464)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.close(WsHttpUpgradeHandler.java:183)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.access$200(WsHttpUpgradeHandler.java:48)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:209)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:194)
at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:95)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:653)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
```
All other nine AsyncOp are blocked on the same syncronized block:
```
"Atmosphere-Shared-AsyncOp-685" daemon group="main" prio=5 tid=272,876 nid=272,876 blocked on org.atmosphere.cpr.AtmosphereResourceImpl@79709eca
by "Atmosphere-Shared-AsyncOp-679"
java.lang.Thread.State: BLOCKED
at org.atmosphere.cpr.DefaultBroadcaster$3.run(DefaultBroadcaster.java:516)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
```