react / react/react-native

Dev app crashes when switching color scheme while Text element with DynamicColorIOS is used

未关闭
#48,493 8 条评论 7 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Component: Switch Issue: Author Provided Repro Newer Patch Available Platform: iOS Type: New Architecture
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

Description

The React Native app in development mode will crash when switching the color scheme (through system or the app) while there's a Text element using DynamicColorIOS. (see App.tsx in repro)

The error message shown in Xcode is:

Assertion failure: self.size() > 3 || std::none_of( self.begin(), self.end(), [&](auto const& k) { return self.key_eq()(key, k); })
Message: 
File: .../ReproducerApp/ios/Pods/Headers/Public/RCT-Folly/folly/container/detail/F14SetFallback.h
Line: 239
Function: findImpl

(It's a FOLLY_SAFE_DCHECK assertion written here)

Stack trace shows the assertion failure occurs while calling _cache.get here in RCTTextLayoutManager.mm:

- (NSAttributedString *)_nsAttributedStringFromAttributedString:(AttributedString)attributedString
{
  auto sharedNSAttributedString = _cache.get(attributedString, [](AttributedString attributedString) {
    return wrapManagedObject(RCTNSAttributedStringFromAttributedString(attributedString));
  });

  return unwrapManagedObject(sharedNSAttributedString);
}

Fun fact is that due to the assertion statement (self.size() > 3 || ...), if we add more Text elements on the screen, the app won't crash.

This also doesn't affect release builds since that assertion seems to be dev-only.

Steps to reproduce
  1. Run the app through Xcode.
  2. Switch color scheme.
  3. Notice the crash.
React Native Version

0.76.0, 0.76.4, 0.76.5

Affected Platforms

Runtime - iOS

Areas

(not sure) Fabric - The New Renderer

Output of npx react-native info
System:
  OS: macOS 14.5
  CPU: (8) arm64 Apple M1
  Memory: 203.86 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.15.0
    path: ~/.asdf/installs/nodejs/20.15.0/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.asdf/installs/nodejs/20.15.0/bin/yarn
  npm:
    version: 10.7.0
    path: ~/.asdf/plugins/nodejs/shims/npm
  Watchman:
    version: 2024.06.24.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/z/.asdf/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.2
      - iOS 18.2
      - macOS 15.2
      - tvOS 18.2
      - visionOS 2.2
      - watchOS 11.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.2 AI-242.23339.11.2421.12550806
  Xcode:
    version: 16.2/16C5032a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.1
    path: /opt/homebrew/opt/openjdk/bin/javac
  Ruby:
    version: 3.0.2
    path: /Users/z/.asdf/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.1
    wanted: 15.0.1
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.5
    wanted: 0.76.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true
Stacktrace or Logs
Assertion failure: self.size() > 3 || std::none_of( self.begin(), self.end(), [&](auto const& k) { return self.key_eq()(key, k); })
Message: 
File: .../ReproducerApp/ios/Pods/Headers/Public/RCT-Folly/folly/container/detail/F14SetFallback.h
Line: 239
Function: findImpl
Reproducer

https://github.com/zetavg/rn-crash-DynamicColorIOS-color-scheme-switch

Screenshots and Videos

贡献指南

打开贡献指南

从这里开始

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

调研方向

从链接的 reproducer 和 RCTTextLayoutManager.mm 开始,重点查看 _nsAttributedStringFromAttributedString: 以及第 337 行附近的 _cache.get 调用。通过 Xcode 运行应用,使用采用 DynamicColorIOS 的 Text 元素切换颜色方案,并检查缓存/assertion 的行为。在此场景下开发版 iOS 应用不再崩溃即表示完成。

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

评估

技术栈
cpp, ios, react-native
领域
mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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