Commit b7812a8
Fix race condition in native module invalidation (#44048)
Summary:
Pull Request resolved: #44048
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_leave` 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.
Reviewed By: cipolleschi
Differential Revision: D55965290
fbshipit-source-id: 0c2b5957371bf3573155cee687c661603da162de1 parent 91d3bc5 commit b7812a8
1 file changed
Lines changed: 28 additions & 14 deletions
File tree
- packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon
Lines changed: 28 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
181 | 186 | | |
182 | 187 | | |
183 | 188 | | |
| |||
1033 | 1038 | | |
1034 | 1039 | | |
1035 | 1040 | | |
1036 | | - | |
| 1041 | + | |
1037 | 1042 | | |
1038 | 1043 | | |
1039 | 1044 | | |
| |||
1056 | 1061 | | |
1057 | 1062 | | |
1058 | 1063 | | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
1059 | 1067 | | |
1060 | | - | |
1061 | | - | |
1062 | | - | |
1063 | | - | |
1064 | | - | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
1065 | 1071 | | |
1066 | | - | |
1067 | | - | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
1068 | 1087 | | |
1069 | | - | |
1070 | | - | |
1071 | | - | |
1072 | | - | |
1073 | | - | |
1074 | | - | |
| 1088 | + | |
1075 | 1089 | | |
1076 | 1090 | | |
1077 | 1091 | | |
| |||
0 commit comments