From 89cd2d5631e09555562a2a25d1c098f70406a469 Mon Sep 17 00:00:00 2001 From: "Marc J. Schmidt" Date: Mon, 29 Jan 2024 03:07:53 +0100 Subject: [PATCH] fix(core): clear timeouts correctly in ProcessLock makes sure no unnecessary pending timeout prevents node from exiting. --- packages/core/src/process-locker.ts | 41 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/packages/core/src/process-locker.ts b/packages/core/src/process-locker.ts index 07a8e770e..ab1b78a11 100644 --- a/packages/core/src/process-locker.ts +++ b/packages/core/src/process-locker.ts @@ -35,28 +35,27 @@ export class ProcessLock { } return new Promise((resolve, reject) => { - const ourTake = () => { - LOCKS[this.id].time = Date.now() / 1000; - - this.holding = true; - resolve(); - - if (ttl) { - this.ttlTimeout = setTimeout(() => { - this.unlock(); - }, ttl * 1000); - } - }; - - if (timeout > 0) { - setTimeout(() => { - if (LOCKS[this.id]) arrayRemoveItem(LOCKS[this.id].queue, ourTake); - //reject is never handled when resolve is called first - reject('Lock timed out ' + this.id); - }, timeout * 1000); - } - if (LOCKS[this.id]) { + let timeoutId: any; + const ourTake = () => { + LOCKS[this.id].time = Date.now() / 1000; + clearTimeout(timeoutId); + this.holding = true; + resolve(); + + if (ttl) { + this.ttlTimeout = setTimeout(() => { + this.unlock(); + }, ttl * 1000); + } + }; + if (timeout > 0) { + timeoutId = setTimeout(() => { + if (LOCKS[this.id]) arrayRemoveItem(LOCKS[this.id].queue, ourTake); + //reject is never handled when resolve is called first + reject('Lock timed out ' + this.id); + }, timeout * 1000); + } LOCKS[this.id].queue.push(ourTake); } else { LOCKS[this.id] = {