Skip to content

Commit afb9d41

Browse files
vzaidmanmeta-codesync[bot]
authored andcommitted
fix RCTBundleURLProvider crash when failing to check isPackagerRunning (#55761)
Summary: Pull Request resolved: #55761 In certain situations, where there are network issues reaching the Dev Server, the app crashes because of waiting too long on a semaphore. Instead, reduce the timeout on the `/status` request to 6 seconds, which is plenty of time to validate that the Dev Server is running, and reduce the timeout for the semaphore to 8 seconds, in case the request still won't finish by then. Changelog: [iOS][Fixed] not crashing on network issues with connecting to DevServer Reviewed By: cipolleschi Differential Revision: D94382277
1 parent 8475dcc commit afb9d41

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/react-native/React/Base/RCTBundleURLProvider.mm

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed)
3131
static NSString *const kRCTEnableDevKey = @"RCT_enableDev";
3232
static NSString *const kRCTEnableMinificationKey = @"RCT_enableMinification";
3333
static NSString *const kRCTInlineSourceMapKey = @"RCT_inlineSourceMap";
34+
static const NSTimeInterval kRCTPackagerStatusRequestTimeout = 6;
3435

3536
@implementation RCTBundleURLProvider
3637

@@ -96,22 +97,32 @@ + (BOOL)isPackagerRunning:(NSString *)hostPort scheme:(NSString *)scheme
9697
NSURLSession *session = [NSURLSession sharedSession];
9798
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
9899
cachePolicy:NSURLRequestUseProtocolCachePolicy
99-
timeoutInterval:10];
100+
timeoutInterval:kRCTPackagerStatusRequestTimeout];
100101
[[RCTDevSupportHttpHeaders sharedInstance] applyHeadersToRequest:request];
101102
__block NSURLResponse *response;
102103
__block NSData *data;
103104

105+
__block BOOL isRunning = NO;
104106
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
105107
[[session dataTaskWithRequest:request
106-
completionHandler:^(NSData *d, NSURLResponse *res, __unused NSError *err) {
108+
completionHandler:^(NSData *d, NSURLResponse *res, NSError *err) {
107109
data = d;
108110
response = res;
111+
NSString *status = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
112+
isRunning = [status isEqualToString:@"packager-status:running"];
113+
if (!isRunning) {
114+
RCTLogWarn(
115+
@"Packager status check returned unexpected result for %@: %@, error: %@", url, status, err);
116+
}
109117
dispatch_semaphore_signal(semaphore);
110118
}] resume];
111-
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
112-
113-
NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
114-
return [status isEqualToString:@"packager-status:running"];
119+
long result = dispatch_semaphore_wait(
120+
semaphore, dispatch_time(DISPATCH_TIME_NOW, (int64_t)((kRCTPackagerStatusRequestTimeout + 2) * NSEC_PER_SEC)));
121+
if (result != 0) {
122+
RCTLogWarn(@"Packager status check timed out for %@", url);
123+
return NO;
124+
}
125+
return isRunning;
115126
}
116127

117128
- (NSString *)guessPackagerHost

0 commit comments

Comments
 (0)