Skip to content

Commit 55cdc99

Browse files
authored
[0.73-stable] Make socketConnections NSDictionary thread-safe (#1986)
#### Please select one of the following - [ ] I am removing an existing difference between facebook/react-native and microsoft/react-native-macos 👍 - [ ] I am cherry-picking a change from Facebook's react-native into microsoft/react-native-macos 👍 - [x] I am making a fix / change for the macOS implementation of react-native - [ ] I am making a change required for Microsoft usage of react-native ## Summary: Cherry-pick of commits from #1980. ## Changelog: [IOS] [FIXED] - Improve `RCTInspectorDevServerHelper` thread safety ## Test Plan: Tested this change in an internal app that consumes React Native and confirmed that it doesn't crash. (Without this change, crashes happened intermittently but frequently enough that it was noticeable.)
2 parents 666d7b3 + a20841e commit 55cdc99

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

packages/react-native/React/DevSupport/RCTInspectorDevServerHelper.mm

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ @implementation RCTInspectorDevServerHelper
6161
RCT_NOT_IMPLEMENTED(-(instancetype)init)
6262

6363
static NSMutableDictionary<NSString *, RCTInspectorPackagerConnection *> *socketConnections = nil;
64+
static NSLock *connectionsLock = [NSLock new];
6465

6566
static void sendEventToAllConnections(NSString *event)
6667
{
68+
[connectionsLock lock]; // [macOS]
6769
for (NSString *socketId in socketConnections) {
6870
[socketConnections[socketId] sendEventToAllConnections:event];
6971
}
72+
[connectionsLock unlock]; // [macOS]
7073
}
7174

7275
+ (void)openDebugger:(NSURL *)bundleURL withErrorMessage:(NSString *)errorMessage
@@ -101,9 +104,11 @@ + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL
101104
// Note, using a static dictionary isn't really the greatest design, but
102105
// the packager connection does the same thing, so it's at least consistent.
103106
// This is a static map that holds different inspector clients per the inspectorURL
107+
[connectionsLock lock]; // [macOS]
104108
if (socketConnections == nil) {
105109
socketConnections = [NSMutableDictionary new];
106110
}
111+
[connectionsLock unlock]; // [macOS]
107112

108113
NSString *key = [inspectorURL absoluteString];
109114
// [macOS safety check to avoid a crash
@@ -112,18 +117,23 @@ + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL
112117
return nil;
113118
}
114119
// macOS]
115-
RCTInspectorPackagerConnection *connection = socketConnections[key];
120+
121+
RCTInspectorPackagerConnection *connection;
122+
123+
[connectionsLock lock]; // [macOS]
124+
connection = socketConnections[key];
116125
if (!connection || !connection.isConnected) {
117126
connection = [[RCTInspectorPackagerConnection alloc] initWithURL:inspectorURL];
118127
// [macOS safety check to avoid a crash
119-
if (connection == nil) {
128+
if (connection != nil) {
129+
socketConnections[key] = connection;
130+
[connection connect];
131+
} else {
120132
RCTLogError(@"failed to initialize RCTInspectorPackagerConnection");
121-
return nil;
122133
}
123134
// macOS]
124-
socketConnections[key] = connection;
125-
[connection connect];
126135
}
136+
[connectionsLock unlock]; // [macOS]
127137

128138
return connection;
129139
}

0 commit comments

Comments
 (0)