Skip to content

feat(core): Warn on repeated Sentry.init() and unbind the client on close() - #24962

Draft
isaacs wants to merge 3 commits into
developfrom
isaacschlueter/js-3867-bring-repeated-sentryinit-behavior-into-alignment
Draft

isaacs wants to merge 3 commits into
developfrom
isaacschlueter/js-3867-bring-repeated-sentryinit-behavior-into-alignment

Conversation

@isaacs

@isaacs isaacs commented Oct 1, 2026

Copy link
Copy Markdown
Member

A repeated Sentry.init() call was mostly undefined behavior, and each SDK handled it in its own way. Most SDKs built a new client and replaced the old one without a warning. Nothing closed the old client, so its buffers, timers, and hooks stayed alive, and setupOnce kept the settings of the first call.

The goal is one rule for all SDKs: the first init() wins, a later call returns the active client, and close() lets you start over. Most SDKs cannot switch to "first wins" before a major version, so this change adds the parts that are safe now:

  • initAndBind, Node's _init, and Vercel Edge's init print a warning (with or without debug) when a client is already bound. They still replace the client for now. Wrappers that expect a repeated call (Next.js server, Remix server, Nuxt server, Hono) keep their own guard and return early, so they do not warn. Cloudflare's cacheClient: false asks for a new client on each call, so it unbinds the old one first and does not warn. The Next.js client drops its own warning, which used a flag that never reset and so also fired after close(). Its config-file hint moves to the docs.
  • Sentry.close() unbinds the client after it closes it. Before, a guard based on getClient() treated the closed client as active, so close(); init() returned the closed client. Nuxt's server guard now also checks for a bound client, for the same reason. Cloudflare also caches its client for the isolate, and the cache handed the closed client back to every later init(), so the isolate sent nothing until it was recycled. Closing the cached client now clears the cache.
  • Next.js server and Remix server return the active client from a repeated call, not undefined. A caller could not tell "already initialized" from "failed".

docs/repeated-init.md records the rule, the current behavior of each SDK, and the plan for the next major. The warning text should point apps that share a page to the isolated client helper from #24883 once that helper has a final name.

Fixes #24960

… `close()`

A repeated `Sentry.init()` call was mostly undefined behavior, and
each SDK handled it in its own way. Most SDKs built a new client and
replaced the old one without a warning. Nothing closed the old client,
so its buffers, timers, and hooks stayed alive, and `setupOnce` kept
the settings of the first call.

The goal is one rule for all SDKs: the first `init()` wins, a later
call returns the active client, and `close()` lets you start over.
Most SDKs cannot switch to "first wins" before a major version, so
this change adds the parts that are safe now:

- `initAndBind`, Node's `_init`, and Vercel Edge's `init` print a
  warning (with or without `debug`) when a client is already bound.
  They still replace the client for now. Wrappers that expect a
  repeated call (Next.js server, Remix server, Nuxt server, Hono) keep
  their own guard and return early, so they do not warn. Cloudflare's
  `cacheClient: false` asks for a new client on each call, so it
  unbinds the old one first and does not warn. The Next.js client drops
  its own warning, which used a flag that never reset and so also fired
  after `close()`. Its config-file hint moves to the docs.
- `Sentry.close()` unbinds the client after it closes it. Before, a
  guard based on `getClient()` treated the closed client as active, so
  `close(); init()` returned the closed client. Nuxt's server guard
  now also checks for a bound client, for the same reason. Cloudflare
  also caches its client for the isolate, and the cache handed the
  closed client back to every later `init()`, so the isolate sent
  nothing until it was recycled. Closing the cached client now clears
  the cache.
- Next.js server and Remix server return the active client from a
  repeated call, not `undefined`. A caller could not tell "already
  initialized" from "failed".

`docs/repeated-init.md` records the rule, the current behavior of
each SDK, and the plan for the next major. The warning text should
point apps that share a page to the isolated client helper from
#24883 once that helper has a final name.

Fixes #24960

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@linear-code

linear-code Bot commented Oct 1, 2026

Copy link
Copy Markdown

JS-3867

@isaacs

isaacs commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.64 kB +0.43% +124 B 🔺
@sentry/browser - with treeshaking flags 27.81 kB +0.48% +131 B 🔺
@sentry/browser - with treeshaking flags tracing without tracing 27.71 kB +0.48% +132 B 🔺
@sentry/browser (incl. Tracing) 51.59 kB +0.26% +132 B 🔺
@sentry/browser (incl. Tracing + Span Streaming) 51.6 kB +0.26% +130 B 🔺
@sentry/browser (incl. Tracing, Profiling) 54.59 kB +0.21% +114 B 🔺
@sentry/browser (incl. Tracing, Replay) 91.17 kB +0.14% +126 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.16 kB +0.16% +128 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 95.86 kB +0.13% +124 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 108.84 kB +0.12% +121 B 🔺
@sentry/browser (incl. Feedback) 47.16 kB +0.26% +120 B 🔺
@sentry/browser (incl. sendFeedback) 34.69 kB +0.35% +120 B 🔺
@sentry/browser (incl. FeedbackAsync) 39.8 kB +0.3% +118 B 🔺
@sentry/browser (incl. Metrics) 30.65 kB +0.4% +122 B 🔺
@sentry/browser (incl. Logs) 30.94 kB +0.4% +122 B 🔺
@sentry/browser (incl. Metrics & Logs) 31.6 kB +0.39% +122 B 🔺
@sentry/react 31.48 kB +0.38% +119 B 🔺
@sentry/react (incl. Tracing) 53.95 kB +0.23% +121 B 🔺
@sentry/vue 37.62 kB +0.31% +113 B 🔺
@sentry/vue (incl. Tracing) 54.48 kB +0.23% +121 B 🔺
@sentry/svelte 29.66 kB +0.42% +123 B 🔺
@sentry/remix (Remix 3 client bundle) 55.89 kB +0.27% +145 B 🔺
CDN Bundle 31.38 kB +0.5% +155 B 🔺
CDN Bundle (incl. Tracing) 52.14 kB +0.3% +153 B 🔺
CDN Bundle (incl. Logs, Metrics) 33.62 kB +0.49% +161 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 54.09 kB +0.29% +155 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 74.36 kB +0.21% +152 B 🔺
CDN Bundle (incl. Tracing, Replay) 89.71 kB +0.18% +153 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 91.69 kB +0.17% +153 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 95.89 kB +0.17% +157 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 97.89 kB +0.16% +155 B 🔺
CDN Bundle - uncompressed 92.51 kB +0.4% +362 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 154.89 kB +0.24% +362 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.08 kB +0.37% +362 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 160.85 kB +0.23% +362 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 228.65 kB +0.16% +362 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 274.62 kB +0.14% +362 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 280.56 kB +0.13% +362 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 288.32 kB +0.13% +362 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 294.25 kB +0.13% +362 B 🔺
@sentry/nextjs (client) 56.31 kB -0.04% -20 B 🔽
@sentry/sveltekit (client) 52 kB +0.23% +115 B 🔺
@sentry/core/server 39.99 kB - -
@sentry/core/browser 13.63 kB - -
@sentry/node 144.59 kB +0.09% +120 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.08 kB - -
@sentry/node - without tracing 93.14 kB +0.13% +114 B 🔺
@sentry/node - without channel injection 122.87 kB +0.1% +119 B 🔺
@sentry/aws-serverless 101.44 kB +0.11% +109 B 🔺
@sentry/cloudflare (withSentry) - minified 207.55 kB +0.22% +453 B 🔺
@sentry/cloudflare (withSentry) 516.11 kB +0.16% +797 B 🔺

View base workflow run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 78c4faa. Configure here.

Comment thread packages/remix/src/server/sdk.ts

This branch has not been deployed

No deployments
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.

Bring repeated Sentry.init() behavior into alignment

1 participant