Repository navigation
Add conditional on onclose code in order to prevent retrying logic on… - #2832
chrismccord merged 5 commits into
Conversation
… regular socket closure
| this.triggerChanError() | ||
| clearInterval(this.heartbeatTimer) | ||
| this.reconnectTimer.scheduleTimeout() | ||
| if (!(event instanceof Event) || ((event instanceof Event) && event.code !== 1000)) { |
There was a problem hiding this comment.
do we need the instanceof checks? Can we simply say if event && event.code !== 1000
There was a problem hiding this comment.
@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!
|
@chrismccord the test is passing and I've refactored it as you've said. |
| const spy = sinon.spy(socket.reconnectTimer, "scheduleTimeout") | ||
|
|
||
| socket.onConnClose() | ||
| const event = new Event("onClose", { code: 1000 }) |
There was a problem hiding this comment.
this test is wrong, it is sending 1000 code that should not hit "scheduleTimeout", but the assert passes
There was a problem hiding this comment.
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)
})| this.triggerChanError() | ||
| clearInterval(this.heartbeatTimer) | ||
| this.reconnectTimer.scheduleTimeout() | ||
| if (event && event.code !== 1000) { |
There was a problem hiding this comment.
I would suggest using defined constant
if (event && event.code !== WS_CLOSE_NORMAL) {|
@prem-prakash thanks for your suggestions! I took them! |
|
@chrismccord I did a commit just for running again the tests, and now they are all passing! |
|
❤️❤️❤️🐥🔥 |
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
* 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
* 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

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:
so, what I understood from inspecting the
phoenix.jscode 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)