Skip to content

fix(auth): defer init-time notifications until initializePromise resolves - #2498

Merged
mandarini merged 2 commits into
masterfrom
fix/init-notify-deadlock
Jul 7, 2026
Merged

mandarini merged 2 commits into
masterfrom
fix/init-notify-deadlock

Conversation

@mandarini

@mandarini mandarini commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

What changed?

Three changes to GoTrueClient in @supabase/auth-js:

  • Added a private _pendingInitNotifications field -- an array that is non-null only while initialize() is running, used as a deferred notification queue.
  • Added a guard at the top of _notifyAllSubscribers(): when the queue is open (non-null), notifications are enqueued instead of fired immediately.
  • Modified initialize() to open the queue before _initialize() runs, then flush it in order after initializePromise resolves.

Three regression tests were added to GoTrueClient.test.ts covering the SIGNED_IN path (valid session at boot), the TOKEN_REFRESHED path (near-expiry session that triggers a proactive refresh), and an assertion that initializePromise is already resolved when the callback runs.

Why was this change needed?

During initialize(), _recoverAndRefresh() fires auth events (SIGNED_IN, TOKEN_REFRESHED, SIGNED_OUT) before initializePromise has resolved. Every public auth method -- getSession(), getUser(), signOut(), updateUser(), refreshSession() -- gates on await this.initializePromise. If an onAuthStateChange callback called any of those methods, a permanent circular deadlock formed:

  • callback awaits getSession()
  • getSession() awaits initializePromise
  • initializePromise awaits _initialize()
  • _initialize() awaits _notifyAllSubscribers()
  • _notifyAllSubscribers() awaits the callback

Once stuck, initializePromise never resolves for the lifetime of the client. Because a new client is constructed on each page load and finds the same valid session in storage, the hang survives full reloads -- which made it appear persistent. This also caused signInWithPassword to hang after a successful 200 response, because it fires SIGNED_IN through the same notify path.

The lockless refactor in v2.107.0 (PR #2392) resolved the navigator.locks-based deadlock but left this separate initializePromise circular dependency intact. It manifested most reliably on iOS PWA, where sessions persist across reloads and the stored-session boot path (which triggers SIGNED_IN during init) is the common case.

The fix defers all init-time notifications until after initializePromise is resolved, so callbacks can call any public auth method safely.

Closes #2491

Screenshots/Examples

Before the fix -- initialize() would never return:

supabase.auth.onAuthStateChange(async (event, session) => {
  if (event === 'SIGNED_IN') {
    // This awaits initializePromise, which is still pending -- permanent hang
    const { data } = await supabase.auth.getSession()
  }
})

await supabase.auth.initialize() // never resolves

After the fix, initialize() completes and getSession() inside the callback works correctly.

Breaking changes

  • This PR contains no breaking changes

initialize() still waits for all init-time callbacks to complete before returning. Event ordering is preserved. The only behavioral difference is that getSession() and other public methods now resolve inside a callback instead of deadlocking.

Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run pnpm nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

Additional notes

The guard in _notifyAllSubscribers() is the single interception point that covers all four call sites in _recoverAndRefresh() without modifying each one individually. The _pendingInitNotifications field is null at all times except during an active initialize() call, so there is no behavior change for any notification fired outside of initialization.

@github-actions github-actions Bot added the auth-js Related to the auth-js library. label Jul 6, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@2498

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@2498

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@2498

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@2498

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@2498

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@2498

commit: 0e5bd93

@coveralls

coveralls commented Jul 6, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 71.473%. first build — fix/init-notify-deadlock into master

@mandarini
mandarini marked this pull request as ready for review July 6, 2026 13:24
@mandarini
mandarini requested review from a team as code owners July 6, 2026 13:24
@mandarini mandarini self-assigned this Jul 6, 2026
@mandarini
mandarini force-pushed the fix/init-notify-deadlock branch from 3861d3a to 0e5bd93 Compare July 6, 2026 14:40
@mandarini
mandarini merged commit 3d2c620 into master Jul 7, 2026
30 checks passed
@mandarini
mandarini deleted the fix/init-notify-deadlock branch July 7, 2026 11:38
mandarini pushed a commit to supabase/ssr that referenced this pull request Jul 7, 2026
This PR updates `@supabase/supabase-js` to v2.110.1.

**Source**: supabase-js-stable-release

---

## Release Notes

## v2.110.1

## 2.110.1 (2026-07-07)

### 🩹 Fixes

- **auth:** defer init-time notifications until initializePromise
resolves ([#2498](supabase/supabase-js#2498))
- **realtime:** suppress disconnected status from onHeartbeat consumers
([#2496](supabase/supabase-js#2496))

### ❤️ Thank You

- Katerina Skroumpelou @mandarini

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
mandarini pushed a commit to supabase/supabase that referenced this pull request Jul 7, 2026
This PR updates @supabase/*-js libraries to version 2.110.1.

**Source**: supabase-js-stable-release

**Changes**:
- Updated @supabase/supabase-js to 2.110.1
- Updated @supabase/auth-js to 2.110.1
- Updated @supabase/realtime-js to 2.110.1
- Updated @supabase/postgest-js to 2.110.1
- Refreshed pnpm-lock.yaml

---

## Release Notes

## v2.110.1

## 2.110.1 (2026-07-07)

### 🩹 Fixes

- **auth:** defer init-time notifications until initializePromise
resolves ([#2498](supabase/supabase-js#2498))
- **realtime:** suppress disconnected status from onHeartbeat consumers
([#2496](supabase/supabase-js#2496))

### ❤️ Thank You

- Katerina Skroumpelou @mandarini
## v2.110.0

## 2.110.0 (2026-06-30)

### 🚀 Features

- **repo:** drop Node.js 20 support
([#2482](supabase/supabase-js#2482))

### ❤️ Thank You

- Katerina Skroumpelou @mandarini
## v2.109.0

## 2.109.0 (2026-06-30)

### 🚀 Features

- **auth:** add custom_claims_allowlist to custom providers admin API
([#2473](supabase/supabase-js#2473))
- **realtime:** add postgres_changes filter builder, new operators and
select ([#2463](supabase/supabase-js#2463))
- **storage:** expose purgeCache for buckets and single objects
([#2429](supabase/supabase-js#2429))

### 🩹 Fixes

- **functions:** honor a caller's Content-Type override regardless of
casing ([#2455](supabase/supabase-js#2455))
- **realtime:** pin @supabase/phoenix and browser test CDN deps
([#2457](supabase/supabase-js#2457))
- **realtime:** add replication connection system message option
([#2470](supabase/supabase-js#2470))
- **storage:** keep sortBy defaults when list() is given a partial
sortBy ([#2454](supabase/supabase-js#2454))

### ❤️ Thank You

- Anubhav Anand @i-anubhav-anand
- Cemal Kılıç @cemalkilic
- Claude Opus 4.8 (1M context)
- Filipe Cabaço @filipecabaco
- Katerina Skroumpelou @mandarini
- Lenny
- Rodrigo Mansueli @mansueli

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
@coderabbitai coderabbitai Bot mentioned this pull request Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth-js Related to the auth-js library.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug report: signInWithPassword and session recovery hang indefinitely, no network request visible, survives full page reload Environment

3 participants