Skip to content

[iOS] Hermes sampling profiler crashes (EXC_BAD_ACCESS in walkRuntimeStack) when JS runs synchronously on the main thread #60598

Backlog

Description

@sbs44

Description

On iOS, the Hermes sampling profiler can crash the app with EXC_BAD_ACCESS in hermes::vm::SamplingProfiler::walkRuntimeStack when it samples while React Native is executing JS synchronously on the main thread.

  • React Native registers the runtime for profiling once, on the JS thread: HermesInstance.cpp → unstable_initializeOnJsThread() → hermesRuntime_.registerForProfiling().
  • When a component dispatches with EventEmitter::experimental_flushSync (React Native's own VirtualView does), the event beat runs the runtime on the main thread instead (AppleEventBeat::activityDidChange → EventBeat::induce → RuntimeScheduler_Modern::executeNowOnTheSameThread → executeSynchronouslyOnSameThread_CAN_DEADLOCK), while the JS thread is parked on a future.
  • Nothing in RuntimeExecutorSyncUIThreadUtils.mm re-registers the profiler for that hand-off. The Hermes contract in jsi/hermes-interfaces.h requires it: "Before using the runtime on another thread, invoke this function again from the new thread to make the sampling profiler target the new thread".
  • The sampler therefore signals the parked JS thread and walks the runtime's stack from the signal handler while the main thread is mutating it.

Expected: sampling is safe (or skipped) while the runtime is borrowed by the main thread.
Actual: fatal crash.

First seen in a production App Store build, one second after launch, with the sampler enabled by @sentry/react-native 8.33.0 profiling (hermesAPI->enableSamplingProfiler()) and the synchronous dispatch coming from a third-party Fabric component. The linked reproducer needs neither: it uses only React Native 0.88.1, enabling the sampler with the same call and 10 kHz frequency React Native uses for the DevTools Performance panel (HermesRuntimeTargetDelegate.cpp). The same single registerForProfiling() call and the same sync path are still on main as of 2026-12-05.

Steps to reproduce

  1. Clone the reproducer and install: cd ReproducerApp && yarn install && cd ios && pod install && cd ..
  2. Run a Release build on an iOS simulator: yarn react-native run-ios --mode Release
  3. Do nothing. The app scrolls a list of native VirtualView rows by itself; each row that becomes visible dispatches modeChange with experimental_flushSync, running a JS handler on the main thread.
  4. The app crashes about one second after launch.

Results on an iPhone 17 Pro simulator (iOS 26.5 runtime, Xcode 27.2): 6 of 6 launches crashed after ~1.2 s, all in SamplingProfiler::walkRuntimeStack. Control: the same binary with the sampler left off (SIMCTL_CHILD_REPRO_DISABLE_PROFILER=1 xcrun simctl launch booted org.reactjs.native.example.ReproducerApp) was still running after 60 s in 2 of 2 launches.

React Native Version

0.88.1

Affected Platforms

Runtime - iOS

Output of npx @react-native-community/cli info

System:
  OS: macOS 27.2.1
  CPU: (12) arm64 Apple M2 Max
  Memory: 2.31 GB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 26.14.0
    path: /opt/homebrew/bin/node
  Yarn:
    version: 1.22.22
    path: /opt/homebrew/bin/yarn
  npm:
    version: 12.2.3
    path: /opt/homebrew/bin/npm
  Watchman: Not Found
Managers:
  CocoaPods:
    version: 1.17.0
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 27.2
      - iOS 27.2
      - macOS 27.2
      - tvOS 27.2
      - visionOS 27.2
      - watchOS 27.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2026.2 AI-262.10968.63.2622.16975389
  Xcode:
    version: 27.2/27B52
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 21.0.13.1
    path: /opt/homebrew/opt/openjdk@21/bin/javac
  Ruby:
    version: 4.0.10
    path: /opt/homebrew/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react: 19.3.0
  react-native: 0.88.1
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

From the production crash; the reproducer produces the same three stacks
(see its README for the simulator version).

EXC_BAD_ACCESS (SIGSEGV) KERN_INVALID_ADDRESS at 0x409f402070584
iPhone17,5, iOS 27.2.1 (24B156), App Store build, ~1s after launch

Crashed thread: com.facebook.react.runtime.JavaScript (innermost first)
  hermes::vm::SamplingProfiler::walkRuntimeStack
  hermes::vm::sampling_profiler::Sampler::walkRuntimeStack
  hermes::vm::sampling_profiler::(anonymous namespace)::SamplerPosix::profilingSignalHandler
  _sigtramp
  _pthread_cond_wait
  std::__1::condition_variable::wait
  std::__1::__assoc_sub_state::__sub_wait
  std::__1::__assoc_sub_state::wait            <- JS thread parked on the future
  facebook::react::RCTMessageThread::tryFunc
  facebook::react::RCTMessageThread::runAsync
  +[RCTJSThreadManager runRunLoop]

Thread 0 (main), at the same moment (innermost first)
  hermes::vm::Interpreter::interpretFunction
  hermes::vm::Runtime::interpretFunctionImpl
  hermes::vm::JSFunction::_callImpl
  hermes::vm::Callable::executeCall0
  hermes::vm::Runtime::drainJobs
  facebook::hermes::HermesRuntimeImpl::drainMicrotasks
  facebook::react::RuntimeScheduler_Modern::performMicrotaskCheckpoint
  facebook::react::RuntimeScheduler_Modern::runEventLoopTick
  facebook::react::RuntimeScheduler_Modern::executeNowOnTheSameThread::lambda
  facebook::react::coordinatedExecuteSynchronouslyOnSameThread_CAN_DEADLOCK
  facebook::react::executeSynchronouslyOnSameThread_CAN_DEADLOCK
  facebook::react::RuntimeScheduler_Modern::executeNowOnTheSameThread
  facebook::react::EventBeat::induce
  facebook::react::AppleEventBeat::activityDidChange
  facebook::react::PlatformRunLoopObserver
  __CFRunLoopDoObservers
  UIApplicationMain

Thread: hermes-sampling-profiler
  sem_wait
  hermes::vm::sampling_profiler::Sampler::platformSuspendVMAndWalkStack
  hermes::vm::sampling_profiler::Sampler::sampleStack
  hermes::vm::sampling_profiler::Sampler::sampleStacks
  hermes::vm::sampling_profiler::Sampler::timerLoop

MANDATORY Reproducer

https://github.com/sbs44/rn-hermes-sampling-profiler-sync-crash

Screenshots and Videos

None. The thread dumps above and in the reproducer's README are the evidence.

Activity

  1. sbs44 commented on Dec 5, 2026

    @sbs44
    Author

    Maintainers – I prototyped a fix against the reproducer and I'm happy to open a PR. Let me know your thoughts on the approach:

    Hermes already supports moving a runtime between threads: registerForProfiling() on a runtime that is already registered calls SamplingProfiler::setRuntimeThread(), which retargets the sampler to the calling thread. It takes runtimeDataLock_, the same lock the sampler holds for the whole of each sample, so it waits for any in-flight sample before switching (API/hermes/hermes.cpp, lib/VM/Profiler/SamplingProfilerPosix.cpp at hermes-v260318099.0.5).

    So the prototype calls it at both ends of the synchronous hand-off in RuntimeExecutorSyncUIThreadUtils.mm:

    • on the calling thread, after the runtime thread has parked and before runtimeWork runs;
    • on the runtime thread, when it wakes up and before it runs any more JS.
    Diff (iOS only, 0.88.1)
    --- a/packages/react-native/ReactCommon/runtimeexecutor/platform/ios/ReactCommon/RuntimeExecutorSyncUIThreadUtils.mm
    +++ b/packages/react-native/ReactCommon/runtimeexecutor/platform/ios/ReactCommon/RuntimeExecutorSyncUIThreadUtils.mm
    @@ -8,6 +8,7 @@
     #import <Foundation/Foundation.h>
    
     #import <ReactCommon/RuntimeExecutorSyncUIThreadUtils.h>
    +#import <jsi/hermes-interfaces.h>
     #import <react/debug/react_native_assert.h>
     #import <react/utils/OnScopeExit.h>
     #import <functional>
    @@ -19,6 +20,23 @@
     namespace facebook::react {
    
     namespace {
    +/**
    + * The Hermes sampling profiler interrupts the single thread the runtime was
    + * registered on and walks the runtime's stack from that thread's signal
    + * handler. When the runtime is about to be used on a different thread, the
    + * profiler has to be told, or it keeps interrupting the previous (now parked)
    + * thread and reads a stack that this thread is mutating.
    + *
    + * Must be called on the thread that is about to use `runtime`, before it
    + * does. No-op for runtimes that are not Hermes.
    + */
    +void retargetSamplingProfilerToCurrentThread(jsi::Runtime &runtime)
    +{
    +  if (auto *hermes = jsi::castInterface<hermes::IHermes>(&runtime)) {
    +    hermes->registerForProfiling();
    +  }
    +}
    +
     class UITask {
       std::promise<void> _isDone;
       std::function<void()> _uiWork;
    @@ -141,6 +159,8 @@
         }
    
         runtimeWorkDoneFuture.wait();
    +    // The calling thread is done with the runtime; this thread owns it again.
    +    retargetSamplingProfilerToCurrentThread(rt);
       });
    
       while (true) {
    @@ -156,6 +176,8 @@
       }
    
       OnScopeExit onScopeExit([&]() { runtimeWorkDone.set_value(); });
    +  // The runtime thread is parked until `runtimeWorkDone` is set.
    +  retargetSamplingProfilerToCurrentThread(*runtime);
       // Calls into runtime scheduler, which takes care of error handling
       runtimeWork(*runtime);
     }
    @@ -180,12 +202,16 @@
         if (callingThread != std::this_thread::get_id()) {
           // Block the runtime thread on execution of `runtimeWork` on the calling thread.
           runtimeWorkDone.get_future().wait();
    +      // The calling thread is done with the runtime; this thread owns it again.
    +      retargetSamplingProfilerToCurrentThread(rt);
         }
       });
    
       jsi::Runtime *runtimePtr = runtime.get_future().get();
    
       OnScopeExit onScopeExit([&]() { runtimeWorkDone.set_value(); });
    +  // No-op when the runtime executor ran inline on this thread.
    +  retargetSamplingProfilerToCurrentThread(*runtimePtr);
       // Calls into runtime scheduler, which takes care of error handling
       runtimeWork(*runtimePtr);
     }

    Results

    Reproducer built with React Native 0.88.1 from source (RCT_USE_PREBUILT_RNCORE=0), Release, iPhone 17 Pro simulator (iOS 26.5), sampler at 10 kHz:

    Build Launches Outcome
    From source, unpatched 4 4 crashed after ~1.2 s in SamplingProfiler::walkRuntimeStack
    From source, patched 4 4 still running after 60 s
    From source, patched, soak 1 Still running after 303 s and 6,060 synchronous main-thread JS calls

    The profiler keeps working, and it now attributes main-thread JS to the main thread. A 15 s sampled trace from the patched build has 111,992 samples: 98,997 on the JS thread and 12,995 on a second thread. The function that only runs inside the synchronous handler (churnStack) is on the stack in 12,295 samples, all of them on that second thread.

    Questions

    1. Is this the right layer? RuntimeExecutorSyncUIThreadUtils is engine-agnostic today, and this adds a Hermes interface cast to it (via jsi/hermes-interfaces.h, so no new dependency). Alternatives I can see: a hook on JSRuntime next to unstable_initializeOnJsThread(), doing it in RuntimeScheduler_Modern::executeNowOnTheSameThread, or fixing it in Hermes (for example, skip the sample when the runtime is not on the registered thread). Preference?
    2. Other platforms. platform/cxx/.../RuntimeExecutorSyncUIThreadUtils.cpp has the same shape (the runtime thread parks while the caller uses the runtime), and Android reaches it through EventEmitterWrapper.cpp's experimental_flushSync. Cover Android, too?
    3. Cost. It adds one mutex lock and a small map write per hand-off, twice per synchronous call. That looked fine in the reproducer (about 20 synchronous calls per second) but I have not measured it. Could also gate on the sampler being enabled if that's not acceptable on this path.
    4. Is this path about to change? experimental_flushSync is marked experimental.
    5. Anything I'm missing? In particular, other places the runtime is used off its registered thread.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions