Repository navigation
Description
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
[-]bug: keepalive timeout should close original connection[/-][+]bug: wslink keepalive timeout should close original connection[/+]KATT commentedon Feb 28, 2025
cc @hmatthieu let me know if you need a hand
hmatthieu commentedon Mar 1, 2025
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
-> 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?
TomKaltz commentedon Mar 4, 2025
What then is the intention of the keepalive mechanism and it's timeout?
hmatthieu commentedon Mar 5, 2025
When the server stop responding for any reason (Server is down for example)
KATT commentedon Mar 15, 2025
I definitely think we should stop and restart the connection on the client if
pongTimeoutMsis exceededtrpc/packages/client/src/links/wsLink/wsClient/options.ts
Lines 48 to 57 in 83962fc
Otherwise this is very misleading. We handled it before but maybe we didn't have a good test for it
KATT commentedon Mar 15, 2025
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...KATT commentedon Mar 15, 2025
In my test the
onCloseis called but it might be that our tests aren't testing actual offline-behaviorKATT commentedon Mar 15, 2025
I think we should call the same callback whether it comes from a
websocket.addEventListener('close', ...)or from awebsocket.close()callhmatthieu commentedon Mar 26, 2025
Btw, still in discussion with @KATT
Nick-Lucas commentedon Oct 23, 2025
Closing as stale but feel free to re-open if this is still current
github-actions commentedon Oct 24, 2025
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.
fix(client): reconnect right away when a wsLink keepAlive pong times …