Skip to content

Commit

Permalink
fix(core): clear timeouts correctly in ProcessLock
Browse files Browse the repository at this point in the history
makes sure no unnecessary pending timeout prevents node from exiting.
  • Loading branch information
marcj committed Jan 29, 2024
1 parent e2676f6 commit 89cd2d5
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions packages/core/src/process-locker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,27 @@ export class ProcessLock {
}

return new Promise<void>((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] = {
Expand Down

0 comments on commit 89cd2d5

Please sign in to comment.