Skip to content

Move DSN parsing off the main thread during init #5447

@runningcode

Description

@runningcode

Problem

retrieveParsedDsn() is called synchronously on the main thread during Sentry.init()preInitConfigurations() at Sentry.java:600. This triggers lazy Dsn construction, which performs URI parsing, regex-based org ID extraction, and key validation — all blocking the main thread.

Despite loadLazyFields() being designed to run on a background thread (submitted via executor at Sentry.java:359), the DSN is already parsed by the time that background task runs, so the lazy loading optimization doesn't help here.

Call chain on main thread

  1. SentryInitProvider.onCreate() (main thread, ContentProvider lifecycle)
  2. SentryAndroid.init()
  3. Sentry.init()preInitConfigurations() (line 309)
  4. options.retrieveParsedDsn() (line 600) — triggers lazy Dsn parsing on main thread
  5. Later: new SentryClient(options)RequestDetailsResolveroptions.retrieveParsedDsn() (line 35 of RequestDetailsResolver.java) — hits cached value, cheap

What Dsn constructor does

  • new URI(dsn) — Java's URI parsing (known to be slow)
  • Regex matching for org ID extraction
  • Public/secret key validation
  • URI normalization

Possible approaches

  1. Defer DSN parsing: The DSN string validation in preInitConfigurations could be simplified to a lightweight string check (non-empty, basic format), deferring full Dsn object construction to the background loadLazyFields() call
  2. Custom DSN parser: Replace java.net.URI with a faster custom parser (already noted in the project description)
  3. Move preInitConfigurations validation off main thread: Restructure init so validation happens after executor service is available

Files

  • sentry/src/main/java/io/sentry/Sentry.javapreInitConfigurations() at line 584
  • sentry/src/main/java/io/sentry/SentryOptions.javaretrieveParsedDsn() and loadLazyFields()
  • sentry/src/main/java/io/sentry/Dsn.java — constructor with URI parsing
  • sentry/src/main/java/io/sentry/RequestDetailsResolver.java — second call site (cached)

Metadata

Metadata

Assignees

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions