Skip to content

bug: wslink keepalive timeout should close original connection #6562

Done

Description

@TomKaltz

Provide environment information

System:
    OS: macOS 15.0.1
    CPU: (8) arm64 Apple M3
    Memory: 128.59 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.12.0 - ~/.nvm/versions/node/v22.12.0/bin/node
    npm: 11.1.0 - ~/Documents/Projects/2024/n-moat/node_modules/.bin/npm
    bun: 1.1.43 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 133.1.75.180
    Chrome: 133.0.6943.127
    Safari: 18.0.1

Describe the bug

As of the merging of #6547 by @hmatthieu, a keepalive timeout does not close the original websocket connection. Therefore user code is not immediately notified of the timeout via the onClose callback. Later when the server becomes available again, the onClose callback is then called and a new connection is immediately re-established.

There are cases where the OS/browser will not detect a severed connection immediately. The keepalive timeout should be used to detect a "server not reachable" scenario. The connection should be closed immediately upon the elapse of PongTimeoutMs so that user code can be notified of the disconnect or loss of server/network via onClose callback.

To reproduce

    wsClient = createWSClient({
      url: `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.host}/api/trpc-ws`,
      retryDelayMs: (retryCount) =>
        Math.max(200 * Math.pow(2, retryCount), 2000),
      WebSocket: WebSocket,
      async connectionParams() {
        const user = await useCurrentUser();
        const token = await user.value?.getIdToken(true);
        return token ? { Authorization: `Bearer ${token}` } : {};
      },
      keepAlive: {
        enabled: true,
        intervalMs: 1000,
        pongTimeoutMs: 4000,
      },
      onClose(cause) {
        console.log("wsClient onClose callback:", cause);
      },
      onOpen() {
        console.log("wsClient onOpen callback");
      },
      onError(error) {
        console.error("wsClient onError callback:", error);
      },
    });

Disconnect the network using Chrome DevTools....the onClose callback is NOT called even after the configured PongTimeoutMs has elapsed. The onClose callback is not called until the network is re-enabled and just before the new connection is created.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. changed the title [-]bug: keepalive timeout should close original connection[/-] [+]bug: wslink keepalive timeout should close original connection[/+] on Feb 25, 2025
  2. self-assigned this
    on Feb 25, 2025
  3. KATT commented on Feb 28, 2025

    @KATT
    Member

    cc @hmatthieu let me know if you need a hand

  4. hmatthieu commented on Mar 1, 2025

    @hmatthieu
    Contributor

    I believe this isn't actually a bug. Let me explain (open to discussion):
    When the network goes offline, browsers keep websockets in an open state. When connectivity returns, all messages are sent normally. If you need to monitor network status, you shouldn't rely on the websocket but rather use navigator.onLine or the offline/online window events. I think wsLink should follow standard websocket behavior.

    If we really want to close connections when offline, the problem is that we can't. When the network is offline, calling ws.close has no effect (until the network is back online). This means we would need to treat the connection as closed until we're back online, then properly close the websocket. This is implementable, but I feel the behavior is somewhat counterintuitive.

    I suggest we maintain the current behavior (where onClose is not called) since the websocket isn't actually closed during network disruptions.
    Note that even if we are going this path, there is still a bug related to

    Later when the server becomes available again, the onClose callback is then called and a new connection is immediately re-established.

    -> We should not close/open the connection when we are back online

    WDYT ? @TomKaltz @KATT

    PS: It might be interesting to check how ws (for Node.js) handles network disruptions, though servers losing their network connection isn't really a common scenario, is it?

  5. TomKaltz commented on Mar 4, 2025

    @TomKaltz
    Author

    What then is the intention of the keepalive mechanism and it's timeout?

  6. hmatthieu commented on Mar 5, 2025

    @hmatthieu
    Contributor

    When the server stop responding for any reason (Server is down for example)

  7. KATT commented on Mar 15, 2025

    @KATT
    Member

    I definitely think we should stop and restart the connection on the client if pongTimeoutMs is exceeded

    /**
    * Send a ping message every this many milliseconds
    * @default 5_000
    */
    intervalMs?: number;
    /**
    * Close the WebSocket after this many milliseconds if the server does not respond
    * @default 1_000
    */
    pongTimeoutMs?: number;

    Otherwise this is very misleading. We handled it before but maybe we didn't have a good test for it

  8. KATT commented on Mar 15, 2025

    @KATT
    Member

    Did some digging, we do close and restart the connection when it times out as we should, but there might not be an onClose-callback...

  9. KATT commented on Mar 15, 2025

    @KATT
    Member

    In my test the onClose is called but it might be that our tests aren't testing actual offline-behavior

  10. KATT commented on Mar 15, 2025

    @KATT
    Member

    I think we should call the same callback whether it comes from a websocket.addEventListener('close', ...) or from a websocket.close() call

  11. hmatthieu commented on Mar 26, 2025

    @hmatthieu
    Contributor

    Btw, still in discussion with @KATT

  12. Nick-Lucas commented on Oct 23, 2025

    @Nick-Lucas
    Contributor

    Closing as stale but feel free to re-open if this is still current

  13. github-actions commented on Oct 24, 2025

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  14. locked as resolved and limited conversation to collaborators on Oct 24, 2025
  15. reopened this on Nov 4, 2026
  16. added a commit that references this issue on Nov 24, 2026
    f9f9084
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions