From 1abe15566325f1ac94f5fc29aa7abf3f4b2498a1 Mon Sep 17 00:00:00 2001 From: Aleksandrs Jansons <43913143+alexjansons@users.noreply.github.com> Date: Sun, 5 Mar 2023 08:39:12 +0200 Subject: [PATCH] Ensure WebSocket disconnect msg reaches the client 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 StompHeaderAccessor` exception is not thrown in the handleErrorMessageToClient method in StompSubProtocolErrorHandler class. See gh-30120 --- .../web/socket/messaging/StompSubProtocolHandler.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java index b06bea9b5b..5be8839e0a 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java @@ -465,7 +465,9 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE } if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) { - Message errorMessage = getErrorHandler().handleErrorMessageToClient((Message) message); + Message enrichedMessage = + MessageBuilder.createMessage((byte[]) message.getPayload(), accessor.getMessageHeaders()); + Message errorMessage = getErrorHandler().handleErrorMessageToClient(enrichedMessage); if (errorMessage != null) { accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class); Assert.state(accessor != null, "No StompHeaderAccessor");