Skip to content
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

Reduce flush timeout to 4s on Android to avoid ANRs #2858

Merged
merged 3 commits into from
Jul 21, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

Breaking changes:
- Capture failed HTTP requests by default ([#2794](https://github.com/getsentry/sentry-java/pull/2794))
- Reduce flush timeout to 4s on Android to avoid ANRs ([#2858](https://github.com/getsentry/sentry-java/pull/2858))

### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
@SuppressWarnings("Convert2MethodRef") // older AGP versions do not support method references
final class AndroidOptionsInitializer {

static final long DEFAULT_FLUSH_TIMEOUT_MS = 4000;

static final String SENTRY_COMPOSE_GESTURE_INTEGRATION_CLASS_NAME =
"io.sentry.compose.gestures.ComposeGestureTargetLocator";

Expand Down Expand Up @@ -93,6 +95,9 @@ static void loadDefaultAndMetadataOptions(

options.setDateProvider(new SentryAndroidDateProvider());

// set a lower flush timeout on Android to avoid ANRs
options.setFlushTimeoutMillis(DEFAULT_FLUSH_TIMEOUT_MS);

ManifestMetadataReader.applyMetadata(context, options, buildInfoProvider);
initializeCacheDirs(context, options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ class AndroidOptionsInitializerTest {
assertTrue(innerLogger.get(loggerField) is AndroidLogger)
}

@Test
fun `flush timeout is set to Android specific default value`() {
fixture.initSut()
assertEquals(AndroidOptionsInitializer.DEFAULT_FLUSH_TIMEOUT_MS, fixture.sentryOptions.flushTimeoutMillis)
}

@Test
fun `flush timeout can be overridden`() {
fixture.initSut(configureOptions = {
flushTimeoutMillis = 1234
})
assertEquals(1234, fixture.sentryOptions.flushTimeoutMillis)
}

@Test
fun `AndroidEventProcessor added to processors list`() {
fixture.initSut()
Expand Down
6 changes: 5 additions & 1 deletion sentry/src/main/java/io/sentry/cache/EnvelopeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class EnvelopeCache extends CacheStrategy implements IEnvelopeCache {

public static final String STARTUP_CRASH_MARKER_FILE = "startup_crash";

private static final long SESSION_FLUSH_DISK_TIMEOUT_MS = 15000;

private final CountDownLatch previousSessionLatch;

private final @NotNull Map<SentryEnvelope, String> fileNameMap = new WeakHashMap<>();
Expand Down Expand Up @@ -429,7 +431,9 @@ public void discard(final @NotNull SentryEnvelope envelope) {
/** Awaits until the previous session (if any) is flushed to its own file. */
public boolean waitPreviousSessionFlush() {
try {
return previousSessionLatch.await(options.getFlushTimeoutMillis(), TimeUnit.MILLISECONDS);
// use fixed timeout instead of configurable options.getFlushTimeoutMillis() to ensure there's
// enough time to flush the session to disk
Copy link
Member Author

Choose a reason for hiding this comment

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

@romtsn if you've better reasoning here, please adapt the comment 😊

Copy link
Member

Choose a reason for hiding this comment

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

yeah, it's good!

return previousSessionLatch.await(SESSION_FLUSH_DISK_TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
options.getLogger().log(DEBUG, "Timed out waiting for previous session to flush.");
Expand Down