From abe9aa1fdc70942bcf36bf544001ef4189b5c06a Mon Sep 17 00:00:00 2001 From: Mononaut Date: Mon, 18 Nov 2024 20:40:08 +0000 Subject: [PATCH 1/3] fix acceleration websocket timeout loop --- backend/src/api/services/acceleration.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/src/api/services/acceleration.ts b/backend/src/api/services/acceleration.ts index e18bcf464..e4edab791 100644 --- a/backend/src/api/services/acceleration.ts +++ b/backend/src/api/services/acceleration.ts @@ -247,6 +247,7 @@ class AccelerationApi { if (!this.ws) { this.ws = new WebSocket(this.websocketPath); this.websocketConnected = true; + this.lastPing = 0; this.ws.on('open', () => { logger.info(`Acceleration websocket opened to ${this.websocketPath}`); @@ -286,12 +287,13 @@ class AccelerationApi { this.lastPong = Date.now(); }); } else { - if (this.lastPing > this.lastPong && Date.now() - this.lastPing > 10000) { + if (this.lastPing && this.lastPing > this.lastPong && (Date.now() - this.lastPing > 10000)) { logger.warn('No pong received within 10 seconds, terminating connection'); this.ws.terminate(); this.ws = null; this.websocketConnected = false; - } else if (Date.now() - this.lastPing > 30000) { + this.lastPing = 0; + } else if (!this.lastPing || (Date.now() - this.lastPing > 30000)) { logger.debug('sending ping to acceleration websocket server'); this.ws.ping(); this.lastPing = Date.now(); From 9a81db8e6c25c3707294cc01549287ef0d692375 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 19 Nov 2024 23:00:28 +0000 Subject: [PATCH 2/3] fix acceleration websocket ping error --- backend/src/api/services/acceleration.ts | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/backend/src/api/services/acceleration.ts b/backend/src/api/services/acceleration.ts index e4edab791..43624e19b 100644 --- a/backend/src/api/services/acceleration.ts +++ b/backend/src/api/services/acceleration.ts @@ -246,11 +246,11 @@ class AccelerationApi { this.startedWebsocketLoop = true; if (!this.ws) { this.ws = new WebSocket(this.websocketPath); - this.websocketConnected = true; this.lastPing = 0; this.ws.on('open', () => { logger.info(`Acceleration websocket opened to ${this.websocketPath}`); + this.websocketConnected = true; this.ws?.send(JSON.stringify({ 'watch-accelerations': true })); @@ -286,17 +286,28 @@ class AccelerationApi { logger.debug('received pong from acceleration websocket server'); this.lastPong = Date.now(); }); - } else { + } else if (this.websocketConnected) { if (this.lastPing && this.lastPing > this.lastPong && (Date.now() - this.lastPing > 10000)) { logger.warn('No pong received within 10 seconds, terminating connection'); - this.ws.terminate(); - this.ws = null; - this.websocketConnected = false; - this.lastPing = 0; + try { + this.ws?.terminate(); + } catch (e) { + logger.warn('failed to terminate acceleration websocket connection: ' + (e instanceof Error ? e.message : e)); + } finally { + this.ws = null; + this.websocketConnected = false; + this.lastPing = 0; + } } else if (!this.lastPing || (Date.now() - this.lastPing > 30000)) { logger.debug('sending ping to acceleration websocket server'); - this.ws.ping(); - this.lastPing = Date.now(); + if (this.ws?.readyState === WebSocket.OPEN) { + try { + this.ws?.ping(); + this.lastPing = Date.now(); + } catch (e) { + logger.warn('failed to send ping to acceleration websocket server: ' + (e instanceof Error ? e.message : e)); + } + } } } await new Promise(resolve => setTimeout(resolve, 5000)); From ffa582558b426901e87e2ce5fdff3329345ab964 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 21 Nov 2024 21:54:19 +0000 Subject: [PATCH 3/3] more verbose accelerator websocket error logs --- backend/src/api/services/acceleration.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/src/api/services/acceleration.ts b/backend/src/api/services/acceleration.ts index 43624e19b..053da6e82 100644 --- a/backend/src/api/services/acceleration.ts +++ b/backend/src/api/services/acceleration.ts @@ -257,7 +257,11 @@ class AccelerationApi { }); this.ws.on('error', (error) => { - logger.err(`Acceleration websocket error on ${this.websocketPath}: ` + error); + let errMsg = `Acceleration websocket error on ${this.websocketPath}: ${error['code']}`; + if (error['errors']) { + errMsg += ' - ' + error['errors'].join(' - '); + } + logger.err(errMsg); this.ws = null; this.websocketConnected = false; });