Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] session.sendText(str, Callback.from(session::demand, Throwable::printStackTrace)) in a loop results in java.nio.channels.ReadPendingException

The demand will allow you to receive the next websocket frame/message.
But if you have already demanded and are awaiting a call to onWebSocketText and you demand again, you will get a ReadPendingException.

So per each onWebSocketText you should demand only once.
In your code you are demanding twice for each session because you are sending twice.

On Fri, Nov 8, 2024 at 9:29 PM Alexander Farber via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
Hello,

I am migrating a Websockets game from Jetty 10 to Jetty 12 with no auto-demand
and have replaced org.eclipse.jetty.websocket.api.Callback.NOOP by the new callback
in code below, so that I can call the "demand()" method:

    // the method below is called by the public void onWebSocketText(String str)

    private void handleNewGame() throws SQLException, IOException {
           // here some PostgreSQL code

            String gidJsonStr = String.format("{\"gid\":%d}", gid);
            // a new game has been created, get the new games list
            String gamesJsonStr = mServlet.getGames(mUid);

            for (Session session : Client.getOpenSessions(mUid)) {
                // send updated games list to all open sessions of this user
                session.sendText(gamesJsonStr, Callback.from(session::demand, Throwable::printStackTrace));
                // send the new game id to all open sessions of this user
                session.sendText(gidJsonStr, Callback.from(session::demand, Throwable::printStackTrace));
            }

I.e. if a user is playing the game in several browser tabs, I would like to
send the up-to-date list of games (gamesJsonStr) to the user's every
connected Websocket client (to his/here every browser tab).

However I get the exception:

2024-11-08 11:13:58.492:WARN :oejwc.WebSocketCoreSession$Flusher:qtp183284570-53: Exception while notifying success of callback Callback@2792df7d{BLOCKING, org.eclipse.jetty.websocket.common.WebSocketSession$$Lambda/0x000001da8131e8e0@68c25a59,org.eclipse.jetty.websocket.common.WebSocketSession$$Lambda/0x000001da8131eb00@25efa676}
java.nio.channels.ReadPendingException
        at org.eclipse.jetty.websocket.core.util.DemandingFlusher.demand(DemandingFlusher.java:82)
        at org.eclipse.jetty.websocket.core.internal.PerMessageDeflateExtension.demand(PerMessageDeflateExtension.java:262)
        at org.eclipse.jetty.websocket.core.ExtensionStack.demand(ExtensionStack.java:276)
        at org.eclipse.jetty.websocket.core.WebSocketCoreSession.demand(WebSocketCoreSession.java:416)
        at org.eclipse.jetty.websocket.common.WebSocketSession.demand(WebSocketSession.java:58)
        at org.eclipse.jetty.websocket.api.Callback$2.succeed(Callback.java:49)
        at org.eclipse.jetty.util.Callback$3.succeeded(Callback.java:170)
        at org.eclipse.jetty.websocket.core.util.TransformingFlusher.notifyCallbackSuccess(TransformingFlusher.java:195)
        at org.eclipse.jetty.websocket.core.util.TransformingFlusher$Flusher.process(TransformingFlusher.java:152)
        at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:262)
        at org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:243)
        at org.eclipse.jetty.websocket.core.util.TransformingFlusher.sendFrame(TransformingFlusher.java:78)
        at org.eclipse.jetty.websocket.core.WebSocketCoreSession.sendFrame(WebSocketCoreSession.java:508)
        at org.eclipse.jetty.websocket.common.WebSocketSession.sendText(WebSocketSession.java:108)
        at de.afarber.WordsListener.handleLogin(WordsListener.java:210)
        at de.afarber.WordsListener.onWebSocketText(WordsListener.java:105)
        at org.eclipse.jetty.websocket.core.messages.StringMessageSink.accept(StringMessageSink.java:67)
        at org.eclipse.jetty.websocket.common.JettyWebSocketFrameHandler.acceptFrame(JettyWebSocketFrameHandler.java:397)
        at org.eclipse.jetty.websocket.common.JettyWebSocketFrameHandler.onTextFrame(JettyWebSocketFrameHandler.java:368)
        at org.eclipse.jetty.websocket.common.JettyWebSocketFrameHandler.onFrame(JettyWebSocketFrameHandler.java:216)
        at org.eclipse.jetty.websocket.core.WebSocketCoreSession$IncomingAdaptor.lambda$onFrame$0(WebSocketCoreSession.java:647)
        at org.eclipse.jetty.server.handler.ContextHandler$ScopedContext.run(ContextHandler.java:1513)
        at org.eclipse.jetty.server.handler.ContextHandler$ScopedContext.run(ContextHandler.java:1500)
        at org.eclipse.jetty.websocket.core.server.internal.AbstractHandshaker$1.handle(AbstractHandshaker.java:179)
        at org.eclipse.jetty.websocket.core.WebSocketCoreSession$IncomingAdaptor.onFrame(WebSocketCoreSession.java:647)
        at org.eclipse.jetty.websocket.core.AbstractExtension.nextIncomingFrame(AbstractExtension.java:145)
        at org.eclipse.jetty.websocket.core.internal.PerMessageDeflateExtension.nextIncomingFrame(PerMessageDeflateExtension.java:239)
        at org.eclipse.jetty.websocket.core.util.DemandingFlusher.emitFrame(DemandingFlusher.java:143)
        at org.eclipse.jetty.websocket.core.internal.PerMessageDeflateExtension$IncomingFlusher.inflate(PerMessageDeflateExtension.java:492)
        at org.eclipse.jetty.websocket.core.internal.PerMessageDeflateExtension$IncomingFlusher.handle(PerMessageDeflateExtension.java:417)
        at org.eclipse.jetty.websocket.core.util.DemandingFlusher.process(DemandingFlusher.java:167)
        at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:262)
        at org.eclipse.jetty.util.IteratingCallback.succeeded(IteratingCallback.java:403)
        at org.eclipse.jetty.websocket.core.util.DemandingFlusher.onFrame(DemandingFlusher.java:105)
        at org.eclipse.jetty.websocket.core.internal.PerMessageDeflateExtension.onFrame(PerMessageDeflateExtension.java:96)
        at org.eclipse.jetty.websocket.core.ExtensionStack.onFrame(ExtensionStack.java:113)
        at org.eclipse.jetty.websocket.core.WebSocketCoreSession.onFrame(WebSocketCoreSession.java:463)
        at org.eclipse.jetty.websocket.core.WebSocketConnection.onFrame(WebSocketConnection.java:254)
        at org.eclipse.jetty.websocket.core.WebSocketConnection.fillAndParse(WebSocketConnection.java:447)
        at org.eclipse.jetty.websocket.core.WebSocketConnection.onFillable(WebSocketConnection.java:332)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:322)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:99)
        at org.eclipse.jetty.io.SelectableChannelEndPoint$1.run(SelectableChannelEndPoint.java:53)
        at org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.runTask(AdaptiveExecutionStrategy.java:478)
        at org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.consumeTask(AdaptiveExecutionStrategy.java:441)
        at org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.tryProduce(AdaptiveExecutionStrategy.java:293)
        at org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.run(AdaptiveExecutionStrategy.java:201)
        at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:311)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:979)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.doRunJob(QueuedThreadPool.java:1209)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1164)
        at java.base/java.lang.Thread.run(Thread.java:1583)

I think Jetty wanted to call the callback lambda to tell
that everything is good ("... notifyCallbackSuccess ..."), but fails.

Why does it fail, could you please give me some hints?

Is it because I loop and the "Session session" variable goes out of scope?
Or something else? And how to call the "demand()" then?

Best regards
Alex
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users

Back to the top