From 2bba38f6fe58d6cba99b4b71202a0e943c161675 Mon Sep 17 00:00:00 2001 From: zivCheng Date: Wed, 17 Apr 2024 14:06:14 +0800 Subject: [PATCH] Fix possible memory leak --- src/impl/Protocol.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/impl/Protocol.ts b/src/impl/Protocol.ts index c3b8050..6a2b248 100644 --- a/src/impl/Protocol.ts +++ b/src/impl/Protocol.ts @@ -63,16 +63,19 @@ export class Protocol { messageId, request, payload]); - this.socket.send(result); + const timeout =setTimeout(() => { + // timeout error + this.onCallError(messageId, ERROR_INTERNALERROR, 'No response from the client', {}); + }, 10000); this.pendingCalls[messageId] = { resolve, reject, + timeout }; + this.socket.send(result); + - setTimeout(() => { - // timeout error - this.onCallError(messageId, ERROR_INTERNALERROR, 'No response from the client', {}); - }, 10000); + } catch (e) { console.error(e); reject(e); @@ -100,20 +103,22 @@ export class Protocol { errorDetails: any, ) { if (this.pendingCalls[messageId]) { - const { reject } = this.pendingCalls[messageId]; + const { reject, timeout } = this.pendingCalls[messageId]; if (reject) { reject(new OcppError(errorCode, errorDescription, errorDetails)); } + clearTimeout(timeout); delete this.pendingCalls[messageId]; } } private onCallResult(messageId: string, payload: any) { if (this.pendingCalls[messageId]) { - const { resolve } = this.pendingCalls[messageId]; + const { resolve, timeout } = this.pendingCalls[messageId]; if (resolve) { resolve(payload); } + clearTimeout(timeout); delete this.pendingCalls[messageId]; } } @@ -126,15 +131,17 @@ export class Protocol { const validator = new SchemaValidator(schema); validator.validate(payload); const response = await new Promise((resolve, reject) => { - setTimeout(() => { + const timeout = setTimeout(() => { // timeout error reject(new OcppError(ERROR_INTERNALERROR, 'No response from the handler')); }, 10000); const hasListener = this.eventEmitter.emit(request, payload, (result: any) => { + clearTimeout(timeout) resolve(result); }); if (!hasListener) { + clearTimeout(timeout) reject(new OcppError(ERROR_NOTIMPLEMENTED, `Listener for action "${request}" not set`)); } });