Skip to content

WebSocket: introduce a reliable way to detect "WebSocket is closed" error #5221

Closed

Description

@mkouba

Describe the feature

Currently, when the connection is closed but the client still attempts to write a message the API returns a failed Future where the cause is represented as a NoStackTraceThrowable with message WebSocket is closed. Consequently, it's not possible to detect this kind of error in a reliable way.

It might be useful to introduce either (A) a specific exception like WebSocketClosedException or (B) any other means, such as an error code (enum, constant, etc.) stored in the throwable/failed future.

Use cases

In Quarkus, we would like to be able to catch this kind of error and react appropriately.

Activity

  1. mkouba commented on Jun 4, 2024

    @mkouba
    Author
  2. geoand commented on Jun 4, 2024

    @geoand
    Contributor

    +1

  3. vietj commented on Jun 5, 2024

    @vietj
    Member

    @mkouba I think you can set a close handler on the WebSocket and be aware of when a WebSocket is closed and cannot send message anymore

  4. mkouba commented on Jun 5, 2024

    @mkouba
    Author

    @mkouba I think you can set a close handler on the WebSocket and be aware of when a WebSocket is closed and cannot send message anymore

    Yes, that's possible and we use it but for a different purpose. In this particular case, we need to react immediately, e.g. when WebSocket#writeTextMessage(String, Handler<AsyncResult<Void>>) is called.

  5. vietj commented on Jun 5, 2024

    @vietj
    Member

    we totally can do that, I was just pointing out that the common design we advocate is that you set a boolean closed that is set to true when the close handler is called and it can be probed

  6. mkouba commented on Jun 6, 2024

    @mkouba
    Author

    we totally can do that, I was just pointing out that the common design we advocate is that you set a boolean closed that is set to true when the close handler is called and it can be probed

    I get it and as I already mentioned we use this pattern but it does not really help if you need to analyze a failure produced by WebSocket#writeTextMessage() and friends. Currently, we have to test the exception message like this: https://github.com/quarkusio/quarkus/blob/3.11.1/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/Endpoints.java#L250-L262. Which is far from being ideal. As you can see, we test the "closed boolean" first in the connection.isClosed() method but then we still need to analyze the exception in order to react appropriately.

  7. added this to the 5.2.2 milestone on Oct 20, 2026
  8. added a commit that references this issue on Oct 20, 2026
    66a1004
  9. added a commit that references this issue on Oct 20, 2026
    096ea79
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions