[NOJIRA] [FIX] [ANDROID] Fix Invariant Violation crash on native initialization#1251
[NOJIRA] [FIX] [ANDROID] Fix Invariant Violation crash on native initialization#1251marco-saia-datadog wants to merge 8 commits intodevelopfrom
Conversation
37faf1e to
67c9c4d
Compare
There was a problem hiding this comment.
Pull request overview
Fixes an Android crash during native-first initialization where DdSdkSessionStartedListener could call into the JS bridge before DatadogInternalReactBridge is registered, causing an uncaught Invariant Violation on older React Native architecture.
Changes:
- Add
isRnSdkInitializedgating to preventcallFunction("DatadogInternalReactBridge", ...)until JS-sideinitialize()has run. - Plumb a
isCalledFromJsflag throughDdSdkNativeInitialization.initialize()to distinguish JS vs native entrypoints and triggeronRnSdkInitialized()only for the JS path. - Add/adjust unit tests to cover ordering between session start,
setReactContext, and JS initialization.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkSessionStartedListener.kt | Adds isRnSdkInitialized state and gates old-arch bridge calls until JS initialization is confirmed. |
| packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkNativeInitialization.kt | Adds isCalledFromJs parameter and triggers listener “JS initialized” behavior only on JS-driven initialization. |
| packages/core/android/src/test/kotlin/com/datadog/reactnative/DdSdkSessionStartedListenerTest.kt | Adds tests for ordering between setReactContext and onRnSdkInitialized, plus updates existing tests for the new gate. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
67c9c4d to
a32da24
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Move isRnSdkInitialized to companion object so it survives invalidate() calls from onHostPause/onHostDestroy - Added initialization tests covering the isCalledFromJs branching - Minor comments improvements
|
Hi @marco-saia-datadog I have found this PR by total coincidence, We are facing the same exact error but For IOS devices only for some reason, Do u have any idea why could this happen? FYI we don't init on native side and we are on old ARC |
Hi @othmanAbdelaziz2001 👋 I am about to test through the full matrix, with and without the fix: Any iOS regression would most likely surface there :) In the meantime, feel free to share more details about your setup so that I can give it some extra attention. In particular:
|
Thanks a lot for your reply, will add some info here
We are init the Datadog in the root of app |
|
@marco-saia-datadog Also I am happy to support in debugging this, If there is a specific setup or steps i could that could help me debugging this issue in the App and walkthrough the init in IOS in the SDK could be helpful 🙏 |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
What does this PR do?
Fixes an
Invariant Violation: Module DatadogInternalReactBridge is not a registered callable modulecrash on Android when the SDK is initialized natively fromMainApplicationviaDdSdkNativeInitialization.initFromNative().Motivation
When using native-first initialization, a
DdSdkSessionStartedListenercaptures the RUM session ID before the React bridge is ready. WhenonHostResumefires andsetReactContextis called, the old code attempted to deliver the stored session ID viacallFunction("DatadogInternalReactBridge", ...). BecausecallFunctionis asynchronous on old arch, the nativetry-catchnever fires, the call queues onto the JS thread and throws once executed, propagating through React Native's global JS error handler and crashing the app.The root cause:
DatadogInternalReactBridgeis a JS-side callable module registered during JS bundle execution, which may not have completed by the timeonHostResumefires.Fix: gate all bridge calls on a new
isRnSdkInitializedflag, set only when JS callsinitialize(). At that point the callable module is guaranteed to be registered.Additional Notes
@datadog/mobile-react-native3.2.0 in a reproduction project before porting to the SDK source.DdSdkSessionStartedListener.ktReview checklist (to be filled by reviewers)