Plug one more memory leak in the OCPP stack in case of WS closed.
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 28 Sep 2021 20:56:01 +0000 (22:56 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 28 Sep 2021 20:56:01 +0000 (22:56 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/OCPPRequestService.ts

index 18c73918fb9111851dd1fa43174750fe5265a475..9f00f9fd9e18a79b1a8a8012c52fac16ae0536d2 100644 (file)
@@ -26,7 +26,7 @@ import tar from 'tar';
 
 export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
   public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record<string, unknown>): Promise<void> {
-    let response;
+    let response: Record<string, unknown>;
     const methodName = `handleRequest${commandName}`;
     // Call
     if (typeof this[methodName] === 'function') {
index ffe9f92080f7537fc5dcec884caaf4acc8b1567f..1bc8e70cd275cab6decf2049d12e6bfc9316eaef 100644 (file)
@@ -37,21 +37,29 @@ export default abstract class OCPPRequestService {
       if (this.chargingStation.isWebSocketConnectionOpened()) {
         // Yes: Send Message
         const beginId = PerformanceStatistics.beginMeasure(commandName);
+        // FIXME: Handle sending error
         this.chargingStation.wsConnection.send(messageToSend);
         PerformanceStatistics.endMeasure(commandName, beginId);
       } else if (!skipBufferingOnError) {
         // Buffer it
         this.chargingStation.addToMessageQueue(messageToSend);
-        // Reject it but keep the request in the cache
-        return reject(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams?.details ?? {}));
+        const ocppError = new OCPPError(ErrorType.GENERIC_ERROR, `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {});
+        if (messageType === MessageType.CALL_MESSAGE) {
+          // Reject it but keep the request in the cache
+          return reject(ocppError);
+        }
+        return rejectCallback(ocppError);
+      } else {
+        // Reject it
+        return rejectCallback(new OCPPError(ErrorType.GENERIC_ERROR, `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {}));
       }
       // Response?
       if (messageType !== MessageType.CALL_MESSAGE) {
         // Yes: send Ok
-        resolve(commandName);
+        resolve({ commandName, commandParams });
       } else {
         // Send timeout
-        setTimeout(() => rejectCallback(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_SOCKET_TIMEOUT);
+        setTimeout(() => rejectCallback(new OCPPError(ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_SOCKET_TIMEOUT);
       }
 
       /**
@@ -67,7 +75,7 @@ export default abstract class OCPPRequestService {
         // Send the response
         await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload);
         self.chargingStation.requests.delete(messageId);
-        resolve(payload);
+        resolve({ commandName, payload, requestPayload });
       }
 
       /**
@@ -91,7 +99,7 @@ export default abstract class OCPPRequestService {
     throw error;
   }
 
-  private buildMessageToSend(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand,
+  private buildMessageToSend(messageId: string, commandParams: Record<string, unknown>, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand,
       responseCallback: (payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>) => Promise<void>, rejectCallback: (error: OCPPError) => void): string {
     let messageToSend: string;
     // Type of message
@@ -99,7 +107,7 @@ export default abstract class OCPPRequestService {
       // Request
       case MessageType.CALL_MESSAGE:
         // Build request
-        this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandName, commandParams as Record<string, unknown>]);
+        this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandName, commandParams]);
         messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]);
         break;
       // Response