Skip to content

[NOJIRA] [FIX] [ANDROID] Fix Invariant Violation crash on native initialization#1251

Open
marco-saia-datadog wants to merge 8 commits intodevelopfrom
marcosaia/fix/native-initialization-crash
Open

[NOJIRA] [FIX] [ANDROID] Fix Invariant Violation crash on native initialization#1251
marco-saia-datadog wants to merge 8 commits intodevelopfrom
marcosaia/fix/native-initialization-crash

Conversation

@marco-saia-datadog
Copy link
Copy Markdown
Member

What does this PR do?

Fixes an Invariant Violation: Module DatadogInternalReactBridge is not a registered callable module crash on Android when the SDK is initialized natively from MainApplication via DdSdkNativeInitialization.initFromNative().

Motivation

When using native-first initialization, a DdSdkSessionStartedListener captures the RUM session ID before the React bridge is ready. When onHostResume fires and setReactContext is called, the old code attempted to deliver the stored session ID via callFunction("DatadogInternalReactBridge", ...). Because callFunction is asynchronous on old arch, the native try-catch never 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: DatadogInternalReactBridge is a JS-side callable module registered during JS bundle execution, which may not have completed by the time onHostResume fires.

Fix: gate all bridge calls on a new isRnSdkInitialized flag, set only when JS calls initialize(). At that point the callable module is guaranteed to be registered.

Additional Notes

  • Validated via patch-package against @datadog/mobile-react-native 3.2.0 in a reproduction project before porting to the SDK source.
  • Improved documentation of DdSdkSessionStartedListener.kt

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)
  • If this PR is auto-generated, please make sure also to manually update the code related to the change

@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/fix/native-initialization-crash branch from 37faf1e to 67c9c4d Compare April 15, 2026 14:38
@marco-saia-datadog marco-saia-datadog marked this pull request as ready for review April 15, 2026 15:39
@marco-saia-datadog marco-saia-datadog requested a review from a team as a code owner April 15, 2026 15:39
Copilot AI review requested due to automatic review settings April 15, 2026 15:39
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 isRnSdkInitialized gating to prevent callFunction("DatadogInternalReactBridge", ...) until JS-side initialize() has run.
  • Plumb a isCalledFromJs flag through DdSdkNativeInitialization.initialize() to distinguish JS vs native entrypoints and trigger onRnSdkInitialized() 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.

@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/fix/native-initialization-crash branch from 67c9c4d to a32da24 Compare April 23, 2026 07:30
Copilot AI review requested due to automatic review settings April 23, 2026 07:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@othmanAbdelaziz2001
Copy link
Copy Markdown

othmanAbdelaziz2001 commented Apr 23, 2026

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?
we are using "@datadog/mobile-react-native": "^2.6.6",

FYI we don't init on native side and we are on old ARC

@marco-saia-datadog
Copy link
Copy Markdown
Member Author

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? we are using "@datadog/mobile-react-native": "^2.6.6",

Hi @othmanAbdelaziz2001 👋 I am about to test through the full matrix, with and without the fix:

{native, JS} × {old, new} × {iOS, Android} × {RN 0.80 / SDK v3, RN 0.72 / SDK v2}.

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:

  • Old Architecture or New Architecture?
  • Pure React Native or Expo?
  • Datadog Native Initialization (initFromNative API) or JS Initialization?

@othmanAbdelaziz2001
Copy link
Copy Markdown

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? we are using "@datadog/mobile-react-native": "^2.6.6",

Hi @othmanAbdelaziz2001 👋 I am about to test through the full matrix, with and without the fix:

{native, JS} × {old, new} × {iOS, Android} × {RN 0.80 / SDK v3, RN 0.72 / SDK v2}.

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:

  • Old Architecture or New Architecture?
  • Pure React Native or Expo?
  • Datadog Native Initialization (initFromNative API) or JS Initialization?

Thanks a lot for your reply, will add some info here

  1. React native version we are using is 0.77.3 (We are still on the old Architecture)
  2. The project is Pure React native
  3. And we are using jS Initialization

We are init the Datadog in the root of app

` };
  const datadogFirebaseRemoteConfig = useMemo(() => {
    return datadogUserSpecificEnabled
      ? FirebaseRemoteConfigOption.UserSpecificConfig
      : FirebaseRemoteConfigOption.GenericConfig;
  }, [datadogUserSpecificEnabled]);

  const loggingVerbosity = shouldEnableDatadogDebugConfig
    ? LogVerbosity.Debug
    : LogVerbosity.Info;

  useEffect(() => {
    datadogEnabled &&
      initializeDatadog(
        navigationRef,
        datadogFirebaseRemoteConfig,
        loggingVerbosity,
      );
  }, [datadogEnabled, datadogFirebaseRemoteConfig, loggingVerbosity]);`

@othmanAbdelaziz2001
Copy link
Copy Markdown

@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 🙏

Copilot AI review requested due to automatic review settings April 23, 2026 15:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants