Skip to content

Commit 662a9c2

Browse files
author
Adam Gleitman
committed
Use NSLock instead of @synchronized
Reason: @synchronized(nil) can do weird stuff
1 parent e97e59d commit 662a9c2

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +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
{
67-
@synchronized (socketConnections) { // [macOS]
68-
for (NSString *socketId in socketConnections) {
69-
[socketConnections[socketId] sendEventToAllConnections:event];
70-
}
68+
[connectionsLock lock]; // [macOS]
69+
for (NSString *socketId in socketConnections) {
70+
[socketConnections[socketId] sendEventToAllConnections:event];
7171
}
72+
[connectionsLock unlock]; // [macOS]
7273
}
7374

7475
+ (void)openDebugger:(NSURL *)bundleURL withErrorMessage:(NSString *)errorMessage
@@ -103,11 +104,11 @@ + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL
103104
// Note, using a static dictionary isn't really the greatest design, but
104105
// the packager connection does the same thing, so it's at least consistent.
105106
// This is a static map that holds different inspector clients per the inspectorURL
106-
@synchronized (socketConnections) { // [macOS]
107-
if (socketConnections == nil) {
108-
socketConnections = [NSMutableDictionary new];
109-
}
107+
[connectionsLock lock]; // [macOS]
108+
if (socketConnections == nil) {
109+
socketConnections = [NSMutableDictionary new];
110110
}
111+
[connectionsLock unlock]; // [macOS]
111112

112113
NSString *key = [inspectorURL absoluteString];
113114
// [macOS safety check to avoid a crash
@@ -119,20 +120,21 @@ + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL
119120

120121
RCTInspectorPackagerConnection *connection;
121122

122-
@synchronized (socketConnections) { // [macOS]
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]
123+
[connectionsLock lock]; // [macOS]
124+
connection = socketConnections[key];
125+
if (!connection || !connection.isConnected) {
126+
connection = [[RCTInspectorPackagerConnection alloc] initWithURL:inspectorURL];
127+
// [macOS safety check to avoid a crash
128+
if (connection != nil) {
132129
socketConnections[key] = connection;
133130
[connection connect];
131+
} else {
132+
RCTLogError(@"failed to initialize RCTInspectorPackagerConnection");
134133
}
134+
// macOS]
135+
135136
}
137+
[connectionsLock unlock]; // [macOS]
136138

137139
return connection;
138140
}

0 commit comments

Comments
 (0)