Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Method addObserver must be called on the main thread #520

Closed
4 of 8 tasks
lewisf opened this issue Aug 14, 2020 · 26 comments
Closed
4 of 8 tasks

Method addObserver must be called on the main thread #520

lewisf opened this issue Aug 14, 2020 · 26 comments
Labels
bug Something isn't working dependencies Pull requests that update a dependency file question Further information is requested

Comments

@lewisf
Copy link

lewisf commented Aug 14, 2020

Platform:

  • iOS
  • Android

SDK:

  • @sentry/react-native (>= 1.0.0)
  • react-native-sentry (<= 0.43.2)

RN SDK version: 1.6.3
Android SDK version: 2.2.0

react-native version: 0.62.17

Are you using Expo?

  • Yes
  • No

Are you using sentry.io or on-premise?

  • sentry.io (SaaS)
  • on-premise

If you are using sentry.io, please post a link to your issue so we can take a look:

https://sentry.io/organizations/persona-identities-inc/issues/1839866967/?project=5378601&query=is%3Aunresolved

Configuration:

(@sentry/react-native)

Sentry.init({
  dsn: 'https://...@sentry.io/...'
});

I have following issue:

I went through barebones setup steps and am getting this error. Commenting out the Sentry.init gets rid of it. Stack trace seems to indicate it's all happening within the Sentry library but I'm not sure if I'm doing something wrong.

Steps to reproduce:

  • New project on React Native 0.62.17
  • Install Sentry
  • Launch in Android Emulator using Pixel 2 API 29
  • Red screen with error

Actual result:

image

Expected result:

No error.

@jennmueng
Copy link
Member

Hmm this error is coming from the Android SDK, can you please indicate the SDK version and React Native version that you're using? Thanks.

@lewisf
Copy link
Author

lewisf commented Aug 17, 2020

@jennmueng, thanks for the quick response!

react-native is 0.62.17

"@sentry/react-native@1.6.3":
  version "1.6.3"
  resolved "https://registry.yarnpkg.com/@sentry/react-native/-/react-native-1.6.3.tgz#80c68d6bc7f10bd4b7eb12e1b028620a1fadbcc5"
  integrity sha512-0nkPV/QWEXWQb67XKy4HY9x8zLEURzBT7xaal2KUGRWu2JGObfy5eYk8+k+XsGLVAMfelCkcLQWNneUY80reng==
  dependencies:
    "@sentry/browser" "^5.19.0"
    "@sentry/core" "^5.19.0"
    "@sentry/hub" "^5.19.0"
    "@sentry/integrations" "^5.19.0"
    "@sentry/types" "^5.19.0"
    "@sentry/utils" "^5.19.0"
    "@sentry/wizard" "^1.1.4"

@jennmueng
Copy link
Member

Ok so the React Native SDK @ 1.6.3 would depend on sentry-android:2.2.0. I'll move this issue over to our Android SDK as I think my colleagues over there would be more knowledgable to help you out with this error.

@jennmueng jennmueng transferred this issue from getsentry/sentry-react-native Aug 18, 2020
@lewisf
Copy link
Author

lewisf commented Aug 18, 2020

Thanks. Is there anything else I can do to help with debugging?

@haelsisy
Copy link

I have the same issue with react-native

    "react": "16.11.0",
    "react-native": "0.62.2",
    "@sentry/react-native": "^1.7.1"

@marandaneto
Copy link
Contributor

hey thanks for reporting this, quick question:

are you initing the Android SDK manually by yourself or are you letting the Sentry RN SDK do the job automatically?

If you are letting the Sentry RN SDK do the job, on which life cycle (which class/method) of the RN App. are ou initing the Sentry RN SDK? thanks :)

@marandaneto marandaneto added the question Further information is requested label Aug 24, 2020
@marandaneto
Copy link
Contributor

marandaneto commented Aug 24, 2020

@lewisf @zumaawad what version of the androidx.lifecycle/lifecycle-process lib are you working with btw? are you referencing directly or is it a transitive dependency? could you track this down, please? 2.2.0 doesn't have this check enforceMainThreadIfNeeded but maybe an older version has it.

@marandaneto marandaneto added the dependencies Pull requests that update a dependency file label Aug 24, 2020
@marandaneto
Copy link
Contributor

as you can see, this has been changed in the lib and ideally, we'd add a main thread checker to avoid this problem.

1st commit: https://android.googlesource.com/platform/frameworks/support/+/1a587cf583809379add635a9047ded82d44306c8
2nd commit: https://android.googlesource.com/platform/frameworks/support/+/5675f1a128d0c3878ba97e74ef3aad521071a63b
3nd commit: https://android.googlesource.com/platform/frameworks/support/+/a9a8ffd7d455ff81f81f5c71bb300b6acab823ba

@marandaneto
Copy link
Contributor

as a workaround, you can disable the lifecycle breadcrumbs for now:

https://docs.sentry.io/platforms/android/#default-context-for-android

<application>
    <!--     To disable the app lifecycle breadcrumbs integration-->
    <meta-data android:name="io.sentry.breadcrumbs.app-lifecycle" android:value="false" />
</application>

@haelsisy
Copy link

@marandaneto I'm using RN SDK and I initiate in app.js

@haelsisy
Copy link

@marandaneto Still the same

E/unknown:ReactNative: Exception in native call java.lang.IllegalStateException: Method addObserver must be called on the main thread at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172) at io.sentry.android.core.AppLifecycleIntegration.register(AppLifecycleIntegration.java:53) at io.sentry.core.Sentry.init(Sentry.java:159) at io.sentry.core.Sentry.init(Sentry.java:96) at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:59) at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:44) at io.sentry.RNSentryModule.startWithOptions(RNSentryModule.java:74) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loop(Looper.java:214) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226) at java.lang.Thread.run(Thread.java:919)

@marandaneto
Copy link
Contributor

@marandaneto Still the same

E/unknown:ReactNative: Exception in native call java.lang.IllegalStateException: Method addObserver must be called on the main thread at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172) at io.sentry.android.core.AppLifecycleIntegration.register(AppLifecycleIntegration.java:53) at io.sentry.core.Sentry.init(Sentry.java:159) at io.sentry.core.Sentry.init(Sentry.java:96) at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:59) at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:44) at io.sentry.RNSentryModule.startWithOptions(RNSentryModule.java:74) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loop(Looper.java:214) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226) at java.lang.Thread.run(Thread.java:919)

ups sorry, you also have to disable the session tracking as it depends on this 3rd party lib.

https://docs.sentry.io/platforms/react-native/#release-health

so if enableAutoSessionTracking is enabled, you've to disable it too, sorry about that.

@haelsisy
Copy link

haelsisy commented Aug 24, 2020

@marandaneto Its working now with
"@sentry/react-native": "^1.6.2"
and
enableAutoSessionTracking is disabled
and
<application> <!-- To disable the app lifecycle breadcrumbs integration--> <meta-data android:name="io.sentry.breadcrumbs.app-lifecycle" android:value="false" /> </application>

@marandaneto
Copy link
Contributor

@zumaawad good to know, mind answering the other question so we could choose the best solution? life cycle in the app.js file etc, thanks.

@billnbell
Copy link

billnbell commented Aug 24, 2020

I tried Sentry.init() in App.js and in index.android.js. Nothing helps. How about:
"@sentry/react-native": "^1.7.1" ?

@haelsisy
Copy link

haelsisy commented Aug 24, 2020

@zumaawad good to know, mind answering the other question so we could choose the best solution? life cycle in the app.js file etc, thanks.

@marandaneto In componentDidMount

@marandaneto
Copy link
Contributor

marandaneto commented Aug 24, 2020

@zumaawad have you tried right before the App ReactNode?

eg:

import * as Sentry from '@sentry/react-native';

Sentry.init({ 
  dsn: '...',
  debug: true
});

const App: () => React$Node = () => {
  return (...)
}

@haelsisy
Copy link

@marandaneto I tired that too and didn’t work without workaround you mentioned before

@marandaneto
Copy link
Contributor

@zumaawad ok, I will check if something has changed in the way how RN inits, we already have a PR to fix the issue anyway, getting feedback and will release a patch soon, thanks.

@marandaneto marandaneto added the bug Something isn't working label Aug 24, 2020
@haelsisy
Copy link

@marandaneto I think your PR #525 will fix this

@marandaneto
Copy link
Contributor

marandaneto commented Aug 24, 2020

@zumaawad indeed, but I wanna find out the source of this issue, as we've not changed anything on this repo about that, its either a change on sentry-react-native, on RN itself or you all have a transitive dependency of androidx.lifecycle/lifecycle-process that uses the main thread requirement, the latest is the most probably.

@haelsisy
Copy link

@marandaneto The fun fact is: we didn’t change anything from our side too 😄
Yes I think its related to android dependencies update

@marandaneto
Copy link
Contributor

the Android SDK is published https://github.com/getsentry/sentry-android/releases/tag/2.3.1

you could upgrade this dependency in your build.gradle file or wait for a new version of the RN SDK which should come very soon as well.

path to upgrade the Android SDK:

something like: {project}/node_modules/@sentry/react-native/android/build.gradle
look for io.sentry:sentry-android

If you have applied the workarounds mentioned before, remember to revert it, so you don't lose the session tracking feature nor the app lifecycle breadcrumbs :)

@marandaneto
Copy link
Contributor

RN SDK is also published https://github.com/getsentry/sentry-react-native/releases/tag/1.7.2

@haelsisy
Copy link

@marandaneto Thank you

@lewisf
Copy link
Author

lewisf commented Aug 26, 2020

@marandaneto thanks!! appreciate the quick turnaround on this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working dependencies Pull requests that update a dependency file question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants