Fix IllegalArgumentException that prevents STOMP DISCONNECT from reaching the client #30120
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.
In some application setups, the WebSocket server does not transmit the disconnect message to the client, so that the client has no idea that the established connection has been terminated.
This issue arises when the application uses SimpleBrokerMessageHandler and the error handler is set to the instance of
StompSubProtocolErrorHandler or an extended class that does not override the handleErrorMessageToClient method.
The commit fixes disconnect message population so that
java.lang.IllegalArgumentException: No StompHeaderAccessorexception is not thrown in the handleErrorMessageToClient method in StompSubProtocolErrorHandler class.A reproducible example for the case is available here:
The issue arises when the client fails to send heartbeats for specific period, causing the server to terminate the session by invoking the
handleDisconnectmethod in theSimpleBrokerMessageHandler.HeartbeatTaskclass. This issue was discovered in spring-websocket-5.3.13 but still persists in the latest code base.If the error handler is not set, then there is no problem as the message is delivered to the client in this scenario. A reproducible example that illustrates this is the
DisconnectWithoutErrorHandler.testfound at https://github.com/alexjansons/spring-sandbox/blob/main/web-socket/disconnect-error/src/test/java/com/example/websocket/disconnecterror/ApplicationTests.java#L97.However if the error handler is set to
StompSubProtocolErrorHandlerclass instance or the instance of extended class given that the latter does not implement it's ownhandleErrorMessageToClientmethod, the disconnect message transmission process faces theIllegalArgumentExceptionexception in the aforementioned method. The exception is caught and then silently ignored inSubProtocolWebSocketHandler.handleMessagemethod, which results in no message being sent to the client. This case is covered inDisconnectWithErrorHandler.testat https://github.com/alexjansons/spring-sandbox/blob/main/web-socket/disconnect-error/src/test/java/com/example/websocket/disconnecterror/ApplicationTests.java#L83.A workaround for this issue is to extend the
StompSubProtocolErrorHandlerclass for the error handler and re-implement the methodhandleErrorMessageToClientso that it does not generate an exception. An example of this workaround can be found at https://github.com/alexjansons/spring-sandbox/blob/main/web-socket/disconnect-error/src/main/java/com/example/websocket/disconnecterror/Application.java#L24.