Repository navigation
fix(android): Persist hard-crash envelopes to disk before the process exits #6937
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
Draft
alwx
wants to merge
2
commits into
main
Choose a base branch
from
alwx/bugfix/6918
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
packages/core/android/src/test/java/io/sentry/react/RNSentryCaptureEnvelopeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| package io.sentry.react; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyBoolean; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import android.content.pm.PackageInfo; | ||
| import android.content.pm.PackageManager; | ||
| import android.util.Log; | ||
| import com.facebook.react.bridge.Promise; | ||
| import com.facebook.react.bridge.ReactApplicationContext; | ||
| import com.facebook.react.bridge.ReadableMap; | ||
| import io.sentry.Hint; | ||
| import io.sentry.IScopes; | ||
| import io.sentry.Scope; | ||
| import io.sentry.ScopeCallback; | ||
| import io.sentry.Sentry; | ||
| import io.sentry.SentryEnvelope; | ||
| import io.sentry.SentryEnvelopeItem; | ||
| import io.sentry.SentryEvent; | ||
| import io.sentry.SentryItemType; | ||
| import io.sentry.SentryOptions; | ||
| import io.sentry.Session; | ||
| import io.sentry.android.core.InternalSentrySdk; | ||
| import io.sentry.cache.EnvelopeCache; | ||
| import io.sentry.cache.IEnvelopeCache; | ||
| import io.sentry.transport.NoOpEnvelopeCache; | ||
| import io.sentry.vendor.Base64; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| /** | ||
| * A fatal JS error is rethrown by React Native as JavascriptException, which the SDK ignores, so | ||
| * the process dies right after captureEnvelope returns. The hard-crash path must write the envelope | ||
| * to disk on the calling thread instead of queueing it on the transport executor | ||
| * (https://github.com/getsentry/sentry-react-native/issues/6918). | ||
| */ | ||
| public class RNSentryCaptureEnvelopeTest { | ||
|
|
||
| @Rule public TemporaryFolder tmp = new TemporaryFolder(); | ||
|
|
||
| private RNSentryModuleImpl module; | ||
| private SentryOptions options; | ||
| private Scope scope; | ||
| private IScopes scopes; | ||
| private IEnvelopeCache cache; | ||
| private Promise promise; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| ReactApplicationContext reactContext = mock(ReactApplicationContext.class); | ||
| PackageManager packageManager = mock(PackageManager.class); | ||
| when(packageManager.getPackageInfo(anyString(), anyInt())).thenReturn(new PackageInfo()); | ||
| when(reactContext.getPackageManager()).thenReturn(packageManager); | ||
| when(reactContext.getPackageName()).thenReturn("com.test.app"); | ||
| module = new RNSentryModuleImpl(reactContext); | ||
|
|
||
| options = new SentryOptions(); | ||
| options.setDsn("https://key@sentry.io/1"); | ||
| options.setRelease("1.0.0"); | ||
| options.setCacheDirPath(tmp.getRoot().getAbsolutePath()); | ||
| cache = mock(IEnvelopeCache.class); | ||
| when(cache.storeEnvelope(any(), any())).thenReturn(true); | ||
| options.setEnvelopeDiskCache(cache); | ||
|
|
||
| scope = new Scope(options); | ||
| scopes = mock(IScopes.class); | ||
| when(scopes.getOptions()).thenReturn(options); | ||
| doAnswer( | ||
| invocation -> { | ||
| ((ScopeCallback) invocation.getArgument(0)).run(scope); | ||
| return null; | ||
| }) | ||
| .when(scopes) | ||
| .configureScope(any(ScopeCallback.class)); | ||
|
|
||
| promise = mock(Promise.class); | ||
| } | ||
|
|
||
| private static ReadableMap hardCrashed(final boolean value) { | ||
| final ReadableMap map = mock(ReadableMap.class); | ||
| when(map.hasKey("hardCrashed")).thenReturn(true); | ||
| when(map.getBoolean("hardCrashed")).thenReturn(value); | ||
| return map; | ||
| } | ||
|
|
||
| private String eventEnvelope() throws Exception { | ||
| final SentryEnvelope envelope = | ||
| SentryEnvelope.from(options.getSerializer(), new SentryEvent(), null); | ||
| final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| options.getSerializer().serialize(envelope, out); | ||
| return Base64.encodeToString(out.toByteArray(), Base64.DEFAULT); | ||
| } | ||
|
|
||
| private static List<SentryItemType> itemTypes(final SentryEnvelope envelope) { | ||
| final List<SentryItemType> types = new ArrayList<>(); | ||
| for (final SentryEnvelopeItem item : envelope.getItems()) { | ||
| types.add(item.getHeader().getType()); | ||
| } | ||
| return types; | ||
| } | ||
|
|
||
| @Test | ||
| public void hardCrashStoresEnvelopeOnCallingThreadAndDoesNotQueueIt() throws Exception { | ||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| sentry.when(Sentry::getCurrentScopes).thenReturn(scopes); | ||
|
|
||
| module.captureEnvelope(eventEnvelope(), hardCrashed(true), promise); | ||
|
|
||
| final ArgumentCaptor<SentryEnvelope> stored = ArgumentCaptor.forClass(SentryEnvelope.class); | ||
| verify(cache).storeEnvelope(stored.capture(), any(Hint.class)); | ||
| assertEquals(List.of(SentryItemType.Event), itemTypes(stored.getValue())); | ||
| verify(scopes, never()).captureEnvelope(any()); | ||
| internal.verify(() -> InternalSentrySdk.captureEnvelope(any(), anyBoolean()), never()); | ||
| verify(promise).resolve(true); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void hardCrashEndsSessionAsCrashedAndAttachesIt() throws Exception { | ||
| scope.startSession(); | ||
| final File sessionFile = EnvelopeCache.getCurrentSessionFile(options.getCacheDirPath()); | ||
| assertTrue(sessionFile.getParentFile().mkdirs()); | ||
| assertTrue(sessionFile.createNewFile()); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<Log> ignored = mockStatic(Log.class)) { | ||
| sentry.when(Sentry::getCurrentScopes).thenReturn(scopes); | ||
|
|
||
| module.captureEnvelope(eventEnvelope(), hardCrashed(true), promise); | ||
|
|
||
| final ArgumentCaptor<SentryEnvelope> stored = ArgumentCaptor.forClass(SentryEnvelope.class); | ||
| verify(cache).storeEnvelope(stored.capture(), any(Hint.class)); | ||
| assertEquals( | ||
| List.of(SentryItemType.Event, SentryItemType.Session), itemTypes(stored.getValue())); | ||
|
|
||
| Session attached = null; | ||
| for (final SentryEnvelopeItem item : stored.getValue().getItems()) { | ||
| if (item.getHeader().getType() == SentryItemType.Session) { | ||
| attached = | ||
| options | ||
| .getSerializer() | ||
| .deserialize( | ||
| new InputStreamReader(new ByteArrayInputStream(item.getData()), UTF_8), | ||
| Session.class); | ||
| } | ||
| } | ||
| assertNotNull(attached); | ||
| assertEquals(Session.State.Crashed, attached.getStatus()); | ||
| assertEquals(1, attached.errorCount()); | ||
| assertNotNull(attached.getTimestamp()); | ||
| assertNull(scope.withSession(session -> {})); | ||
| assertFalse(sessionFile.exists()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void hardCrashFallsBackToQueueWhenStoreFails() throws Exception { | ||
| when(cache.storeEnvelope(any(), any())).thenReturn(false); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<Log> ignored = mockStatic(Log.class)) { | ||
| sentry.when(Sentry::getCurrentScopes).thenReturn(scopes); | ||
|
|
||
| module.captureEnvelope(eventEnvelope(), hardCrashed(true), promise); | ||
|
|
||
| verify(cache).storeEnvelope(any(), any(Hint.class)); | ||
| verify(scopes).captureEnvelope(any(SentryEnvelope.class)); | ||
| verify(promise).resolve(true); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void hardCrashUsesQueuedPathWithoutDiskCache() throws Exception { | ||
| options.setEnvelopeDiskCache(NoOpEnvelopeCache.getInstance()); | ||
| final String rawBytes = eventEnvelope(); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| sentry.when(Sentry::getCurrentScopes).thenReturn(scopes); | ||
|
|
||
| module.captureEnvelope(rawBytes, hardCrashed(true), promise); | ||
|
|
||
| internal.verify( | ||
| () -> | ||
| InternalSentrySdk.captureEnvelope( | ||
| eq(Base64.decode(rawBytes, Base64.DEFAULT)), eq(false))); | ||
| verify(promise).resolve(true); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void nonFatalEventUsesQueuedPath() throws Exception { | ||
| final String rawBytes = eventEnvelope(); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| sentry.when(Sentry::getCurrentScopes).thenReturn(scopes); | ||
|
|
||
| module.captureEnvelope(rawBytes, hardCrashed(false), promise); | ||
|
|
||
| verify(cache, never()).storeEnvelope(any(), any()); | ||
| internal.verify( | ||
| () -> | ||
| InternalSentrySdk.captureEnvelope( | ||
| eq(Base64.decode(rawBytes, Base64.DEFAULT)), eq(true))); | ||
| verify(promise).resolve(true); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current session file is deleted even when hard-crash envelope persistence fails
The hard-crash path deletes the current-session file before storing the repackaged envelope. If
cache.storeEnvelopefails, the envelope is only queued (or a thrown exception is caught by the outer handler), while the session file is already gone. Preserve the session file unless the envelope is successfully stored to disk, so the next launch can still report the session as abnormal.Evidence
items, then its current-session file is deleted beforecache.storeEnvelopeis called.storeEnvelopereturns false, the code queues the envelope throughscopes.captureEnvelope; the enclosing capture path can then exit while the process is about to terminate.Identified by Warden · find-bugs · 638-86W