hermes: RangeError: Maximum callstack size exceeded with many modules in single bundle

Bug Description

We are unable to open our full app in development, which runs a single bundle with over 40,000 modules. In production we are using Re.Pack which enables code-splitting and does not run into this problem.

In this PR the default JS register count was doubled - https://github.com/facebook/hermes/pull/923. Sadly we are hitting the same error again.

Note that the error is not reproducible using JSC in our reproduction repo (see below).

  • I have run gradle clean and confirmed this bug does not occur with JSC
  • The issue is reproducible with the latest version of React Native.

Environment

Hermes git revision (if applicable): unknown, tied to RN versions below React Native version: 0.72.12 (our app), 0.74.2 (reproduction app) OS: OSX Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64): unknown, this occurs in the iOS simulator and android emulator

Steps To Reproduce

I’ve been able to mostly* reproduce it in this repo - https://github.com/mikeduminy/rn-reproducer.

* The error we’re experiencing in our app is "RangeError: Maximum callstack size exceeded - no stack" whereas the error in this reproduction repo is simply "RangeError: Maximum callstack size exceeded" so I’m not 100% certain it has the same cause.

Steps to reproduce:

  1. Clone above repo and install dependencies
  2. Generate 50,000 modules to simulate a large app (note: no lazy imports)
  3. Run the app on iOS simulator and Android emulator
  4. Observe the red screen (note, you may need to close and re-open the app to see this)

342809230-3e23f51c-936e-459b-abec-3207ec2737f4 Screenshot_1719331985

The Expected Behavior

A unhandled JS Exception should be thrown but it would not be a call stack size exceeded error.

In order to test if the callstack resulted from executing the entrypoint module of the bundle (the last few lines of the bundle) I added a forced error just before that.

serializer: {
    getRunModuleStatement: moduleId =>
      // If we see this error, it means the module was fully parsed
      // If we see another error, it means the problem occurred during the bundle parsing
      `throw new Error("before module execution: ${JSON.stringify(
        moduleId,
      )}"); __r(${JSON.stringify(moduleId)});`,
  },

If you see this error then hermes has successfully executed the bundle. If you see the max call stack error then this error was never even reached and that is the problem we are facing in our app - the bundle is not fully executed / loaded.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (20 by maintainers)

Commits related to this issue

Most upvoted comments

I found the problem, the size of the global function is causing the register allocator to hit a memory limit and fall back to fast but very inefficient allocation. As a result the global function ends up singlehandedly overflowing the stack. We’ll work on a fix.

what is the purpose of the second condition? It seems like that might be what we are hitting in this case.

@mikeduminy Yep, exactly, your bundle is hitting this case. In dev-mode, the compiler is usually running on a phone, where memory is relatively limited. The full register allocator can be quite memory intensive (more than linear in the size of the function), so we have this limit to avoid OOMing when we allocate (we have seen OOMs because of this in practice). The current limit is fairly conservative though, and as @tmikov said, it should be configurable depending on the use case.

That said, we have a fix in 6b69a06fc4a498ac467b590985515100c63313c3 that improves the code quality of the fast pass significantly. In the case of your bundle, it reduces the register usage for the global function from 402916 to 39. That fix has currently landed in the static_h branch, and we’ll port it to main shortly.

I have added both working and broken bundles for Android and iOS here. The difference between the working and broken bundles is a single additional module.

I also conducted some analysis of each bundle only to discover that the breaking point for both platforms is when the number of modules exceeds 50357.

@tmikov it looks like that file was initially added with FileCheckOrRegen, but this is the first time we actually ran update-lit to regenerate tests, so it ended up getting swept up in this diff.

The only change to that file is this added line: // Auto-generated content below. Please do not modify manually. https://github.com/facebook/hermes/commit/20b13b96f49c6819f6090f17a5ec29d3faec45c6#diff-97c6e0145c7c283475c236bc7a3f9987026beff83bed44f8b4be9e5a7cf6c6b6R14

@cortinico interesting. It appears that test/Parser/flow/declare-module-import-allowed.js was incorrectly included in the diff. @neildhar any idea why?

Seems like ignoring changes to that file should work?

It is a bug fix, so RN should be willing to pick it.

It has already landed in main in 20b13b96f49c6819f6090f17a5ec29d3faec45c6 and you can request a pick in the RN release discussions. The changes are relatively isolated so they should be safe to pick.

@mikeduminy Not yet at least, hopefully the bytecode output of the bundle is enough to pinpoint what is going on.