react-native-reanimated: [android][release] makeShareableCloneRecursive Call Stack Size Exceeded

Description

I’ll start by saying that I’ve spent many days on this and already looked at #4140, #4177, #4367 and #4475, and this is a separate issue. I also created an issue at expo/expo#23297 but I don’t think it’s expo’s fault because I could not reproduce this on their bare template. It’s worth noting that this does not occur on react-native@0.71 or expo@48.

The bug, for me, only occurs on the Android release build or the debug build with the JS Dev Mode disabled.

photo_2023-07-09_19-04-45

This gave me the idea to search for all __DEV__ flags within the installed version in my node_modules and flip all of them so that I can see the error in dev mode and get a nicer stacktrace. Here’s what I got:

photo_2023-07-09_19-07-21

This narrows down the problem quite well, and I found that the following patch solves my issue:

diff --git a/node_modules/react-native-reanimated/src/reanimated2/shareables.ts b/node_modules/react-native-reanimated/src/reanimated2/shareables.ts
index 4f358f4..8be3e2b 100644
--- a/node_modules/react-native-reanimated/src/reanimated2/shareables.ts
+++ b/node_modules/react-native-reanimated/src/reanimated2/shareables.ts
@@ -1,6 +1,6 @@
 import NativeReanimatedModule from './NativeReanimated';
-import { ShareableRef } from './commonTypes';
 import { shouldBeUseWeb } from './PlatformChecker';
+import { ShareableRef } from './commonTypes';
 import { registerWorkletStackDetails } from './errors';
 import { jsVersion } from './platform-specific/jsVersion';
 
@@ -146,8 +146,9 @@ export function makeShareableCloneRecursive<T>(
               value.__workletHash,
               value.__stackDetails
             );
-            delete value.__stackDetails;
           }
+          // TODO: This is the fixes Maximum call stack size exceeded error
+          delete value.__stackDetails;
           // to save on transferring static __initData field of worklet structure
           // we request shareable value to persist its UI counterpart. This means
           // that the __initData field that contains long strings represeting the

Obviously, this is not ideal and doesn’t seem like the official solution (why would stackDetails have anything to do with the fix?), so I’m hoping the team will have a better idea. Thanks for the great work!

Steps to reproduce

I could not reproduce this on the bare expo template, but I’m linking it anyway. I hope the patch I provided above will give some clue as to why it only happens on some projects.

  1. Install react-native-reanimated@3.3.0
  2. npx pod-install
  3. Importing the library using import 'react-native-reanimated' in App.tsx is enough to trigger the bug

Snack or a link to a repository

https://github.com/thespacemanatee/expo-dev-menu-repro/tree/repro/reanimated-makeShareableCloneRecursive-crash

Reanimated version

3.3.0

React Native version

0.72.1

Platforms

Android

JavaScript runtime

Hermes

Workflow

Expo bare workflow

Architecture

Paper (Old Architecture)

Build type

Release mode

Device

Android emulator

Device model

Pixel 7 Pro (Android 13) and Pixel 3a (Android 10)

Acknowledgements

Yes

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 29 (18 by maintainers)

Commits related to this issue

Most upvoted comments

@hirbod Just tried it and the issue still occurs 🤔 @tomekzaw I patched plugin.js with #4747 but the issue still occurs. Even just returning true doesn’t work, what gives?

Screenshot 2023-07-22 at 1 36 21 AM

I’m getting a similar issue on android in release mode, in development everything works fine.

Hey @thespacemanatee, thanks for reporting this issue, investigating the root cause as well as providing the workaround.

Could we get some advisory on whether the workaround above is viable, and why it might be happening?

Sure, we will investigate this issue once we finish investigating other issues that we’re looking into right now.

Bildschirmfoto 2023-07-18 um 04 21 26

PS: the reason why I said your syntax looks wrong to me is because you didn’t include --variant.

@tomekzaw this matches exactly my issue. So the uppercase R was the main problem for me. I retried couple of times and could reproduce this reliable.

@hirbod @thespacemanatee I think I found something. This is how we detect dev/release bundle in Reanimated Babel plugin:

https://github.com/software-mansion/react-native-reanimated/blob/5d4e02e0ded7e7acc2f23e277e68c592fe0e2e2b/plugin/src/utils.ts#L1-L6

If you pass Release as BABEL_ENV it will probably still generate dev bundle.

This issue is also blocking our release right now. The crash only occurs in release mode (–variant Release) and works fine in dev mode.

@hirbod sorry I was typing on my phone. This is very helpful, thanks. There isn’t really any documentation about configuring NODE_ENV so I guess we’ll have to pass that env variable with every build command?

@tomekzaw @hirbod If my variant is productionRelease, how would I go about this? The funny thing is that I tried npx expo run:android productionrelease and it works, and thereafter npx expo run:android productionRelease also works as well, until it stops working randomly. I think it’s related to expo but I’m not sure how to phrase it as an issue.

@tomekzaw the issue occurs again now and then and doesn’t seem to have a deterministic solution, @hirbod could you confirm if it works every time and on different machines?

I also think it’s weird that this issue only happens to Android. If it is what the team says it is, that the Android build cache needs to be cleared, then why does the solution in #4737 suggest fixes in the JS-land (solution 2)? And why does this affect Android only and not iOS as well? More investigations need to be done to find the root cause.

@tomekzaw, the only thing that might support your guess is that I can see the Expo CLI outputs env: load .env.development, even though I did pass the --variant Release parameter. There are no errors in SDK 48. Maybe something happened between these versions.

I need to do some quick research on how to dump the release bundle, never did that before.