From dd056d9b4c007f7e67f5ac3ad2c537e635d4a509 Mon Sep 17 00:00:00 2001 From: Vitali Zaidman Date: Tue, 10 Mar 2026 04:36:30 -0700 Subject: [PATCH] introduce isPackagerRunningAsync (#56004) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56004 Changelog: [iOS][Added] Introduce "isPackagerRunningAsync" that does not block the ui thread waiting on the request to DevServer ("Packager") validating that it is running The previous behavior was getting the UI blocked when the network conditions meant that a request was taking some time to result in success / fail / timeout. Reviewed By: huntie Differential Revision: D94430009 fbshipit-source-id: 6292ce74c027af7c217153620a03b417f30dff49 --- .../React/Base/RCTBundleURLProvider.h | 17 ++++ .../React/Base/RCTBundleURLProvider.mm | 82 +++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/packages/react-native/React/Base/RCTBundleURLProvider.h b/packages/react-native/React/Base/RCTBundleURLProvider.h index cc060e4d42d..f29b6118128 100644 --- a/packages/react-native/React/Base/RCTBundleURLProvider.h +++ b/packages/react-native/React/Base/RCTBundleURLProvider.h @@ -57,6 +57,23 @@ NS_ASSUME_NONNULL_BEGIN */ + (BOOL)isPackagerRunning:(NSString *)hostPort scheme:(NSString *__nullable)scheme; +/** + * Asynchronously checks if there's a packager running at the given host:port. + * The port is optional, if not specified, kRCTBundleURLProviderDefaultPort will be used + * The completion handler is called on an arbitrary queue. + */ ++ (void)isPackagerRunningAsync:(NSString *)hostPort completion:(void (^)(BOOL isRunning))completion; + +/** + * Asynchronously checks if there's a packager running at the given scheme://host:port. + * The port is optional, if not specified, kRCTBundleURLProviderDefaultPort will be used + * The scheme is also optional, if not specified, a default http protocol will be used + * The completion handler is called on an arbitrary queue. + */ ++ (void)isPackagerRunningAsync:(NSString *)hostPort + scheme:(NSString *__nullable)scheme + completion:(void (^)(BOOL isRunning))completion; + /** * Returns the jsBundleURL for a given bundle entrypoint and * the fallback offline JS bundle if the packager is not running. diff --git a/packages/react-native/React/Base/RCTBundleURLProvider.mm b/packages/react-native/React/Base/RCTBundleURLProvider.mm index 9032d5d5dff..e27846cf606 100644 --- a/packages/react-native/React/Base/RCTBundleURLProvider.mm +++ b/packages/react-native/React/Base/RCTBundleURLProvider.mm @@ -94,6 +94,9 @@ static NSURL *serverRootWithHostPort(NSString *hostPort, NSString *scheme) } NSURL *url = [serverRootWithHostPort(hostPort, scheme) URLByAppendingPathComponent:@"status"]; + if (url == nil) { + return NO; + } NSURLSession *session = [NSURLSession sharedSession]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url @@ -133,6 +136,73 @@ static NSURL *serverRootWithHostPort(NSString *hostPort, NSString *scheme) return isRunning; } ++ (void)isPackagerRunningAsync:(NSString *)hostPort completion:(void (^)(BOOL isRunning))completion +{ + [RCTBundleURLProvider isPackagerRunningAsync:hostPort scheme:nil completion:completion]; +} + ++ (void)isPackagerRunningAsync:(NSString *)hostPort + scheme:(NSString *)scheme + completion:(void (^)(BOOL isRunning))completion +{ + if (!kRCTAllowPackagerAccess) { + completion(NO); + return; + } + + NSURL *url = [serverRootWithHostPort(hostPort, scheme) URLByAppendingPathComponent:@"status"]; + if (url == nil) { + completion(NO); + return; + } + + NSURLSession *session = [NSURLSession sharedSession]; + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url + cachePolicy:NSURLRequestUseProtocolCachePolicy + timeoutInterval:kRCTPackagerStatusRequestTimeout]; + [[RCTDevSupportHttpHeaders sharedInstance] applyHeadersToRequest:request]; + + __block BOOL hasCompleted = NO; + NSObject *lock = [[NSObject alloc] init]; + + void (^safeCompletion)(BOOL) = ^(BOOL isRunning) { + @synchronized(lock) { + if (!hasCompleted) { + hasCompleted = YES; + completion(isRunning); + } + } + }; + + NSURLSessionDataTask *task = [session dataTaskWithRequest:request + completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (error != nil || data == nil) { + safeCompletion(NO); + return; + } + NSString *status = [[NSString alloc] initWithData:data + encoding:NSUTF8StringEncoding]; + BOOL isRunning = [status isEqualToString:@"packager-status:running"]; + safeCompletion(isRunning); + }]; + [task resume]; + + dispatch_after( + dispatch_time( + DISPATCH_TIME_NOW, + (int64_t)((kRCTPackagerStatusRequestTimeout + kRCTPackagerStatusRequestTimeoutGraceTime) * NSEC_PER_SEC)), + dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), + ^{ + @synchronized(lock) { + if (!hasCompleted) { + hasCompleted = YES; + [task cancel]; + completion(NO); + } + } + }); +} + - (NSString *)guessPackagerHost { static NSString *ipGuess; @@ -160,6 +230,18 @@ static NSURL *serverRootWithHostPort(NSString *hostPort, NSString *scheme) { return false; } + ++ (void)isPackagerRunningAsync:(NSString *)hostPort completion:(void (^)(BOOL isRunning))completion +{ + completion(NO); +} + ++ (void)isPackagerRunningAsync:(NSString *)hostPort + scheme:(NSString *)scheme + completion:(void (^)(BOOL isRunning))completion +{ + completion(NO); +} #endif - (NSString *)packagerServerHost