sentry-react-native icon indicating copy to clipboard operation
sentry-react-native copied to clipboard

Exception capture context is overwritten by native scope sync

Open lucas-zimerman opened this issue 3 years ago • 6 comments

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.

lucas-zimerman avatar Mar 18 '22 18:03 lucas-zimerman

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.

marandaneto avatar Mar 25 '22 10:03 marandaneto

I agree with this solution

lucas-zimerman avatar Mar 25 '22 16:03 lucas-zimerman

@krystofwoldrich have you tested that this is fixed after https://github.com/getsentry/sentry-react-native/pull/3170?

marandaneto avatar Jul 26 '23 10:07 marandaneto

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

krystofwoldrich avatar Sep 27 '23 15:09 krystofwoldrich