fix: iOS app crash caused by the request operation canceling (#48350)

Summary:
Currently we observed many iOS app crashes caused by the `[RCTFileRequestHanlder invalidate]` method, just as the below screenshot.
<img width="1008" alt="image" src="https://github.com/user-attachments/assets/d2d6714f-63d9-40ae-8de5-742cfe718a36" />

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[IOS] [FIXED] - app crash caused by the `[RCTFileRequestHanlder invalidate]` method

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[IOS] [FIXED] - app crash caused by the `[RCTFileRequestHanlder invalidate]` method

Pull Request resolved: https://github.com/facebook/react-native/pull/48350

Test Plan: I am not able to reproduce this issue locally either, so the changes in this PR are totally from my inference, I am not sure if it really makes sense, so please help take a deeper look, thanks.

Reviewed By: javache

Differential Revision: D69751695

Pulled By: cipolleschi

fbshipit-source-id: aa4654a30f5dfac99b72ed1bda0dae1e0dc881c9
This commit is contained in:
Zhi Zhou
2025-07-01 13:28:59 +00:00
committed by React Native Bot
parent cdbd01100b
commit 9469ec7466
2 changed files with 34 additions and 6 deletions
@@ -8,6 +8,8 @@
#import <React/RCTDataRequestHandler.h>
#import <ReactCommon/RCTTurboModule.h>
#import <mutex>
#import "RCTNetworkPlugins.h"
@interface RCTDataRequestHandler () <RCTTurboModule>
@@ -15,14 +17,22 @@
@implementation RCTDataRequestHandler {
NSOperationQueue *_queue;
std::mutex _operationHandlerMutexLock;
}
RCT_EXPORT_MODULE()
- (void)invalidate
{
[_queue cancelAllOperations];
_queue = nil;
std::lock_guard<std::mutex> lock(_operationHandlerMutexLock);
if (_queue) {
for (NSOperation *operation in _queue.operations) {
if (!operation.isCancelled && !operation.isFinished) {
[operation cancel];
}
}
_queue = nil;
}
}
- (BOOL)canHandleRequest:(NSURLRequest *)request
@@ -32,6 +42,7 @@ RCT_EXPORT_MODULE()
- (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
{
std::lock_guard<std::mutex> lock(_operationHandlerMutexLock);
// Lazy setup
if (!_queue) {
_queue = [NSOperationQueue new];
@@ -69,7 +80,10 @@ RCT_EXPORT_MODULE()
- (void)cancelRequest:(NSOperation *)op
{
[op cancel];
std::lock_guard<std::mutex> lock(_operationHandlerMutexLock);
if (!op.isCancelled && !op.isFinished) {
[op cancel];
}
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
@@ -7,6 +7,8 @@
#import <React/RCTFileRequestHandler.h>
#import <mutex>
#import <MobileCoreServices/MobileCoreServices.h>
#import <React/RCTUtils.h>
@@ -19,14 +21,22 @@
@implementation RCTFileRequestHandler {
NSOperationQueue *_fileQueue;
std::mutex _operationHandlerMutexLock;
}
RCT_EXPORT_MODULE()
- (void)invalidate
{
[_fileQueue cancelAllOperations];
_fileQueue = nil;
std::lock_guard<std::mutex> lock(_operationHandlerMutexLock);
if (_fileQueue) {
for (NSOperation *operation in _fileQueue.operations) {
if (!operation.isCancelled && !operation.isFinished) {
[operation cancel];
}
}
_fileQueue = nil;
}
}
- (BOOL)canHandleRequest:(NSURLRequest *)request
@@ -36,6 +46,7 @@ RCT_EXPORT_MODULE()
- (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
{
std::lock_guard<std::mutex> lock(_operationHandlerMutexLock);
// Lazy setup
if (!_fileQueue) {
_fileQueue = [NSOperationQueue new];
@@ -83,7 +94,10 @@ RCT_EXPORT_MODULE()
- (void)cancelRequest:(NSOperation *)op
{
[op cancel];
std::lock_guard<std::mutex> lock(_operationHandlerMutexLock);
if (!op.isCancelled && !op.isFinished) {
[op cancel];
}
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule: