Skip to content

[iOS] App hangs on iOS 27: SimpleThreadSafeCache runs text measurement while holding its lock #59613

Open

Description

@kasik96

Description

On iOS 27 devices, an app can hang for good when the JS thread and the main thread measure text at the same time.

SimpleThreadSafeCache::getMapIterator holds mutex_ while it runs the generator for a cache miss. TextLayoutManager uses this cache for text measurement, and on iOS the generator creates an NSTextStorage. On iOS 27 devices, -[NSTextStorage processEditing] posts a notification that waits for the main queue (_CFXNotificationPost → -[NSOperation waitUntilFinished]).

If the main thread measures text at that moment, for example in a synchronous commit from Reanimated, it waits for mutex_:

  • JS thread: holds mutex_ and waits for the main queue.
  • Main thread: waits for mutex_.

The observer that the notification waits for comes from iOS, not from app code. The iOS 27.0 simulator does not wait there, so the hang shows only on devices (seen on iOS 27.0.1 and 27.2).

The same code is on main.

Steps to reproduce

  1. Run on a device with iOS 27.
  2. Render a list of about 100 rows with text, and run a Reanimated animation.
  3. Replace the list data while the animation runs.

The app hangs within a few tries.

To see it on the simulator, first add a main-queue observer in lldb:

expr -l objc -- (id)[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextStorageDidProcessEditingNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *n){}]

React Native Version

0.86.3

Affected Platforms

Runtime - iOS

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

System:
  OS: macOS 27.0.1
  CPU: (10) arm64 Apple M1 Pro
Binaries:
  Node: 24.14.1
  Yarn: 4.3.1
  npm: 11.11.0
Managers:
  CocoaPods: 1.16.2
IDEs:
  Xcode: 26.6/17F113
npmPackages:
  react: 19.2.3
  react-native: 0.86.3
  react-native-reanimated: 4.5.1
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

Main thread:
  reanimated::ReanimatedModuleProxy::commitUpdates
  facebook::react::ShadowTree::commit
  facebook::react::RootShadowNode::layoutIfNeeded
  ...
  facebook::react::ParagraphShadowNode::measureContent
  facebook::react::TextLayoutManager::measure
  facebook::react::SimpleThreadSafeCache<...>::getMapIterator
  std::mutex::lock

JS thread:
  facebook::react::UIManager::completeSurface
  facebook::react::ShadowTree::commit
  facebook::react::RootShadowNode::layoutIfNeeded
  ...
  facebook::react::TextLayoutManager::measure
  facebook::react::SimpleThreadSafeCache<...>::getMapIterator
  -[RCTTextLayoutManager measureAttributedString:paragraphAttributes:layoutContext:layoutConstraints:]
  -[RCTTextLayoutManager _textStorageAndLayoutManagerWithAttributesString:paragraphAttributes:size:]
  -[NSConcreteTextStorage initWithAttributedString:]
  -[NSTextStorage processEditing]
  -[NSNotificationCenter postNotificationName:object:userInfo:]
  _CFXNotificationPost
  -[NSOperation waitUntilFinished]

Reproducer

No standalone reproducer: the hang needs an iOS 27 device, or the lldb step above. The unit test in #59614 shows the lock problem without iOS.

Fix: #59614

