Skip to content

fix(clerk-js): Fix token cache refresh timer leak#8098

Open
jacekradko wants to merge 8 commits intomainfrom
jacek/fix-token-cache-timer-leak
Open

fix(clerk-js): Fix token cache refresh timer leak#8098
jacekradko wants to merge 8 commits intomainfrom
jacek/fix-token-cache-timer-leak

Conversation

@jacekradko
Copy link
Member

@jacekradko jacekradko commented Mar 17, 2026

Summary

  • Fix orphaned refresh timer accumulation in SessionTokenCache that caused token refresh requests to fire much earlier than expected
  • When set() is called multiple times for the same cache key (from _updateClient hydration AND #refreshTokenInBackground), old timers were never cleared — each leaked timer independently fired onRefresh, accelerating the effective polling rate
  • The fix clears existing timers at the start of setInternal before creating the new cache entry
  • Particularly visible after session.touch() or any API response that triggers _updateClient

Test plan

  • 8 new unit tests covering timer cleanup on overwrite scenarios
  • All 45 unit tests pass
  • Integration test verifying stable refresh rate after session.touch()
  • Manual verification: sign in, trigger touch, observe network tab for ~2 minutes — token requests should maintain ~43s intervals (not accelerating)

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a proactive token refresh timer leak that could cause growing token refresh requests after session activity or switching, preventing orphaned/stale timers and reducing redundant network calls.
  • Tests

    • Added unit and integration tests validating timers are cleaned up across repeated session updates so token refreshes remain stable and non-duplicative.

When set() was called multiple times for the same cache key, old refresh
and expiration timers were never cancelled. This caused orphaned timers
to accumulate — each independently firing onRefresh — making the poller
appear erratic with accelerating token refresh requests.

The root cause: both _updateClient (via Session constructor → #hydrateCache)
and #refreshTokenInBackground call set() for the same key during a single
refresh cycle. Each set() created new timers without clearing the old ones.
After N cycles, N orphaned timers all fire at different offsets.

Fix: clear existing timers at the start of setInternal before creating
the new cache entry.
@changeset-bot
Copy link

changeset-bot bot commented Mar 17, 2026

🦋 Changeset detected

Latest commit: c0f0f3d

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

This PR includes changesets to release 3 packages
Name Type
@clerk/clerk-js Patch
@clerk/chrome-extension Patch
@clerk/expo 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
Copy link

vercel bot commented Mar 17, 2026

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

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview, Comment Mar 18, 2026 6:40pm

Request Review

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 17, 2026

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@8098

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@8098

@clerk/expo

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

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

@clerk/upgrade

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

@clerk/vue

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

commit: c0f0f3d

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 17, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

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

Review profile: ASSERTIVE

Plan: Pro

Run ID: 2ff59aca-7231-4a29-8859-789ed1ed4887

📥 Commits

Reviewing files that changed from the base of the PR and between 1c01b01 and c0f0f3d.

📒 Files selected for processing (1)
  • packages/clerk-js/src/core/__tests__/tokenCache.test.ts

📝 Walkthrough

Walkthrough

Clears prior refresh and expiration timers before updating entries in MemoryTokenCache.setInternal and prevents installing timers for entries overwritten while a pending tokenResolver resolves, fixing a token cache refresh timer leak. Adds unit tests under packages/clerk-js/src/core/__tests__/tokenCache.test.ts, a Playwright integration test at integration/tests/session-token-cache/refresh-timer-cleanup.test.ts, and a changeset entry for a patch release.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(clerk-js): Fix token cache refresh timer leak' directly and clearly summarizes the main change: fixing a token cache refresh timer leak in clerk-js.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/clerk-js/src/core/tokenCache.ts`:
- Around line 353-364: The cache set flow currently only clears timers on
existing entries but doesn't prevent a pending tokenResolver promise from later
installing orphaned refresh timers or invoking onRefresh after the key has been
overwritten; update the logic in the tokenCache set/update paths (the code that
constructs/assigns entries with tokenResolver, timeoutId, refreshTimeoutId and
that installs refresh timers and calls onRefresh) to attach a unique
marker/version id to each cache entry (e.g., entry.version or entry.resolverId)
when creating it and, before installing any timers or invoking onRefresh inside
tokenResolver.then/Promise continuations, verify the stored marker still matches
the current cache entry for that key—if it does not match, bail out and do not
set refreshTimeoutId or call onRefresh; apply the same guard wherever
tokenResolver continuations create timers (the other block that handles
refreshTimeoutId) so pending resolvers cannot schedule orphaned refreshes after
an overwrite.

ℹ️ Review info
⚙️ Run configuration

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

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9af73399-f0a6-4ae4-810d-636541e51793

📥 Commits

Reviewing files that changed from the base of the PR and between b9cb6e5 and fd6d143.

📒 Files selected for processing (4)
  • .changeset/fix-token-cache-timer-leak.md
  • integration/tests/session-token-cache/refresh-timer-cleanup.test.ts
  • packages/clerk-js/src/core/__tests__/tokenCache.test.ts
  • packages/clerk-js/src/core/tokenCache.ts

Comment on lines +357 to +364
if (existing) {
if (existing.timeoutId !== undefined) {
clearTimeout(existing.timeoutId);
}
if (existing.refreshTimeoutId !== undefined) {
clearTimeout(existing.refreshTimeoutId);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

NIT: I don't mind the verbosity, just noting that since clearTimeout(undefined) is a noop, this could be shortened for readability:

Suggested change
if (existing) {
if (existing.timeoutId !== undefined) {
clearTimeout(existing.timeoutId);
}
if (existing.refreshTimeoutId !== undefined) {
clearTimeout(existing.refreshTimeoutId);
}
}
clearTimeout(existing?.timeoutId);
clearTimeout(existing?.refreshTimeoutId);

Comment on lines 383 to +389
entry.tokenResolver
.then(newToken => {
// If this entry was overwritten by a newer set() call while our promise
// was pending, bail out to avoid installing orphaned timers.
if (cache.get(key) !== value) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

This makes sense and as long as tokenResolver doesn't have side effects (which it doesn't since that fetch has no client piggybacking), short circuiting here seems safe.

NIT: Moving

cache.set(key, value);

to above

entry.tokenResolver
  .then(newToken => {

should be semantically the same, and could make this part a tiny bit clearer to read.

const existing = cache.get(key);
if (existing) {
if (existing.timeoutId !== undefined) {
clearTimeout(existing.timeoutId);
Copy link
Member

Choose a reason for hiding this comment

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

If we only want to have one timer pending at any single time, would it make sense to move the timeoutId and refreshTimeoutId out into the parent scope and not saving them on the entries to have stricter guarantees?

expect(result?.entry.tokenId).toBe('exp-overwrite');
});

it('does not accumulate refresh timers across multiple set() calls', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

NIT: Is this test necessary? It seems implied by the cancels old refresh timer when set() is called again for the same key test.

expect(onRefresh).toHaveBeenCalledTimes(1);
});

it('simulates the hydration + background refresh double-set scenario', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Is this not the same test as cancels old refresh timer when set() is called again for the same key, with different names for the onRefresh callbacks?

I like that these names reflect a real scenario, so maybe merge, but use these names and point out it's one scenario that could happen?

expect(backgroundRefresh).toHaveBeenCalledTimes(1);
});

it('simulates multiple refresh cycles without timer accumulation', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

This looks like cancels old refresh timer when set() is called again for the same key but 5 times in a row.

I think this is a keeper though since it might test for other scenarios of accumulation which is good.

}
});

it('set() with different key does not affect existing timers', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, what's the scenario when we should be refreshing multiple tokens in parallel in a single tab?

Comment on lines +51 to +55
for (let i = 0; i < 5; i++) {
await page.evaluate(async () => {
await (window as any).Clerk?.session?.touch();
});
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a multi-session app so it wont hit the 5s touch throttle? Might be worth a comment.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants