Skip to content

Fix race condition in native module invalidation#44048

Closed
dmytrorykun wants to merge 1 commit into
facebook:mainfrom
dmytrorykun:export-D55965290
Closed

Fix race condition in native module invalidation#44048
dmytrorykun wants to merge 1 commit into
facebook:mainfrom
dmytrorykun:export-D55965290

Conversation

@dmytrorykun
Copy link
Copy Markdown

Summary:
This is an attempt to fix a couple of similar memory corruption crashes that happen during the deallocation of RCTHost.

Hypotheis: there is a race condition between this and this chunks of work.
I.e. let's say we are invalidating 10 modules. Because of the race condition it is possible that we call dispatch_group_enter/dispatch_group_enter for the first five before we call dispatch_group_enter for the sixth one. In that case we would resume at dispatch_group_wait, not waiting for the remaining modules to invalidate. That would lead to RCTInstance potentially being invalidated prematurely, deallocating all its ivars including _turboModuleManager and _jsThreadManager. That in turn would lead to memory access error during the invalidation of remaining modules.

This diff is trying to solve this problem by calling dispatch_group_enter for all modules before any other work is performed.

Changelog: [iOS][Fixed] - Fixed race condition in native module invalidation.

Differential Revision: D55965290

Summary:

This is an attempt to fix a couple of similar memory corruption crashes that happen during the deallocation of RCTHost.

**Hypotheis:** there is a race condition between [this](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm#L1038-L1058) and [this](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm#L1062) chunks of work.
I.e. let's say we are invalidating 10 modules. Because of the race condition it is possible that we call `dispatch_group_enter`/`dispatch_group_enter` for the first five before we call `dispatch_group_enter` for the sixth one. In that case we would resume at [dispatch_group_wait](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm#L1079), not waiting for the remaining modules to invalidate. That would lead to `RCTInstance` potentially being invalidated prematurely, deallocating all its ivars including `_turboModuleManager` and `_jsThreadManager`. That in turn would lead to memory access error during the invalidation of remaining modules.

This diff is trying to solve this problem by calling `dispatch_group_enter` for all modules before any other work is performed.

Changelog: [iOS][Fixed] - Fixed race condition in native module invalidation.

Differential Revision: D55965290
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 11, 2024
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D55965290

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D55965290

@analysis-bot
Copy link
Copy Markdown

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 19,368,799 +12
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 22,743,722 -11
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: affadb6
Branch: main

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Apr 12, 2024
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in b7812a8.

@github-actions
Copy link
Copy Markdown

This pull request was successfully merged by @dmytrorykun in b7812a8.

When will my fix make it into a release? | How to file a pick request?

facebook-github-bot pushed a commit that referenced this pull request May 13, 2024
Summary:
Pull Request resolved: #44523

This is a revert of #44048
The original change has introduced some regression in native module invalidation stability.

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D57207598

fbshipit-source-id: eb967102351434c652cb9d6f9935cbf9952d7328
dmytrorykun pushed a commit to dmytrorykun/react-native that referenced this pull request May 30, 2024
…4727)

Summary:

This is a re-land of facebook#44048

Reverting it caused even bigger regression, so my earlier assessment was wrong. The initial regression was caused by something else.

Changelog: [Internal] - Let's keep the changelog entry form the original diff.

Differential Revision: D57970133
facebook-github-bot pushed a commit that referenced this pull request May 30, 2024
Summary:
Pull Request resolved: #44727

This is a re-land of #44048

Reverting it caused even bigger regression, so my earlier assessment was wrong. The initial regression was caused by something else.

Changelog: [Internal] - Let's keep the changelog entry form the original diff.

Reviewed By: fkgozali

Differential Revision: D57970133

fbshipit-source-id: c683d661a805d44434f5491e89dd4b7218379bee
kosmydel pushed a commit to kosmydel/react-native that referenced this pull request Jun 11, 2024
…#44523)

Summary:
Pull Request resolved: facebook#44523

This is a revert of facebook#44048
The original change has introduced some regression in native module invalidation stability.

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D57207598

fbshipit-source-id: eb967102351434c652cb9d6f9935cbf9952d7328
kosmydel pushed a commit to kosmydel/react-native that referenced this pull request Jun 11, 2024
…4727)

Summary:
Pull Request resolved: facebook#44727

This is a re-land of facebook#44048

Reverting it caused even bigger regression, so my earlier assessment was wrong. The initial regression was caused by something else.

Changelog: [Internal] - Let's keep the changelog entry form the original diff.

Reviewed By: fkgozali

Differential Revision: D57970133

fbshipit-source-id: c683d661a805d44434f5491e89dd4b7218379bee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants