supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

fix(auth): prevent deadlock and unhandled rejections in processLock

Open mayur9210 opened this issue 4 weeks ago • 0 comments

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:

  1. processLock uses Promise.race([lockOperation(), timeoutPromise]) to implement acquire timeout
  2. When lockOperation() wins the race (lock acquired before timeout), the timeout promise still fires later
  3. The timeout promise rejection goes unhandled, crashing Node.js
  4. 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.race with a flag-based approach: set a timeoutError flag 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

mayur9210 avatar Jan 14 '26 14:01 mayur9210