supabase-js
supabase-js copied to clipboard
fix(auth): prevent deadlock and unhandled rejections in processLock
Summary
Fixes unhandled promise rejections and potential deadlocks in processLock by replacing the Promise.race timeout pattern with a flag-based approach.
Problem
The processLock function 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 timeout - When
lockOperation()wins the race (lock acquired before timeout), the timeout promise still fires later - The timeout promise rejection goes unhandled, crashing Node.js
- Additionally, the
PROCESS_LOCKS[name]promise chain would re-throw errors, causing subsequent operations waiting on it to receive unhandled rejections or potentially deadlock
Solution
- Replace
Promise.racewith a flag-based approach: set atimeoutErrorflag in the setTimeout callback, check it after acquiring the lock - Clear the timeout immediately when the lock is acquired to prevent unnecessary timeout firing
- Ensure
PROCESS_LOCKS[name]always resolves (never throws) so subsequent operations can proceed without deadlocking - Fix test expectations: operation 0 acquires the lock immediately (no previous operation to wait on), so it completes successfully rather than timing out
Related
Fixes the ProcessLockAcquireTimeoutError: Acquiring process lock with name "rapid-test" timed out crash in @supabase/auth-js:test