react / react/react-native

After JS reload, native ios modules crash the app when they try to send events

オープン
#34,105 コメント 9 件 リアクション 1 件 担当者 1 名 GitHub で見る

@RSNara がすでに取り組んでいます。

2023年9月14日 から。

Needs: Author Feedback Never gets stale Platform: iOS
主要言語
C++
スター
127k
フォーク
25.3k
平均マージ
1日 23時間
マージ済み PR(30日)
4

説明

Description

Native ios modules inherit from RCTEventEmitter and use the sendEventWithName method to send events to the JS world.

This method starts out as followed:

- (void)sendEventWithName:(NSString *)eventName body:(id)body
{
	  RCTAssert(
	        _callableJSModules != nil,
	        @"Error when sending event: %@ with body: %@. "
	         "RCTCallableJSModules is not set. This is probably because you've "
	         "explicitly synthesized the RCTCallableJSModules in %@, even though it's inherited "
	         "from RCTEventEmitter.",
	        eventName,
	        body,
	        [self class]);

This means if _callableJSModules is nil, sendEventWithName will crash the app.

It's fine when you start the app the first time, but after a JS reload, I put a timer to output _callableJSModules != nil every second, and it looks like this.

image

I still don't understand the root cause of this issue, but it seems to happen to some native ios modules I've tried this on. I've also tried logging from the parent RCTEventEmitter and can see that some modules do get away with this issue, but I haven't found a way to get the name of those modules.

PS: Even Reanimated has this issue

image

Version

0.68.2

Output of npx react-native info
System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1
    Memory: 127.50 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    Yarn: 1.22.17 - ~/WebstormProjects/diana/node_modules/.bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK: Not Found
  IDEs:
    Android Studio: 2020.3 AI-203.7717.56.2031.7784292
    Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.13 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2 
    react-native: 0.68.2 => 0.68.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found
Steps to reproduce

Put this in RCTEventEmitter.m. Watch the output before and after a JS reload


@implementation RCTEventEmitter {
  NSInteger _listenerCount;
  BOOL _observationDisabled;
    
    @private NSString *__ID;
}

......

- (void)startObserving
{
    dispatch_async(dispatch_get_main_queue(), ^{
		self->__ID = [[NSProcessInfo processInfo] globallyUniqueString];
        [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(printCallableJSModules:)
                                       userInfo:nil
                                        repeats:YES];
    });
}

- (void) printCallableJSModules:(NSTimer*)timer
{
    NSLog(@"INIT callableJSModules %@ -- %i", __ID, [self callableJSModules] != nil);
}
Snack, code example, screenshot, or link to a repository

Error you'll get when calling sendEventWithName

image

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。