Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RCT_handleKeyCommand to avoid concurrency issues #42708

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/react-native/React/Base/RCTKeyCommands.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,24 @@ - (void)handleKeyUIEventSwizzle:(UIEvent *)event

- (void)RCT_handleKeyCommand:(NSString *)input flags:(UIKeyModifierFlags)modifierFlags
{
// In Bridgeless mode we might incur in some concurrency issues
// where the React Native instance is invalidated while iterating on the
// list of available commands.
// That will cleanup the set while iterating, which is a not allowed mutation.
// To work around that, we store the commands that we need to execute in a separate
// array, local to this function call, so we don't incur in concurrency issues
NSMutableArray<RCTKeyCommand *> *commandsToExecute = [NSMutableArray new];
for (RCTKeyCommand *command in [RCTKeyCommands sharedInstance].commands) {
if ([command matchesInput:input flags:modifierFlags]) {
if (command.block) {
command.block(nil);
[commandsToExecute addObject:command];
}
}
}

for (RCTKeyCommand *command in commandsToExecute) {
command.block(nil);
}
}

+ (instancetype)sharedInstance
Expand Down
Loading