Activity

  1. added
    Needs: Repro
    on Oct 29, 2026
  2. react-native-bot commented on Oct 29, 2026

    @react-native-bot
    Collaborator

    Warning

    Missing reproducer: We could not detect a reproducible example in your issue report. Reproducers are mandatory and we can accept only one of those as a valid reproducer:


    You can read more about about it on our website: How to report a bug.

  3. changed the title [-][iOS][Fabric] App hangs: SimpleThreadSafeCache runs text measurement under its mutex, and the measurement waits for the main queue[/-] [+][iOS] App hangs on iOS 27: SimpleThreadSafeCache runs text measurement while holding its lock[/+] on Oct 29, 2026
  4. kasik96 commented on Oct 29, 2026

    @kasik96
    Author

    A Playground or Snack can't reproduce this on its own: the hang needs iOS 27 on a device, where creating an NSTextStorage waits for the main queue. That wait can't be triggered from JS, and the simulator doesn't do it.

    Reproducers that work without a device:

    • Unit test: GeneratorDoesNotBlockOtherThreads in [iOS] Fix a deadlock in text measurement: run the SimpleThreadSafeCache generator outside the lock #59614 (SimpleThreadSafeCacheTest.cpp). The generator waits for another thread that uses the cache. On main that thread blocks on the cache mutex and the test fails; with the fix it passes.
    • Simulator: run any app that renders text and runs a Reanimated animation, add the main-queue observer in lldb (see the issue description), then change the rendered text while the animation runs. It hangs with the two stacks above.
  5. added
    Needs: Attention
    and removed on Oct 29, 2026
  6. Nazareneism commented on Nov 9, 2026

    @Nazareneism

    @kasik96 we encountered a freeze consistent with this issue on React Native 0.88.1, including on the iOS 27.1 simulator. We did not inject the LLDB notification observer described above.

    Environment

    • Expo SDK 58.0.23 development client, Hermes / New Architecture
    • React Native 0.88.1, Reanimated 4.7.1, Worklets 0.13.0
    • Xcode 27.1, building against the iOS Simulator 27.1 SDK
    • iPhone Duo simulator, unfolded landscape

    App-level trigger

    Our app has a Bible reader where tapping a word opens a sheet animated with Reanimated. The sheet renders a definition and concordance loaded asynchronously.

    The observed sequence was:

    1. Open the reader on the unfolded simulator.
    2. Enable word selection.
    3. Tap a word to open its details sheet.
    4. The app becomes unresponsive.

    This is timing-dependent; we captured one frozen process rather than establishing a reliable reproduction rate.

    A native process sample showed the same apparent circular wait:

    Main thread:
      ReanimatedModuleProxy::performOperations / commitUpdates
      → Yoga / ParagraphShadowNode::measureContent
      → TextLayoutManager::measure
      → SimpleThreadSafeCache::getMapIterator
      → std::mutex::lock
    
    JavaScript thread:
      SimpleThreadSafeCache::getMapIterator
      → native text measurement
      → RCTApplyBaselineOffsetForRange
      → NSTextStorage::processEditing
      → notification delivery
      → NSOperation::waitUntilFinished
    

    Local patch and regression test

    We applied the mechanism proposed in #59614: look up the key under the mutex, release the mutex before running the generator, then reacquire it and recheck before inserting. If another thread inserted that key meanwhile, retain its value. Concurrent misses can therefore perform duplicate computation.

    We also ran this C++ regression against the actual installed RN header:

    #include <chrono>
    #include <future>
    #include <iostream>
    #include <optional>
    #include <string>
    #include <thread>
    #include <react/utils/SimpleThreadSafeCache.h>
    
    using facebook::react::SimpleThreadSafeCache;
    using namespace std::chrono_literals;
    
    int main() {
      SimpleThreadSafeCache<int, std::string, 3> cache;
      std::promise<void> otherDone;
      auto finished = otherDone.get_future();
      std::thread other;
    
      auto value = cache.get(1, [&] {
        other = std::thread([&] {
          cache.get(2, [] { return std::string("two"); });
          otherDone.set_value();
        });
        return std::string(
            finished.wait_for(2s) == std::future_status::ready
                ? "one" : "blocked");
      });
      other.join();
    
      if (value != "one") {
        std::cerr << "FAIL: cache generator blocked another thread\n";
        return 1;
      }
    
      auto first = cache.get(3, [&] {
        std::thread writer([&] {
          cache.get(3, [] { return std::string("first"); });
        });
        writer.join();
        return std::string("second");
      });
    
      if (first != "first" || cache.get(3) != "first") {
        std::cerr << "FAIL: concurrent miss replaced first inserted value\n";
        return 1;
      }
    
      std::cout << "PASS: cross-thread generator and concurrent insertion\n";
    }

    From a project with React Native installed:

    c++ -std=c++20 -pthread \
      -I node_modules/react-native/ReactCommon \
      cache-repro.cpp -o cache-repro
    ./cache-repro

    The unpatched 0.88.1 header fails the first check after the timeout. The patched header passes both checks.

    For the simulator validation, we explicitly rebuilt React Native from source (buildReactNativeFromSource: true in Expo’s iOS build properties) and installed a new native client so the cache patch was compiled into the binary.

    After rebuilding, two word-sheet open/close cycles and a fold transition completed without freezing. A subsequent process sample showed the main thread servicing its normal run loop. This is limited smoke-test evidence, not a long-duration stress result.

    We have not yet reduced the UI trigger to an RNTester or standalone application reproducer. The C++ test isolates the locking dependency, and the captured simulator stacks provide additional evidence that this can occur during normal iOS 27.1 simulator use.

  7. beonde commented on Nov 23, 2026

    @beonde

    We're hitting this in production on iOS 27.0.1 and 27.2.1 (RN 0.83.9). Same two stacks: main thread in __psynch_mutexwait under TextLayoutManager::measure, JS thread in NSOperation waitUntilFinished under NSTextStorage processEditing.

    We applied the change from #59614 as a local patch (generator runs outside the lock) and built React Native from source. On an iPhone running iOS 27.2.1 the hang is gone under heavy typing and fast tab switching, and the device logs no hang or watchdog reports.

    Main still calls generator() under the lock in SimpleThreadSafeCache::getMapIterator. Could #59614 be reopened, or the fix picked up another way?

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

    Needs: AttentionNeeds: Repro

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions