Skip to content

fix: cherry pick "[iOS] Reland: avoid race condition crash in [RCTDataRequestHandler invalidate]" #2450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions packages/react-native/Libraries/Network/RCTDataRequestHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
_queue.maxConcurrentOperationCount = 2;
}

__weak __block NSBlockOperation *weakOp;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// [macOS
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
NSBlockOperation *op = [NSBlockOperation new];
__weak NSBlockOperation *weakOp = op;
[op addExecutionBlock:^{
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
if (strongOp == nil || [strongOp isCancelled]) {
return;
}
// macOS]

// Get mime type
NSRange firstSemicolon = [request.URL.resourceSpecifier rangeOfString:@";"];
NSString *mimeType =
Expand All @@ -58,18 +56,17 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
expectedContentLength:-1
textEncodingName:nil];

[delegate URLRequest:strongOp didReceiveResponse:response]; // [macOS]
[delegate URLRequest:strongOp didReceiveResponse:response];

// Load data
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
if (data) {
[delegate URLRequest:strongOp didReceiveData:data]; // [macOS]
[delegate URLRequest:strongOp didReceiveData:data];
}
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
[delegate URLRequest:strongOp didCompleteWithError:error];
}];

weakOp = op;
[_queue addOperation:op];
return op;
}
Expand Down
19 changes: 8 additions & 11 deletions packages/react-native/Libraries/Network/RCTFileRequestHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,19 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
_fileQueue.maxConcurrentOperationCount = 4;
}

__weak __block NSBlockOperation *weakOp;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// [macOS
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
NSBlockOperation *op = [NSBlockOperation new];
__weak NSBlockOperation *weakOp = op;
[op addExecutionBlock:^{
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
if (strongOp == nil || [strongOp isCancelled]) {
return;
}
// macOS]

// Get content length
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager new];
NSDictionary<NSString *, id> *fileAttributes = [fileManager attributesOfItemAtPath:request.URL.path error:&error];
if (!fileAttributes) {
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
[delegate URLRequest:strongOp didCompleteWithError:error];
return;
}

Expand All @@ -77,17 +75,16 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
expectedContentLength:[fileAttributes[NSFileSize] ?: @-1 integerValue]
textEncodingName:nil];

[delegate URLRequest:strongOp didReceiveResponse:response]; // [macOS]
[delegate URLRequest:strongOp didReceiveResponse:response];

// Load data
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
if (data) {
[delegate URLRequest:strongOp didReceiveData:data]; // [macOS]
[delegate URLRequest:strongOp didReceiveData:data];
}
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
[delegate URLRequest:strongOp didCompleteWithError:error];
}];

weakOp = op;
[_fileQueue addOperation:op];
return op;
}
Expand Down
Loading