Skip to content

Make websocket disconnect codes explicit - #6678

Merged
SteffenDE merged 3 commits into
phoenixframework:mainfrom
mtrudel:fix-websocket-close-code
May 22, 2026
Merged

SteffenDE merged 3 commits into
phoenixframework:mainfrom
mtrudel:fix-websocket-close-code

Conversation

@mtrudel

@mtrudel mtrudel commented May 15, 2026 •

Copy link
Copy Markdown
Member

Credit to @dl-alexandre for this; they had worked it up on a bandit issue, but since their org is blocked from opening PRs on the phoenix project I'm doing it by proxy.

I've triaged this from Bandit/Cowboy's perspective and it's legit.

@SteffenDE

Copy link
Copy Markdown
Member

Can you fix the tests?

@mtrudel

mtrudel commented May 18, 2026

Copy link
Copy Markdown
Member Author

Tests updated!

Comment thread lib/phoenix/socket.ex Resolved
Comment thread test/phoenix/socket/socket_test.exs Resolved
Co-authored-by: Steffen Deusch <[email protected]>
@SteffenDE
SteffenDE merged commit b6a4e31 into phoenixframework:main May 22, 2026
6 checks passed
@SteffenDE

Copy link
Copy Markdown
Member

Thank you @mtrudel!

@dl-alexandre was blocked because of numerous low-quality AI contributions. Looking at the bandit issue, I'm not even sure how much of the communication is done by Dalton Alexandre, the person. Quoting:

If a maintainer here has push access to dl-alexandre/phoenix's fork branch they could open the PR upstream from it; otherwise the diff is small enough to lift wholesale. Happy to answer questions on the analysis if useful.

SteffenDE added a commit that referenced this pull request Oct 19, 2026
Originally introduced in #2832, the close code 1000 was treated as a way to
make the documented "disconnect" broadcast not reconnect in the browser.

See also #3161 (comment)

The problem is that code 1000 is not always in our control. A middleware
might close a WebSocket connection with code 1000, leaving the browser
permanently disconnected. So using code 1000 is not a realiable meachnism
for this.

Additionally, we recently changed "disconnect" to send 1001 instead,
circumventing the whole thing: #6678

With all that in mind, I propose to remove special casing of 1000 completely.
I see two ways we can go forward:

1. Use a custom code, e.g. in 4000-4999 for "disconnect" and document
   that the socket will not reconnect. LiveView can still handle this
   code specially, but instead of reloading with jitter, we can immediately
   reload, as it is most commonly used when disconnecting a single user.
2. Keep "disconnect" reconnecting, as it does now and introduce a new
   "permanent_disconnect" or however it would be called that is documented
   to not reconnect.

Relates to: #2832
Relates to: #3327
Relates to: 470337d
Relates to: phoenixframework/phoenix_live_view@302833c
Relates to: phoenixframework/phoenix_live_view@953a862
Relates to: phoenixframework/phoenix_live_view#2421
Relates to: mtrudel/bandit#582
Relates to: #6678
SteffenDE added a commit that referenced this pull request Oct 20, 2026
This reverts commit b6a4e31.

Now that phoenix.js reconnects after any close by the server, the close code is
no longer needed to make the client reconnect after "disconnect".

Bandit now closes normally and waits for the client, so channels exit
with {:shutdown, :peer_closed} after a "disconnect" broadcast.
SteffenDE added a commit that referenced this pull request Oct 20, 2026
* Don't treat code 1000 as special

Originally introduced in #2832, the close code 1000 was treated as a way to
make the documented "disconnect" broadcast not reconnect in the browser.

See also #3161 (comment)

The problem is that code 1000 is not always in our control. A middleware
might close a WebSocket connection with code 1000, leaving the browser
permanently disconnected. So using code 1000 is not a realiable meachnism
for this.

Additionally, we recently changed "disconnect" to send 1001 instead,
circumventing the whole thing: #6678

With all that in mind, I propose to remove special casing of 1000 completely.
I see two ways we can go forward:

1. Use a custom code, e.g. in 4000-4999 for "disconnect" and document
   that the socket will not reconnect. LiveView can still handle this
   code specially, but instead of reloading with jitter, we can immediately
   reload, as it is most commonly used when disconnecting a single user.
2. Keep "disconnect" reconnecting, as it does now and introduce a new
   "permanent_disconnect" or however it would be called that is documented
   to not reconnect.

Relates to: #2832
Relates to: #3327
Relates to: 470337d
Relates to: phoenixframework/phoenix_live_view@302833c
Relates to: phoenixframework/phoenix_live_view@953a862
Relates to: phoenixframework/phoenix_live_view#2421
Relates to: mtrudel/bandit#582
Relates to: #6678

* update tests and comments

* Revert "Make websocket disconnect codes explicit (#6678)"

This reverts commit b6a4e31.

Now that phoenix.js reconnects after any close by the server, the close code is
no longer needed to make the client reconnect after "disconnect".

Bandit now closes normally and waits for the client, so channels exit
with {:shutdown, :peer_closed} after a "disconnect" broadcast.

* Accept local_closed when Bandit handles a disconnect broadcast
SteffenDE added a commit that referenced this pull request Oct 20, 2026
* Don't treat code 1000 as special

Originally introduced in #2832, the close code 1000 was treated as a way to
make the documented "disconnect" broadcast not reconnect in the browser.

See also #3161 (comment)

The problem is that code 1000 is not always in our control. A middleware
might close a WebSocket connection with code 1000, leaving the browser
permanently disconnected. So using code 1000 is not a realiable meachnism
for this.

Additionally, we recently changed "disconnect" to send 1001 instead,
circumventing the whole thing: #6678

With all that in mind, I propose to remove special casing of 1000 completely.
I see two ways we can go forward:

1. Use a custom code, e.g. in 4000-4999 for "disconnect" and document
   that the socket will not reconnect. LiveView can still handle this
   code specially, but instead of reloading with jitter, we can immediately
   reload, as it is most commonly used when disconnecting a single user.
2. Keep "disconnect" reconnecting, as it does now and introduce a new
   "permanent_disconnect" or however it would be called that is documented
   to not reconnect.

Relates to: #2832
Relates to: #3327
Relates to: 470337d
Relates to: phoenixframework/phoenix_live_view@302833c
Relates to: phoenixframework/phoenix_live_view@953a862
Relates to: phoenixframework/phoenix_live_view#2421
Relates to: mtrudel/bandit#582
Relates to: #6678

* update tests and comments

* Revert "Make websocket disconnect codes explicit (#6678)"

This reverts commit b6a4e31.

Now that phoenix.js reconnects after any close by the server, the close code is
no longer needed to make the client reconnect after "disconnect".

Bandit now closes normally and waits for the client, so channels exit
with {:shutdown, :peer_closed} after a "disconnect" broadcast.

* Accept local_closed when Bandit handles a disconnect broadcast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants