Skip to content

fix(react): Detect nested ClerkProvider via context instead of a global mount counter - #9335

Open
wobsoriano wants to merge 4 commits into
mainfrom
rob/detect-nested-provider
Open

fix(react): Detect nested ClerkProvider via context instead of a global mount counter#9335
wobsoriano wants to merge 4 commits into
mainfrom
rob/detect-nested-provider

Conversation

@wobsoriano

@wobsoriano wobsoriano commented Aug 5, 2026

Copy link
Copy Markdown
Member

Description

On Android, activity recreation (returning from a third-party SDK, a font size or locale change, the OS restoring a backgrounded app) briefly runs two React surfaces in one JS runtime. ClerkProvider's duplicate guard counts mounts in a module-scoped map, so the new surface sees the old surface's mount and throws multipleClerkProvidersError in apps that render exactly one provider, crashing the whole app with no opt-out.

ClerkProvider now reads ClerkInstanceContext during render and throws only when genuinely nested inside another provider. Context is per tree, so a second root or surface can't false-positive. This mirrors the isNested check @clerk/nextjs has used for years. The global counter predates the IsomorphicClerk singleton, when each provider injected its own clerk-js script and a duplicate anywhere genuinely broke the page.

Reproduced and verified on an Android emulator with an Expo dev build. Rapid repeated config changes (font scale and dark mode toggles 0.4s apart) put two surfaces in flight and crashed the current release within two rounds. The same loop against a snapshot of this branch produced 24 surface restarts with zero errors and a healthy app.

Fixes https://clerkinc.slack.com/archives/C0B3EGPLNKH/p1785876391910099

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot

changeset-bot Bot commented Aug 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8efe61f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@clerk/react Patch
@clerk/chrome-extension Patch
@clerk/electron Patch
@clerk/expo Patch
@clerk/nextjs Patch
@clerk/react-router Patch
@clerk/tanstack-react-start Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Aug 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview Aug 5, 2026 4:08am
swingset Ready Ready Preview Aug 5, 2026 4:08am

Request Review

@github-actions github-actions Bot added the react label Aug 5, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@9335

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@9335

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@9335

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@9335

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@9335

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@9335

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@9335

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@9335

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@9335

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@9335

@clerk/express

npm i https://pkg.pr.new/@clerk/express@9335

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@9335

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@9335

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@9335

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@9335

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@9335

@clerk/react

npm i https://pkg.pr.new/@clerk/react@9335

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@9335

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@9335

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@9335

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@9335

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@9335

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@9335

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@9335

commit: 8efe61f

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-08-05T04:08:55.986Z

Summary

Metric Count
Packages analyzed 19
Packages with changes 0
🔴 Breaking changes 0
🟡 Non-breaking changes 0
🟢 Additions 0

No API Changes Detected

All packages have stable APIs with no detected changes.


Report generated by Break Check

Last ran on 8efe61f.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 7fa71538-9dee-4b98-96a8-186e3f090819

📥 Commits

Reviewing files that changed from the base of the PR and between 94509e0 and 8efe61f.

📒 Files selected for processing (1)
  • packages/react/src/contexts/ClerkProvider.tsx
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
  • clerk/clerk-ios (auto-detected)
  • clerk/clerk-android (auto-detected)
  • clerk/cli (auto-detected)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/react/src/contexts/ClerkProvider.tsx

📝 Walkthrough

Walkthrough

ClerkProvider now uses ClerkInstanceContext to detect nested providers and throws multipleClerkProvidersError before rendering the base provider. Tests cover nested providers and concurrent providers in separate React roots. A patch changeset documents the fix.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: nikosdouvlis

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: detecting nested ClerkProvider instances through context instead of a global mount counter.
Description check ✅ Passed The description clearly explains the Android false-positive crash, the context-based fix, validation results, and related implementation changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/react/src/contexts/__tests__/ClerkProvider.test.tsx (1)

27-27: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Replace any in the mock singleton.

Use IsomorphicClerk | undefined for instance to retain type checking for the shared mock.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/react/src/contexts/__tests__/ClerkProvider.test.tsx` at line 27,
Update the mock singleton’s instance declaration in ClerkProvider tests to use
IsomorphicClerk | undefined instead of any, preserving type checking for the
shared mock while retaining its initially undefined state.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/react/src/contexts/__tests__/ClerkProvider.test.tsx`:
- Line 27: Update the mock singleton’s instance declaration in ClerkProvider
tests to use IsomorphicClerk | undefined instead of any, preserving type
checking for the shared mock while retaining its initially undefined state.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 8306c9aa-8672-4edf-8711-dc58b7891e57

📥 Commits

Reviewing files that changed from the base of the PR and between 85e7571 and 94509e0.

📒 Files selected for processing (1)
  • packages/react/src/contexts/__tests__/ClerkProvider.test.tsx
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
  • clerk/clerk-ios (auto-detected)
  • clerk/clerk-android (auto-detected)
  • clerk/cli (auto-detected)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant