Skip to content

Add conditional on onclose code in order to prevent retrying logic on… - #2832

Merged
chrismccord merged 5 commits into
phoenixframework:masterfrom
nerimartinez:nm-distinguish-onclose-codes
Jun 12, 2018
Merged

chrismccord merged 5 commits into
phoenixframework:masterfrom
nerimartinez:nm-distinguish-onclose-codes

Conversation

@nerimartinez

Copy link
Copy Markdown
Contributor

We are working on a project in which we are using sockets.
But we had an issue when we tried to disconnect the socket (we are doing that when the user logs out).
On our logout method on the backend side we are doing Endpoint.broadcast("user_socket:#{user_id}", "disconnect", %{}) as is stated in the docs. But this is not correctly finishing the connection so it keeps trying to reconnect.
On the browser we can see that as:
image

so, what I understood from inspecting the phoenix.js code is that executes the same logic https://github.com/phoenixframework/phoenix/blob/master/assets/js/phoenix.js#L853 if we are closing the socket as I said, or if I shutdown the server (simulating an error on the socket).

and here ( https://github.com/phoenixframework/phoenix/blob/master/assets/js/phoenix.js#L857) it starts the timer which is in charge of trying to reconnect.

And that's not what we were after, we want to close the conection without retrying.

So after inspecting the code, I found that when we disconect the socket on the backend has a different onClose code than if we shutdown the server.

The onClose codes can be found here https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent

So for the socket disconect event on the backend, the code is 1000, that is “Normal closure; the connection successfully completed whatever purpose for which it was created.”. When I shutdown the server I got 1006 that is "Abnormal Closure"

that’s why I’ve added the if statement in order to prevent retrying when we do Endpoint.broadcast("user_socket:#{user_id}", "disconnect", %{}). So it’s gonna execute only if the closure was not normal (server shutdown, exceptions… whatever)

Comment thread assets/js/phoenix.js Outdated
this.triggerChanError()
clearInterval(this.heartbeatTimer)
this.reconnectTimer.scheduleTimeout()
if (!(event instanceof Event) || ((event instanceof Event) && event.code !== 1000)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the instanceof checks? Can we simply say if event && event.code !== 1000

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrismccord yeap, it should work, but I have to refactor the test since they are passing a string on event. this line https://github.com/phoenixframework/phoenix/blob/master/assets/test/socket_test.js#L645

I'm gonna do it as soon as I can.

thanks!

@nerimartinez

Copy link
Copy Markdown
Contributor Author

@chrismccord the test is passing and I've refactored it as you've said.

Comment thread assets/test/socket_test.js Outdated
const spy = sinon.spy(socket.reconnectTimer, "scheduleTimeout")

socket.onConnClose()
const event = new Event("onClose", { code: 1000 })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is wrong, it is sending 1000 code that should not hit "scheduleTimeout", but the assert passes

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest:

  it('does not schedules reconnectTimer timeout if normal close', () => {
    const spy = sinon.spy(socket.reconnectTimer, 'scheduleTimeout')

    const event = { code: 1000 }

    socket.onConnClose(event)

    assert.ok(spy.notCalled)
  })

  it('schedules reconnectTimer timeout if not normal close', () => {
    const spy = sinon.spy(socket.reconnectTimer, 'scheduleTimeout')

    const event = { code: 1001 }

    socket.onConnClose(event)

    assert.ok(spy.calledOnce)
  })

Comment thread assets/js/phoenix.js Outdated
this.triggerChanError()
clearInterval(this.heartbeatTimer)
this.reconnectTimer.scheduleTimeout()
if (event && event.code !== 1000) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using defined constant

if (event && event.code !== WS_CLOSE_NORMAL) {

@nerimartinez

Copy link
Copy Markdown
Contributor Author

@prem-prakash thanks for your suggestions! I took them!
@chrismccord I closed the pull request and opened it again in order to restart the build. There is one test that is failing that has nothing to do with this PR

Please see the image below
image

@nerimartinez

Copy link
Copy Markdown
Contributor Author

@chrismccord I did a commit just for running again the tests, and now they are all passing!

@chrismccord chrismccord self-assigned this May 8, 2018
@chrismccord
chrismccord merged commit ae7df95 into phoenixframework:master Jun 12, 2018
@chrismccord

Copy link
Copy Markdown
Member

❤️❤️❤️🐥🔥

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
* 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