fix(auth): prevent deadlock and unhandled rejections in processLock #2019
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes unhandled promise rejections and potential deadlocks in
processLockby replacing thePromise.racetimeout pattern with a flag-based approach.Problem
The
processLockfunction would crash Node.js with unhandled promise rejections when running tests with rapid successive lock operations that have mixed timeouts.The crash occurred because:
processLockusesPromise.race([lockOperation(), timeoutPromise])to implement acquire timeoutlockOperation()wins the race (lock acquired before timeout), the timeout promise still fires laterPROCESS_LOCKS[name]promise chain would re-throw errors, causing subsequent operations waiting on it to receive unhandled rejections or potentially deadlockSolution
Promise.racewith a flag-based approach: set atimeoutErrorflag in the setTimeout callback, check it after acquiring the lockPROCESS_LOCKS[name]always resolves (never throws) so subsequent operations can proceed without deadlockingRelated
Fixes the
ProcessLockAcquireTimeoutError: Acquiring process lock with name "rapid-test" timed outcrash in@supabase/auth-js:test