Skip to content

fix(shared): Fix memory leaks in react native environment #6485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2025

Conversation

brkalow
Copy link
Member

@brkalow brkalow commented Aug 7, 2025

Description

We got a report of a memory leak in our Expo SDK, which appeared to be stemming from our telemetry code. One likely culprit is our closing over of this.#buffer in the fetch call, which had the possibility to retain the reference if the fetch never resolved.

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:

Summary by CodeRabbit

  • Bug Fixes

    • Resolved a potential memory leak in telemetry data collection, improving resource management and application stability.
  • Tests

    • Added a new test to ensure telemetry event batches are properly isolated, preventing event duplication or mixing between batches.
  • New Features

    • Introduced event sampling rate metadata to telemetry events for improved data handling.

@brkalow brkalow requested a review from jacekradko August 7, 2025 03:24
Copy link

changeset-bot bot commented Aug 7, 2025

🦋 Changeset detected

Latest commit: 1cbf4c0

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

This PR includes changesets to release 19 packages
Name Type
@clerk/shared Patch
@clerk/agent-toolkit Patch
@clerk/astro Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/clerk-expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/clerk-react Patch
@clerk/remix Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/vue 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

Copy link

vercel bot commented Aug 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 7, 2025 3:52am

Copy link

pkg-pr-new bot commented Aug 7, 2025

Open in StackBlitz

@clerk/agent-toolkit

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

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/dev-cli

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

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@6485

@clerk/clerk-expo

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/clerk-react

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

@clerk/react-router

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

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@6485

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@6485

@clerk/types

npm i https://pkg.pr.new/@clerk/types@6485

@clerk/upgrade

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

@clerk/vue

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

commit: 1cbf4c0

Copy link
Contributor

coderabbitai bot commented Aug 7, 2025

📝 Walkthrough

Walkthrough

A patch was applied to the TelemetryCollector component within the @clerk/shared package to address a potential memory leak. The main change refactors the internal buffer management in the #flush() method by copying the current buffer to a new array and clearing the original buffer immediately before sending events. The fetch request now uses the copied array and includes the keepalive: true option. A new test case was added to verify that event batches do not share references, ensuring proper isolation and preventing event duplication or mixing. Additionally, a new constant EVENT_SAMPLING_RATE was introduced and included in the eventMethodCalled event object. No changes were made to the declarations or signatures of exported or public entities.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 87122c0 and 1cbf4c0.

📒 Files selected for processing (2)
  • packages/shared/src/telemetry/collector.ts (1 hunks)
  • packages/shared/src/telemetry/events/method-called.ts (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/shared/src/telemetry/events/method-called.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/shared/src/telemetry/collector.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: Integration Tests (nextjs, chrome, 15)
  • GitHub Check: Integration Tests (nextjs, chrome, 14)
  • GitHub Check: Integration Tests (billing, chrome)
  • GitHub Check: Integration Tests (react-router, chrome)
  • GitHub Check: Integration Tests (vue, chrome)
  • GitHub Check: Integration Tests (tanstack-react-router, chrome)
  • GitHub Check: Integration Tests (nuxt, chrome)
  • GitHub Check: Integration Tests (express, chrome)
  • GitHub Check: Integration Tests (sessions, chrome)
  • GitHub Check: Integration Tests (tanstack-react-start, chrome)
  • GitHub Check: Integration Tests (expo-web, chrome)
  • GitHub Check: Integration Tests (localhost, chrome)
  • GitHub Check: Integration Tests (elements, chrome)
  • GitHub Check: Integration Tests (astro, chrome)
  • GitHub Check: Integration Tests (quickstart, chrome)
  • GitHub Check: Integration Tests (ap-flows, chrome)
  • GitHub Check: Integration Tests (generic, chrome)
  • GitHub Check: Static analysis
  • GitHub Check: Unit Tests (18, --filter=@clerk/astro --filter=@clerk/backend --filter=@clerk/express --filter=@c...
  • GitHub Check: Unit Tests (22, **)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -1,6 +1,7 @@
import type { TelemetryEventRaw } from '@clerk/types';

const EVENT_METHOD_CALLED = 'METHOD_CALLED';
const EVENT_SAMPLING_RATE = 0.1;
Copy link
Member Author

Choose a reason for hiding this comment

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

Expo environments don't currently throttle based on already-sent events in the session, as we rely on localStorage for that. Reducing the sampling rate for now to mitigate excessive events.

Copy link
Member

@jacekradko jacekradko left a comment

Choose a reason for hiding this comment

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

ship it

Copy link
Member

@nikosdouvlis nikosdouvlis left a comment

Choose a reason for hiding this comment

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

LGTM

@nikosdouvlis nikosdouvlis merged commit 1cc66ab into main Aug 7, 2025
38 checks passed
@nikosdouvlis nikosdouvlis deleted the brk.fix/telemetry-memory-leak branch August 7, 2025 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants