Fix race condition in native module invalidation#44048
Closed
dmytrorykun wants to merge 1 commit into
Closed
Conversation
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
Contributor
|
This pull request was exported from Phabricator. Differential Revision: D55965290 |
48f3637 to
1393cb3
Compare
Contributor
|
This pull request was exported from Phabricator. Differential Revision: D55965290 |
Base commit: affadb6 |
Contributor
|
This pull request has been merged in b7812a8. |
|
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? |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_enterfor the first five before we calldispatch_group_enterfor the sixth one. In that case we would resume at dispatch_group_wait, not waiting for the remaining modules to invalidate. That would lead toRCTInstancepotentially being invalidated prematurely, deallocating all its ivars including_turboModuleManagerand_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_enterfor all modules before any other work is performed.Changelog: [iOS][Fixed] - Fixed race condition in native module invalidation.
Differential Revision: D55965290