Skip to content

Commit

Permalink
fix: unhandled promise rejection (#5495)
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l authored Aug 22, 2024
1 parent 5da9e99 commit ae6b5d0
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions packages/services/api/src/modules/shared/providers/mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,36 @@ export class Mutex {
// We try to acquire the lock until the retry counter is exceeded or the lock as been successfully acquired.
do {
logger.debug('Acquiring lock (id=%s, attempt=%n)', id, attemptCounter + 1);
// we avoid using any of the acquire settings for auto-extension, retrying, etc.
// because of the many bugs and weird API design choices in the redlock library.
// By manually handling the retries and lock extension we can abort acquiring the lock as soon as the incoming request has been canceled
lockToAcquire = await this.redlock
.acquire([id], duration, {
// we only want to try once to acquire the lock
// if we fail, we will retry manually with our own logic
retryCount: 0,
})
.catch((err: unknown) => {
// Note: This is kind of a workaround.
// The redlock library should not throw `ExecutionError`, but `ResourceLockedError`.
// We have our own error here for the Mutex.
// See https://github.com/mike-marcacci/node-redlock/issues/168
if (
err instanceof ExecutionError &&
err.message === 'The operation was unable to achieve a quorum during its retry window.'
) {
return null;
}

logger.error('Error while acquiring lock (id=%s)', id);
console.error(err);
throw err;
});

lockToAcquire = await Promise.race([
// we avoid using any of the acquire settings for auto-extension, retrying, etc.
// because of the many bugs and weird API design choices in the redlock library.
// By manually handling the retries and lock extension we can abort acquiring the lock as soon as the incoming request has been canceled
this.redlock
.acquire([id], duration, {
// we only want to try once to acquire the lock
// if we fail, we will retry manually with our own logic
retryCount: 0,
})
.catch((err: unknown) => {
// Note: This is kind of a workaround.
// The redlock library should not throw `ExecutionError`, but `ResourceLockedError`.
// We have our own error here for the Mutex.
// See https://github.com/mike-marcacci/node-redlock/issues/168
if (
err instanceof ExecutionError &&
err.message ===
'The operation was unable to achieve a quorum during its retry window.'
) {
return null;
}

logger.error('Error while acquiring lock (id=%s)', id);
console.error(err);
throw err;
}),
requestAbortedD.promise,
]);

if (lockToAcquire !== null) {
break;
Expand Down

0 comments on commit ae6b5d0

Please sign in to comment.