I use TextWebSocketHandler to manage WebSocket sessions, but when the connection is obviously closed, the session.isOpen() method still returns true.

if (session.isOpen()) {
    System.out.println("Session is not closed. Session ID: " + session.getId());
    try {
        session.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (session.isOpen()) {
        System.out.println("Session is still not closed. Session ID: " + session.getId());
    }
}

Output:

Session is not closed. Session ID: 2e4f942c-e2a7-af86-edcf-fe70cfe4cfe7
Session is still not closed. Session ID: 2e4f942c-e2a7-af86-edcf-fe70cfe4cfe7

At this point, the frontend has already displayed Connection closed and afterConnectionClosed in TextWebSocketHandler has been triggered. When attempting to send a message in this situation, it will throw an error: java.lang.IllegalStateException: Message will not be sent because the WebSocket session has been closed.

This situation only exists in version 3.1.2. I tested several previous versions, and this issue did not appear.

Comment From: wilkinsona

I assume you're referring to org.springframework.web.socket.WebSocketSession. It's part of Spring Framework which is managed as a separate project. Please report the problem to them.