Handle closed session when sending MVC WebSocket response - #1517
Open
Meemaw wants to merge 1 commit into
Open
Conversation
Prior to this commit, SendMessageSubscriber.hookOnNext caught only
IOException from WebSocketSession.sendMessage. When the session is closed
while a subscription response is still in flight, the servlet container
(e.g. Tomcat) instead raises IllegalStateException ("Message will not be
sent because the WebSocket session has been closed"). That exception
escaped hookOnNext into reactor's error handling and, depending on the
reactor version, surfaced as an uncaught error on the serial send
scheduler thread instead of cancelling the response and closing the
session.
This commit also catches IllegalStateException in hookOnNext, handling
it the same as IOException: cancel the response publisher and close the
session with the error.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Meemaw <ematej.snuderl@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In the WebMVC
GraphQlWebSocketHandler,SendMessageSubscriber.hookOnNextcatches onlyIOExceptionfromWebSocketSession.sendMessage:When the WebSocket session is closed while a subscription response is still in flight, the servlet container does not raise
IOException— Tomcat raisesIllegalStateException:That
IllegalStateExceptionis not caught by thecatch (IOException ...)clause, so instead of cancelling the response publisher and closing the session, it escapeshookOnNextand propagates on the serial send-scheduler thread (publishOn) as an uncaught error.Production impact
We are hitting this in production. On our WebSocket subscription gateway (WebMVC + Tomcat), session close during an in-flight subscription is entirely routine — clients navigate away, connections drop — and every such race produces this stack trace. It is one of our highest-volume framework errors on that service (on the order of ~100k occurrences/week, in steady state), pure noise that drowns out real errors and, depending on the Reactor version, is logged as an uncaught error on the scheduler thread rather than being handled cleanly.
Fix
Handle
IllegalStateExceptionthe same way asIOExceptioninhookOnNext— cancel the response publisher and close the session with the error:Test
Adds
sessionClosedShouldCancelPublisherand aClosedSessiontest fixture that lets the initialconnection_ackthrough and then raisesIllegalStateExceptionon subsequent sends (simulating a session closed while a subscription response is in flight).Note on observability: the reactor-core version used by the test suite wraps
hookOnNextinBaseSubscriber.onNextand routes throwables toonOperatorError, which masks the escape at the handler-behaviour level. The regression test therefore asserts that theIllegalStateExceptionis handled inside the subscriber and never reaches Reactor'sHooks.onOperatorError— this fails without the fix and passes with it.