Skip to content

ref(core): Add SentryRuntime for the SDK's own collaborators - #6830

Draft
runningcode wants to merge 1 commit into
mainfrom
no/sentryruntime-foundation
Draft

runningcode wants to merge 1 commit into
mainfrom
no/sentryruntime-foundation

Conversation

@runningcode

@runningcode runningcode commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

📜 Description

Adds io.sentry.internal.SentryRuntime, an immutable holder for the collaborators the SDK owns rather than the customer: the serializer, the executor and the envelope disk cache. It is built once per Sentry.init. Nothing reads from it yet; follow-up PRs move each collaborator's readers to constructor injection.

  • LegacyOptionsBridge builds the runtime from what is on SentryOptions and copies it back, so code that still reads the options sees the same instances.
  • InternalIntegration lets Sentry.init hand the runtime to SDK integrations.
  • Sentry.init and SentryAndroid.init take a SentryRuntime.Builder through new internal overloads. Hybrid SDKs get InternalSentrySdk.init(context, logger, (options, runtime) -> …) and InternalSentrySdk.getRuntime().
// Sentry.init
  initConfigurations(options);                 // still creates the EnvelopeCache on the options
  bindClient(new SentryClient(options));
- if (options.getExecutorService().isClosed())
-   options.setExecutorService(new SentryExecutorService(options));
+ runtime = LegacyOptionsBridge.buildRuntime(runtimeBuilder, options);
+ SentryRuntime.setCurrent(runtime);           // for hybrid SDKs only
  for (integration : options.getIntegrations())
+   if (integration instanceof InternalIntegration) integration.register(scopes, options, runtime);
+   else
      integration.register(scopes, options);

// LegacyOptionsBridge.buildRuntime(builder, options)
  builder.lazySerializer(() -> options.getSerializer());        // the same instance, created lazily
  if (!builder.hasExecutorService()) builder.executorService(executorOrNew(options));
  if (options.cache is not NoOp) builder.envelopeDiskCache(options.cache);  // a customer setter wins
  runtime = builder.build(options);                             // defaults for anything unset
  options.setExecutorService(runtime.executor);                 // the getters on the options
  options.setEnvelopeDiskCache(runtime.cache);                  // see the same objects

No behavior change: the runtime is built where init used to replace a closed executor, and it reuses the disk cache initConfigurations already created.

💡 Motivation and Context

Part of JAVA-817. Customers should only configure SentryOptions. Moving the SDK's internals off it lets 9.0 remove them from the public API.

💚 How did you test it?

SentryRuntimeTest, plus the existing sentry and sentry-android-core unit tests.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

Separate PRs move executor shutdown, the integrations, the disk cache and the init-time executor users onto the runtime. A last PR deprecates the SentryOptions accessors.

🤖 Generated with Claude Code

#skip-changelog

SentryRuntime holds the collaborators the SDK owns rather than the
customer (serializer, executor, envelope disk cache). Sentry.init builds
it once, through LegacyOptionsBridge, from what is on SentryOptions and
copies it back, so existing readers see the same instances. Nothing
reads from it yet; follow-up PRs move the readers to constructor
injection before the SentryOptions accessors get deprecated.

Part of JAVA-817

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@linear-code

linear-code Bot commented Dec 1, 2026

Copy link
Copy Markdown

JAVA-817

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

🚨 Detected changes in high risk code 🚨

High-risk code has higher potential to break the SDK and may be hard to test. To prevent severe bugs, apply the rollout process for releasing such changes and be extra careful when changing and reviewing these files:

  • sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 4b9b580.

Main concerns: reach, public contract, host performance.

Rules that block low also fired:

  • R4: a public API record changed: sentry-android-core/api/sentry-android-core.api, sentry/api/sentry.api

This label is a hint for reviewers, not an approval.

@sentry

sentry Bot commented Dec 1, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.66.0 (1) release

⚙️ sentry-android Build Distribution Settings

Comment on lines +64 to +69
public static void init(
final @NotNull Context context,
final @NotNull ILogger logger,
final @NotNull OptionsAndRuntimeConfiguration configuration) {
SentryAndroid.init(context, logger, configuration);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hybrid init with profiling leaks the executor created before the runtime override

When a hybrid config supplies runtime.executorService(...) and enables profiling, activate() creates an executor on options and initConfigurations() submits profiling cleanup to it before the runtime replaces it; Sentry.close() closes only the replacement, leaving the original worker alive.

Evidence
  • InternalSentrySdk.init passes the builder to the configuration callback, so a hybrid can set runtime.executorService(...) without setting the options executor.
  • Sentry.init calls options.activate() before initConfigurations() and builds the runtime afterward. With the options executor unset, activate() creates a SentryExecutorService; when profiling is enabled, initConfigurations() submits cleanup to it. Android initialization supplies a cache path, so the profiling-traces path is available.
  • LegacyOptionsBridge.buildRuntime then replaces the options executor with the builder’s executor without closing the previous one. The default SentryExecutorService does not time out its core worker, and Sentry.close() closes only the executor currently on options.
Also found at 1 additional location
  • sentry/src/main/java/io/sentry/internal/LegacyOptionsBridge.java:32-43

Identified by Warden · code-review · CXK-F6A

Comment on lines +36 to +39
final @NotNull IEnvelopeCache envelopeDiskCache = options.getEnvelopeDiskCache();
if (!(envelopeDiskCache instanceof NoOpEnvelopeCache)) {
builder.envelopeDiskCache(envelopeDiskCache);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Builder envelopeDiskCache is discarded when cacheDirPath is set

A cache set on SentryRuntime.Builder is overwritten whenever initConfigurations has already replaced options' NoOp with EnvelopeCache for a non-null cacheDirPath, so hybrid/platform caches never reach options or the runtime.

Evidence
  • Sentry.init runs initConfigurations(options) before LegacyOptionsBridge.buildRuntime().
  • initConfigurations does options.setEnvelopeDiskCache(EnvelopeCache.create(options)) when cacheDirPath != null and the options cache is still NoOpEnvelopeCache.
  • buildRuntime then treats any non-NoOp options cache as authoritative and calls builder.envelopeDiskCache(envelopeDiskCache), replacing a cache already set on the builder.
  • The result is copied back with options.setEnvelopeDiskCache(runtime.getEnvelopeDiskCache()), so options readers also keep the initConfigurations instance; the test that expects a platform builder cache to win only covers options that still hold NoOp.

Identified by Warden · code-review · FQU-9LK

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

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant