Skip to content

Latest commit

 

History

History
79 lines (60 loc) · 5.19 KB

repeated-init.md

File metadata and controls

79 lines (60 loc) · 5.19 KB

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). Use isClientClosed() when a check for "already initialized" must tell these cases apart.

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, Astro 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.
TanStack Start 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.
Next.js server and edge in a request of withSentry (Cloudflare) Creates no client, because the client of withSentry handles the request. Returns undefined.

The shared warning lives in warnIfClientIsActive() in packages/core/src/sdk.ts. SDKs that build their client without initAndBind call it from @sentry/core.

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 getClient(). The Next.js, Remix, and Nuxt server guards also skip a client for which isClientClosed() is true, so client.close() followed by init() sets up a new client there.

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.