From d18fc531483840fda8598f464b5293c624903068 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Sun, 29 Jun 2014 16:53:21 -0400 Subject: [PATCH] Avoid multiple invocations of afterConnectionClosed This change ensures the state of a SockJS session is set to CLOSED immediately after close is invoked. This avoids duplicate invocations of afterConnectionClosed in WebSocket transport. This is a backport of: https://github.com/spring-projects/spring-framework/commit/3af488a701834f61b89cd84040605a6252962e9c Issue: SPR-11884 --- .../transport/session/AbstractSockJsSession.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java index 39609c10b5e..efebbefd802 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java @@ -274,6 +274,7 @@ public abstract class AbstractSockJsSession implements SockJsSession { if (logger.isDebugEnabled()) { logger.debug("Closing " + this + ", " + status); } + this.state = State.CLOSED; try { if (isActive() && !CloseStatus.SESSION_NOT_RELIABLE.equals(status)) { try { @@ -289,7 +290,6 @@ public abstract class AbstractSockJsSession implements SockJsSession { disconnect(status); } finally { - this.state = State.CLOSED; try { this.handler.afterConnectionClosed(this, status); } @@ -339,11 +339,17 @@ public abstract class AbstractSockJsSession implements SockJsSession { catch (Throwable ex) { logWriteFrameFailure(ex); try { + // Force disconnect (so we won't try to send close frame) disconnect(CloseStatus.SERVER_ERROR); + } + catch (Throwable disconnectFailure) { + logger.error("Failure while closing " + this, disconnectFailure); + } + try { close(CloseStatus.SERVER_ERROR); } - catch (Throwable ex2) { - // ignore + catch (Throwable t) { + // Nothing of consequence, already forced disconnect } throw new SockJsTransportFailureException("Failed to write " + frame, this.getId(), ex); }