Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reconnecting websockets on heartbeat timeout #4921

Merged
merged 1 commit into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions assets/js/phoenix/socket.js
Expand Up @@ -342,7 +342,9 @@ export default class Socket {
if(this.pendingHeartbeatRef){
this.pendingHeartbeatRef = null
if(this.hasLogger()){ this.log("transport", "heartbeat timeout. Attempting to re-establish connection") }
this.abnormalClose("heartbeat timeout")
this.triggerChanError()
this.closeWasClean = false
this.teardown(() => this.reconnectTimer.scheduleTimeout(), WS_CLOSE_NORMAL, "heartbeat timeout")
}
}

Expand All @@ -365,6 +367,9 @@ export default class Socket {

this.waitForSocketClosed(() => {
if(this.conn){
this.conn.onopen = function (){ } // noop
this.conn.onerror = function (){ } // noop
this.conn.onmessage = function (){ } // noop
this.conn.onclose = function (){ } // noop
this.conn = null
}
Expand Down Expand Up @@ -521,11 +526,6 @@ export default class Socket {
this.heartbeatTimeoutTimer = setTimeout(() => this.heartbeatTimeout(), this.heartbeatIntervalMs)
}

abnormalClose(reason){
this.closeWasClean = false
if(this.isConnected()){ this.conn.close(WS_CLOSE_NORMAL, reason) }
}

flushSendBuffer(){
if(this.isConnected() && this.sendBuffer.length > 0){
this.sendBuffer.forEach(callback => callback())
Expand Down
12 changes: 0 additions & 12 deletions assets/test/socket_test.js
Expand Up @@ -714,18 +714,6 @@ describe("with transports", function(){
clock.restore()
done()
})

it("does not fail with abnormal close after explicit disconnect", function(done){
let clock = sinon.useFakeTimers()
const spy = sinon.spy(socket, "abnormalClose")
socket.onConnOpen()
socket.disconnect()
clock.tick(30000)
clock.tick(30000)
assert.ok(spy.notCalled)
clock.restore()
done()
})
})

describe("onConnError", function(){
Expand Down
16 changes: 9 additions & 7 deletions priv/static/phoenix.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions priv/static/phoenix.cjs.js.map

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions priv/static/phoenix.js
Expand Up @@ -962,7 +962,9 @@ var Phoenix = (() => {
if (this.hasLogger()) {
this.log("transport", "heartbeat timeout. Attempting to re-establish connection");
}
this.abnormalClose("heartbeat timeout");
this.triggerChanError();
this.closeWasClean = false;
this.teardown(() => this.reconnectTimer.scheduleTimeout(), WS_CLOSE_NORMAL, "heartbeat timeout");
}
}
resetHeartbeat() {
Expand All @@ -987,6 +989,12 @@ var Phoenix = (() => {
}
this.waitForSocketClosed(() => {
if (this.conn) {
this.conn.onopen = function() {
};
this.conn.onerror = function() {
};
this.conn.onmessage = function() {
};
this.conn.onclose = function() {
};
this.conn = null;
Expand Down Expand Up @@ -1102,12 +1110,6 @@ var Phoenix = (() => {
this.push({ topic: "phoenix", event: "heartbeat", payload: {}, ref: this.pendingHeartbeatRef });
this.heartbeatTimeoutTimer = setTimeout(() => this.heartbeatTimeout(), this.heartbeatIntervalMs);
}
abnormalClose(reason) {
this.closeWasClean = false;
if (this.isConnected()) {
this.conn.close(WS_CLOSE_NORMAL, reason);
}
}
flushSendBuffer() {
if (this.isConnected() && this.sendBuffer.length > 0) {
this.sendBuffer.forEach((callback) => callback());
Expand Down
2 changes: 1 addition & 1 deletion priv/static/phoenix.min.js

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions priv/static/phoenix.mjs
Expand Up @@ -933,7 +933,9 @@ var Socket = class {
if (this.hasLogger()) {
this.log("transport", "heartbeat timeout. Attempting to re-establish connection");
}
this.abnormalClose("heartbeat timeout");
this.triggerChanError();
this.closeWasClean = false;
this.teardown(() => this.reconnectTimer.scheduleTimeout(), WS_CLOSE_NORMAL, "heartbeat timeout");
}
}
resetHeartbeat() {
Expand All @@ -958,6 +960,12 @@ var Socket = class {
}
this.waitForSocketClosed(() => {
if (this.conn) {
this.conn.onopen = function() {
};
this.conn.onerror = function() {
};
this.conn.onmessage = function() {
};
this.conn.onclose = function() {
};
this.conn = null;
Expand Down Expand Up @@ -1073,12 +1081,6 @@ var Socket = class {
this.push({ topic: "phoenix", event: "heartbeat", payload: {}, ref: this.pendingHeartbeatRef });
this.heartbeatTimeoutTimer = setTimeout(() => this.heartbeatTimeout(), this.heartbeatIntervalMs);
}
abnormalClose(reason) {
this.closeWasClean = false;
if (this.isConnected()) {
this.conn.close(WS_CLOSE_NORMAL, reason);
}
}
flushSendBuffer() {
if (this.isConnected() && this.sendBuffer.length > 0) {
this.sendBuffer.forEach((callback) => callback());
Expand Down
4 changes: 2 additions & 2 deletions priv/static/phoenix.mjs.map

Large diffs are not rendered by default.