Skip to content

Commit 7ee34e1

Browse files
author
Adam Gleitman
committed
Make socketConnections NSDictionary thread-safe
1 parent c6ec213 commit 7ee34e1

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ @implementation RCTInspectorDevServerHelper
6464

6565
static void sendEventToAllConnections(NSString *event)
6666
{
67-
for (NSString *socketId in socketConnections) {
68-
[socketConnections[socketId] sendEventToAllConnections:event];
67+
@synchronized (socketConnections) {
68+
for (NSString *socketId in socketConnections) {
69+
[socketConnections[socketId] sendEventToAllConnections:event];
70+
}
6971
}
7072
}
7173

@@ -101,8 +103,10 @@ + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL
101103
// Note, using a static dictionary isn't really the greatest design, but
102104
// the packager connection does the same thing, so it's at least consistent.
103105
// This is a static map that holds different inspector clients per the inspectorURL
104-
if (socketConnections == nil) {
105-
socketConnections = [NSMutableDictionary new];
106+
@synchronized (socketConnections) {
107+
if (socketConnections == nil) {
108+
socketConnections = [NSMutableDictionary new];
109+
}
106110
}
107111

108112
NSString *key = [inspectorURL absoluteString];
@@ -112,17 +116,22 @@ + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL
112116
return nil;
113117
}
114118
// macOS]
115-
RCTInspectorPackagerConnection *connection = socketConnections[key];
116-
if (!connection || !connection.isConnected) {
117-
connection = [[RCTInspectorPackagerConnection alloc] initWithURL:inspectorURL];
118-
// [macOS safety check to avoid a crash
119-
if (connection == nil) {
120-
RCTLogError(@"failed to initialize RCTInspectorPackagerConnection");
121-
return nil;
119+
120+
RCTInspectorPackagerConnection *connection;
121+
122+
@synchronized (socketConnections) {
123+
connection = socketConnections[key];
124+
if (!connection || !connection.isConnected) {
125+
connection = [[RCTInspectorPackagerConnection alloc] initWithURL:inspectorURL];
126+
// [macOS safety check to avoid a crash
127+
if (connection == nil) {
128+
RCTLogError(@"failed to initialize RCTInspectorPackagerConnection");
129+
return nil;
130+
}
131+
// macOS]
132+
socketConnections[key] = connection;
133+
[connection connect];
122134
}
123-
// macOS]
124-
socketConnections[key] = connection;
125-
[connection connect];
126135
}
127136

128137
return connection;

0 commit comments

Comments
 (0)