sentry-react-native
sentry-react-native copied to clipboard
Exception capture context is overwritten by native scope sync
Environment
How do you use Sentry? Sentry SaaS (sentry.io) Which SDK and version? e.g: Sentry React Native 3.3.3
Steps to Reproduce
Sample code:
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import * as Sentry from '@sentry/react-native';
Sentry.init({
dsn: 'https://[email protected]/5428561',
debug: true,
});
Sentry.captureException(new Error(`${Date.now()}: a test error occurred`),
context => context.addBreadcrumb({ message: 'error with breadcrumbs' }));
export default function App() {
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Expected Result
The exception captured with the breadcrumb error with breadcrumbs
Actual Result
Breadcrumb got removed https://sentry.io/organizations/sentry-sdks/issues/3114951206/?project=5428561&query=is%3Aunresolved&statsPeriod=1h
https://github.com/getsentry/sentry-react-native/blob/a085ec4ef6e9506cca568d8841e1e1bc8dfb63a2/src/js/wrapper.ts#L108-L110 This code doesnt differentiate from synced breadcrumbs and local breadcrumbs, resulting on local breadcrumbs getting removed when setting an isolated scope/context.
I believe the right solution here would be to align the way how we capture events, which is what iOS does, on RN, we call the native bridge to get all device context and assemble the event on RN with already all the context, so this won't need to be done on Android, after the envelope is written to the disk, and because of that, breadcrumbs won't be duplicated. This is a side-effect of this hacky solution of enriching the events on the Android side. We can patch the current version to fix it, but ideally, we'd do it properly.
I agree with this solution
@krystofwoldrich have you tested that this is fixed after https://github.com/getsentry/sentry-react-native/pull/3170?
@marandaneto I'll check it, the logic removing breadcrumbs was completely removed.
Although we are not removing the breadcrumbs anymore, the captureContext is not working due to native scope sync.
Device context integration overwrites the captureContext with the context from the native layer. This works under the assumption that all scope changes are synced to the native layer. But this is not true for the captureContex which operates on a scope clone that is not (and should not be) synchronized to the native layer.
https://github.com/getsentry/sentry-react-native/blob/2c394d14408a2b9bbb854413821e9b2400122f10/src/js/integrations/devicecontext.ts#L87-L93