[iOS] Crash at app exit: static `_managers` map destroyed while `flushOperations` reads it

未关闭
#4,711 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@beqramo 已经在做这个了。

开始于 2026年11月23日。

  • #4712 来自 @beqramo —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
30/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, objective-c
领域
mobile

调研方向

The declaration and the readers are all in packages/react-native-gesture-handler/apple/RNGestureHandlerModule.mm: start at the namespace-scope _managers map, then read flushOperations (around line 185) and +handlerManagerForModuleId: to see the exit-time race. Note that PR #4712 already carries the construct-on-first-use fix, so check that thread before writing anything; the standalone reproducer in the linked Gist is the quickest way to see the mechanism. Done means the map is never destroyed at exit and the Gist model no longer trips ThreadSanitizer or crashes.

由索引模型根据 Issue 内容生成。

描述

Missing info Missing repro Platform: iOS Platform: MacOS
Description

[!NOTE]
This issue was authored by AI on behalf of @beqramo.

The iOS app sometimes crashes while it is quitting. _managers in RNGestureHandlerModule.mm is a namespace-scope static:

static std::unordered_map<int, RNGestureHandlerManager *> _managers;

Its destructor is registered with __cxa_atexit. When the app quits through exit() (for example -[NSApplication terminate:] when the iOS app runs on an Apple Silicon Mac), the main thread destroys the map in __cxa_finalize. At the same moment the mounting queue can still be in flushOperations -> +handlerManagerForModuleId: -> _managers.find(...), which reads the destroyed hash table and crashes (EXC_BAD_ACCESS / SIGSEGV).

Crashed thread (mounting queue), symbolicated frames:

std::__1::__hash_table<std::__1::__hash_value_type<int, RNGestureHandlerManager*>, ...>::find<int>
+[RNGestureHandlerModule handlerManagerForModuleId:]
-[RNGestureHandlerModule flushOperations]            RNGestureHandlerModule.mm:185

Main thread at the same moment:

std::__1::__hash_table<std::__1::__hash_value_type<int, RNGestureHandlerManager*>, ...>::~__hash_table
__cxa_finalize
exit
-[NSApplication terminate:]

The crash reports say the app was not in the foreground, so users see it as "the app crashed when I quit it".

Expected: quitting the app never crashes, even if a flushOperations is still running on the mounting queue.

Proposed fix (PR #4712): allocate the map once and never destroy it (construct-on-first-use idiom). Nothing changes while the app runs; the OS reclaims the memory at exit.

-static std::unordered_map<int, RNGestureHandlerManager *> _managers;
+static auto &_managers = *new std::unordered_map<int, RNGestureHandlerManager *>();

The same declaration is still on main (packages/react-native-gesture-handler/apple/RNGestureHandlerModule.mm).

Versions of related libraries installed in the app:

  • react-native-reanimated 4.7.1
  • react-native-worklets 0.13.0
  • react-native-screens 4.30.0
  • Expo SDK 58 (prebuild)
Steps to reproduce

This is a race between process exit and the mounting queue, so there is no deterministic app-level reproducer. In the app:

  1. Build an app with react-native 0.88.0 (New Architecture) and react-native-gesture-handler 3.3.0 in release mode, with gesture handlers on screen (so flushOperations runs on the mounting queue).
  2. Run the iOS build on an Apple Silicon Mac ("Designed for iPad").
  3. While the UI is updating (an animation or a list re-rendering), quit the app (Cmd+Q). -[NSApplication terminate:] calls exit().
  4. Sometimes the process crashes during quit with the two stacks above.

To see the mechanism deterministically, use the standalone C++ model in the Gist below (a static std::unordered_map read by another thread while main returns):

  1. Download static_map_exit_race.cpp from the Gist.
  2. clang++ -std=c++17 -g -O2 static_map_exit_race.cpp -o plain && ./plain; echo "exit=$?" -> crashes with SIGSEGV (exit 139). It crashed in 50 of 50 runs on Apple clang 21, macOS 26.2, Apple Silicon.
  3. clang++ -std=c++17 -g -O1 -fsanitize=thread static_map_exit_race.cpp -o tsan_plain && ./tsan_plain -> ThreadSanitizer reports a data race between ~unordered_map (main thread, from __cxa_atexit) and the reader thread.
  4. Rebuild with -DLEAKY (the proposed fix) -> 0 of 50 runs crashed, and there is no ThreadSanitizer or AddressSanitizer report.
A link to a Gist, an Expo Snack or a link to a repository based on this template that reproduces the bug.

https://gist.github.com/beqramo/faafb2c113671472e8c46eeda211984c

Gesture Handler version

3.3.0

React Native version

0.88.0

Platforms

iOS

JavaScript runtime

Hermes

Workflow

Using Expo Prebuild or an Expo development build

Build type

Release mode

Device

Real device

Device model

The iOS app ("Designed for iPad") running on Apple Silicon Macs, at quit

Acknowledgements

Yes

主要语言
TypeScript
星标
6.8k
派生
1.1k
平均合并
2 天 17 小时
30 天内合并 PR
28

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

software-mansion/react-native-gesture-handler 的其他 Issue

查看 software-mansion/react-native-gesture-handler 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。