Repository navigation
feat(core): Warn on repeated Sentry.init() and unbind the client on close()
#24962
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
isaacs
merged 7 commits into
develop
from
isaacschlueter/js-3867-bring-repeated-sentryinit-behavior-into-alignment
Oct 5, 2026
+859
−126
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed16e3b
feat(core): Warn on repeated `Sentry.init()` and unbind the client on…
isaacs b730b60
fix(core): Do not treat a closed client as active in init guards
isaacs 03935a6
test: e2e test for repeated init() calls
isaacs 01a2e40
fix(core): Mark closed clients on the client, not in a module-level set
isaacs ec00267
fix: bugbot issue
isaacs 60932ab
Merge branch 'develop' into isaacschlueter/js-3867-bring-repeated-sen…
isaacs 429c91a
fix(nextjs): Add client event processors to the client
isaacs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ackages/node-integration-tests/suites/public-api/init-repeated/scenario-close-in-scope.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| async function run(): Promise<void> { | ||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| // The root scope still holds the closed client after this. | ||
| await Sentry.withIsolationScope(() => Sentry.close()); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '2.0', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| Sentry.captureMessage('after close in a child scope and init'); | ||
| } | ||
|
|
||
| run(); |
22 changes: 22 additions & 0 deletions
22
dev-packages/node-integration-tests/suites/public-api/init-repeated/scenario-close.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| async function run(): Promise<void> { | ||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| await Sentry.close(); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '2.0', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| Sentry.captureMessage('after close and init'); | ||
| } | ||
|
|
||
| run(); |
16 changes: 16 additions & 0 deletions
16
dev-packages/node-integration-tests/suites/public-api/init-repeated/scenario-repeated.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '2.0', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| Sentry.captureMessage('after repeated init'); |
38 changes: 38 additions & 0 deletions
38
dev-packages/node-integration-tests/suites/public-api/init-repeated/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { afterAll, expect, test } from 'vitest'; | ||
| import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
|
||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| const REPEATED_INIT_WARNING = '`Sentry.init()` was called more than once'; | ||
|
|
||
| test('warns on a repeated init and sends events with the new client', async () => { | ||
| const runner = createRunner(__dirname, 'scenario-repeated.ts') | ||
| .expect({ event: { message: 'after repeated init', release: '2.0' } }) | ||
| .start(); | ||
|
|
||
| await runner.completed(); | ||
|
|
||
| expect(runner.getLogs().join('\n')).toContain(REPEATED_INIT_WARNING); | ||
| }); | ||
|
|
||
| test('does not warn on init after close and sends events with the new client', async () => { | ||
| const runner = createRunner(__dirname, 'scenario-close.ts') | ||
| .expect({ event: { message: 'after close and init', release: '2.0' } }) | ||
| .start(); | ||
|
|
||
| await runner.completed(); | ||
|
|
||
| expect(runner.getLogs().join('\n')).not.toContain(REPEATED_INIT_WARNING); | ||
| }); | ||
|
|
||
| test('does not warn on init after close in a child scope', async () => { | ||
| const runner = createRunner(__dirname, 'scenario-close-in-scope.ts') | ||
| .expect({ event: { message: 'after close in a child scope and init', release: '2.0' } }) | ||
| .start(); | ||
|
|
||
| await runner.completed(); | ||
|
|
||
| expect(runner.getLogs().join('\n')).not.toContain(REPEATED_INIT_WARNING); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # Repeated `Sentry.init()` Calls | ||
|
|
||
| This document records how `Sentry.init()` should act when an app calls it | ||
| more than once. Use it when you add or change an `init()` in any SDK. | ||
|
|
||
| ## The rule | ||
|
|
||
| > One active client per `init()` target. The first `init()` wins. A later | ||
| > `init()` changes nothing, returns the active client, and warns. To | ||
| > reconfigure, call `close()` first. | ||
|
|
||
| "Active" means a client is bound to the current scope and is not closed. | ||
| `Sentry.close()` closes the client and unbinds it, so a later `init()` sets | ||
| up a new client. Other scopes can still hold the closed client (for example, | ||
| when `close()` runs inside a request, or when code calls `client.close()` | ||
| directly), so a check for "already initialized" must not use `getClient()` | ||
| alone. | ||
|
|
||
| A repeated `init()` is not supported. Until the next major version, most | ||
| SDKs still replace the client (see below). Do not depend on that. | ||
|
|
||
| ## Why | ||
|
|
||
| When `init()` replaces a client, nothing closes the old one: | ||
|
|
||
| - Buffered logs, metrics, spans, client reports, and flush timers stay on | ||
| the old client. | ||
| - `setupOnce` runs only for the first client, because the list of | ||
| installed integrations is global. The result mixes settings from both | ||
| calls. | ||
| - `initialScope` merges into the current scope on each call. | ||
|
|
||
| "First wins" never gives a user half of one config and half of another. | ||
|
|
||
| ## Current behavior | ||
|
|
||
| | Entry point | Repeated call | | ||
| | ------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | `initAndBind` (browser and its wrappers, Deno), Node `_init`, Vercel Edge | Warns, then replaces the client. Returns the new client. | | ||
| | Next.js server, Remix server, Hono Node | Keeps the first client and returns it. Logs in debug mode only. | | ||
| | Hono Bun and Deno | Keeps the first client and returns it. Warns with its own text. | | ||
| | Nuxt server | Keeps the first client and returns it. Logs that a `--import` preload is no longer needed. | | ||
| | Cloudflare (default) | Keeps the first client of the isolate and returns it, unless that client is closed or closing. `cacheClient: false` makes a new client on each call, with no warning. | | ||
| | Next.js edge | Warns, then replaces the client. Returns `void`. | | ||
|
|
||
| The shared warning lives in `warnIfClientIsActive()` in | ||
| `packages/core/src/sdk.ts`. Core exports it as | ||
| `_INTERNAL_warnIfClientIsActive` for SDKs that build their client without | ||
| `initAndBind`. | ||
|
|
||
| A wrapper that expects a repeated call, such as a server bundle and a | ||
| `--import` preload that both run the config, keeps its own guard and | ||
| returns early. The shared warning then does not show. Guards use | ||
| `getActiveClient()` (exported as `_INTERNAL_getActiveClient`), which | ||
| returns the bound client only if it is not closed. | ||
|
|
||
| ## TODO(v12): Plan for the next major version | ||
|
|
||
| 1. Move the "first wins" guard into `initAndBind`, Node's `_init`, and | ||
| Vercel Edge's `init`. Remove the guards in each wrapper. | ||
| 2. Switch browser to "first wins". For two apps on one page, point users | ||
| to separate clients that are not bound with `init()` (see #24883). | ||
| 3. Make Next.js edge return the client. | ||
|
|
||
| ## Tests | ||
|
|
||
| A test that calls `init()` again without a reset now prints the warning. | ||
| Reset between tests with one of these: | ||
|
|
||
| - `getCurrentScope().setClient(undefined)`, or a helper that clears the | ||
| carrier (`getMainCarrier().__SENTRY__ = undefined`). | ||
| - `await Sentry.close()`. This also flushes, so it is slower. | ||
|
|
||
| The warning goes through `consoleSandbox`, which calls the method stored | ||
| in `originalConsoleMethods`. To assert on it, replace | ||
| `originalConsoleMethods.warn` with a mock. A spy on `console.warn` misses | ||
| it once the console integration is set up. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.