question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket connection retry handling is severely broken in Firefox

See original GitHub issue

Setup:

OS: Mac Monterey v12.4 Firefox: 106.0.5 (64-bit) NATS Server: v2.9.6 nats.ws: v1.10.0,

# server.conf

websocket {
    port: 9222,
    no_tls: true
}
// Web app

conn = await connect({
	servers: config.URL,
	maxReconnectAttempts: -1, // Retry forever
	waitOnFirstConnect: true
});

Steps to reproduce:

  1. Start NATS server with WebSockets enabled.
  2. Open your web app in Firefox. The app connects to the NATS server’s WebSocket endpoint.
  3. Everything is fine.
  4. Stop the NATS server for 3 minutes or so.
  5. Start the NATS server again.
  6. After a while, the web app connects to NATS server by establishing several WebSocket connections!
  7. There should be only one connection, instead of 7 or so.

This happens only in Firefox. Chrome and Safari work without problems. I tried both localhost and non-localhost setups.

This is most probably related to this Firefox behavior (see the accepted answer):

Found it. This is intentional behavior to comply with RFC 6455. Per this patch, it uses an exponential backoff up to 60 seconds max. Unfortunately I think this means auto-reconnecting to a websocket endpoint in Firefox is broken.

I took some screenshots from Chrome (left) and Firefox (right) where this exponential backoff is clearly visible:

screenshot_01

Here you see how multiple connections (should be only one) are established after restarting the NATS server. You can see the connections in NATS server’s http://localhost:8222/connz endpoint too, or by running nats server report connections from command line.

screenshot_02

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
aricartcommented, Dec 16, 2022

Yes I tried a number of things, and even pre-cancelling (which actually happens now) is not honored. Annotating that the WebSocket was cancelled also has no effect (tried to dispose it on create). The current workaround is what you have. While the Firefox folks are doing the right thing by the RFC, the bigger issue is that the client is not able to understand that this is the case or the throttling that is being imposed. One thought I had was to make a version of the reconnect jitter (which takes a function) that implements a similar exponential backoff. This is on my list to do.

1reaction
aricartcommented, Dec 2, 2022

@heikkilamarko you are absolutely right - if you wait a very long amount of time, the behavior is completely different.

image

The crazy thing is that, my handler can detect that it was closed - but it’s like the API calls have no effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · nats-io/nats.ws - GitHub
WebSocket connection retry handling is severely broken in Firefox ... Angular 13.1 Nats.ws client dose't send CONNECT message after receiving INFO message.
Read more >
WebSockets in Chrome and Firefox Disconnecting After One ...
It seems like if sockets are disconnected by the server after one minute of inactivity that would apply to IE and Edge just...
Read more >
Delay websocket reconnection after abnormal termination
What's important here is that there is a backoff. In a severe problem (what we're really worried about preventing) it will backoff to...
Read more >
wss websocket stopped working | Firefox Support Forum
Today firsfox just stopped connecting to a wss websocket server. It has always worked fine before today and still works in Chrome.
Read more >
1673340 - WebSocket duplicate packets sent - Bugzilla@Mozilla
mozregression seems to be broken. Both nightly build 82.0a1 and 83.0a1 do not suffer from this bug and our test ran smoothly (no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found