From 9d7b5fa3591b5cbdfbfb44d7c2721b2050399664 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 19 Nov 2023 15:22:01 +0100 Subject: [PATCH] fix: fix OCPP message sending promise leak MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/OCPPRequestService.ts | 49 ++++++++++--------- ui/web/src/composables/UIClient.ts | 3 +- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 880cf078..01fd3d34 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -349,10 +349,10 @@ export abstract class OCPPRequestService { /** * Function that will receive the request's error response * - * @param error - + * @param ocppError - * @param requestStatistic - */ - const errorCallback = (error: OCPPError, requestStatistic = true): void => { + const errorCallback = (ocppError: OCPPError, requestStatistic = true): void => { if (requestStatistic === true && chargingStation.stationInfo?.enableStatistics === true) { chargingStation.performanceStatistics?.addRequestStatistic( commandName, @@ -364,10 +364,19 @@ export abstract class OCPPRequestService { messageType, )} command ${commandName} with PDU %j:`, messagePayload, - error, + ocppError, ); chargingStation.requests.delete(messageId); - reject(error); + reject(ocppError); + }; + + const rejectWithOcppError = (ocppError: OCPPError): void => { + // Reject response + if (messageType !== MessageType.CALL_MESSAGE) { + return reject(ocppError); + } + // Reject and remove request from the cache + return errorCallback(ocppError, false); }; if (chargingStation.stationInfo?.enableStatistics === true) { @@ -384,14 +393,13 @@ export abstract class OCPPRequestService { if (chargingStation.isWebSocketConnectionOpened() === true) { const beginId = PerformanceStatistics.beginMeasure(commandName); const sendTimeout = setTimeout(() => { - return errorCallback( + return rejectWithOcppError( new OCPPError( ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}'`, commandName, (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT, ), - false, ); }, OCPPConstants.OCPP_WEBSOCKET_TIMEOUT); chargingStation.wsConnection?.send(messageToSend, (error?: Error) => { @@ -411,12 +419,7 @@ export abstract class OCPPRequestService { // Reject and keep request in the cache return reject(ocppError); } - // Reject response - if (messageType !== MessageType.CALL_MESSAGE) { - return reject(ocppError); - } - // Reject and remove request from the cache - return errorCallback(ocppError, false); + return rejectWithOcppError(ocppError); } }); if (messageType === MessageType.CALL_MESSAGE) { @@ -436,6 +439,14 @@ export abstract class OCPPRequestService { ); PerformanceStatistics.endMeasure(commandName, beginId); } else { + const ocppError = new OCPPError( + ErrorType.GENERIC_ERROR, + `WebSocket closed for ${ + params?.skipBufferingOnError === false ? '' : 'non ' + }buffered message id '${messageId}' with content '${messageToSend}'`, + commandName, + (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT, + ); if (params?.skipBufferingOnError === false) { // Buffer chargingStation.bufferMessage(messageToSend); @@ -449,18 +460,10 @@ export abstract class OCPPRequestService { errorCallback, ); } + // Reject and keep request in the cache + return reject(ocppError); } - // Reject and keep request in the cache - return reject( - new OCPPError( - ErrorType.GENERIC_ERROR, - `WebSocket closed for ${ - params?.skipBufferingOnError === false ? '' : 'non ' - }buffered message id '${messageId}' with content '${messageToSend}'`, - commandName, - (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT, - ), - ); + return rejectWithOcppError(ocppError); } // Resolve response if (messageType !== MessageType.CALL_MESSAGE) { diff --git a/ui/web/src/composables/UIClient.ts b/ui/web/src/composables/UIClient.ts index 89db2302..b78d6a0b 100644 --- a/ui/web/src/composables/UIClient.ts +++ b/ui/web/src/composables/UIClient.ts @@ -144,9 +144,8 @@ export class UIClient { command: ProcedureName, data: RequestPayload, ): Promise { - let uuid: string; return new Promise((resolve, reject) => { - uuid = crypto.randomUUID(); + const uuid = crypto.randomUUID(); const msg = JSON.stringify([uuid, command, data]); if (this.ws.readyState !== WebSocket.OPEN) { -- 2.34.1