Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Performance

- Defer starting Session Replay off the `Sentry.init` critical path to reduce app start time ([#5904](https://github.com/getsentry/sentry-java/pull/5904))

### Fixes

- Clear contexts when calling `Scope.clear()` ([#5902](https://github.com/getsentry/sentry-java/pull/5902))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.os.SystemClock;
import android.os.Trace;
Expand Down Expand Up @@ -203,7 +205,11 @@ public static void init(
scopes.startSession();
}
}
scopes.getOptions().getReplayController().start();
// Defer starting replay off the SDK init critical path so it doesn't add to app start
// time. start() is idempotent, so the later start() from the app lifecycle integration
// (once the first activity is in foreground) is a no-op if this one ran first.
new Handler(Looper.getMainLooper())
.post(() -> scopes.getOptions().getReplayController().start());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Replay start deferred past app onCreate

Medium Severity

Handler.post only runs after the current main-looper message finishes. Auto-init runs inside handleBindApplication, so that message also includes Application.onCreate. Session Replay therefore starts only after Application.onCreate, and startup crashes there no longer get a replayId or recording. Previously start() ran synchronously during init, before onCreate continued.

Fix in Cursorย Fix in Web

Reviewed by Cursor Bugbot for commit 9495885. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think this matters because session replays for startup crashes so early won't have anything visible anyways.

}
} catch (IllegalAccessException e) {
logger.log(SentryLevel.FATAL, "Fatal error during SentryAndroid.init(...)", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ class SentryAndroidTest {
@Config(sdk = [26])
fun `init starts session replay if app is in foreground`() {
initSentryWithForegroundImportance(true) { _ ->
// replay start is posted to the main looper, so drain it before asserting

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like we should assert that isRecording() is false before draining the main looper b/c we don't want this test to continue to pass if we regress back to synchronous init.

Shadows.shadowOf(Looper.getMainLooper()).idle()
assertTrue(Sentry.getCurrentHub().options.replayController.isRecording())
}
}
Expand Down
Loading