Skip to content

Commit 497eb57

Browse files
RSNarafacebook-github-bot
authored andcommitted
Fix invalidation for modules with infra-generated method queues
Summary: When invalidating TurboModules, the TurboModule infra looks for a methodQueue method on the TurboModule (line 871 below). If it finds one, the TurboModuleManager dispatches module invalidate on that method queue: https://www.internalfb.com/code/fbsource/[c91dc16f4b63abd05c7c9a038e88ca692a453c69]/xplat/js/react-native-github/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm?lines=847-848%2C870-883 ## The Problem TurboModules that neither provide a method queue, nor request a method queue won't respond to selector(methodQueue). For those modules, the TurboModule system won't dispatch invalidate to their method queue ## The Fix Calling selector(methodQueue) is an unreliable way of getting the module's method queue. The TurboModuleManager attaches a method queue to every TurboModule as an associated object: https://www.internalfb.com/code/fbsource/[c91dc16f4b63]/xplat/js/react-native-github/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm?lines=636 In this diff, in the TurboModule invalidate method, we retrieve the method queue using ObjC's associated object API. This guarantees that all TurboModules will be invalidated on their method queues. Changelog: [iOS][Fixed] - Invalidate TurboModules with infra-generated method queues on their method queues Reviewed By: appden Differential Revision: D27954644 fbshipit-source-id: af4408e444d03a15d8d8e154b3980511b81d5fb1
1 parent f34a289 commit 497eb57

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm

+23-23
Original file line numberDiff line numberDiff line change
@@ -884,31 +884,31 @@ - (void)_invalidateModules
884884
shouldPerfLog:NO];
885885

886886
if ([module respondsToSelector:@selector(invalidate)]) {
887-
if ([module respondsToSelector:@selector(methodQueue)]) {
888-
dispatch_queue_t methodQueue = [module performSelector:@selector(methodQueue)];
889-
890-
if (methodQueue) {
891-
dispatch_group_enter(moduleInvalidationGroup);
892-
dispatch_block_t invalidateModule = ^{
893-
[((id<RCTInvalidating>)module) invalidate];
894-
dispatch_group_leave(moduleInvalidationGroup);
895-
};
896-
897-
if (_bridge) {
898-
[_bridge dispatchBlock:invalidateModule queue:methodQueue];
899-
} else {
900-
// Bridgeless mode
901-
if (methodQueue == RCTJSThread) {
902-
invalidateModule();
903-
} else {
904-
dispatch_async(methodQueue, invalidateModule);
905-
}
906-
}
907-
continue;
908-
}
887+
dispatch_queue_t methodQueue = (dispatch_queue_t)objc_getAssociatedObject(module, &kAssociatedMethodQueueKey);
888+
889+
if (methodQueue == nil) {
890+
RCTLogError(
891+
@"TurboModuleManager: Couldn't invalidate TurboModule \"%@\", because its method queue is nil.",
892+
[module class]);
893+
continue;
909894
}
910895

911-
[((id<RCTInvalidating>)module) invalidate];
896+
dispatch_group_enter(moduleInvalidationGroup);
897+
dispatch_block_t invalidateModule = ^{
898+
[((id<RCTInvalidating>)module) invalidate];
899+
dispatch_group_leave(moduleInvalidationGroup);
900+
};
901+
902+
if (_bridge) {
903+
[_bridge dispatchBlock:invalidateModule queue:methodQueue];
904+
} else {
905+
// Bridgeless mode
906+
if (methodQueue == RCTJSThread) {
907+
invalidateModule();
908+
} else {
909+
dispatch_async(methodQueue, invalidateModule);
910+
}
911+
}
912912
}
913913
}
914914

0 commit comments

Comments
 (0)