Back out "fix: avoid race condition crash in [RCTDataRequestHandler invalidate]" (#49797)

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

Backing D70314889 as it was breaking some internal tests.

I verified that before the backout the tests were failing and after the backout they were not.

## Changelog:
[iOS][Changed] - Reverted  fix: avoid race condition crash in [RCTDataRequestHandler invalidate].

Reviewed By: Abbondanzo

Differential Revision: D70511155

fbshipit-source-id: 276f6947aa6bb648c9c9eeb5c342f336acc8a26f
This commit is contained in:
Riccardo Cipolleschi
2025-03-03 15:25:46 -08:00
committed by Facebook GitHub Bot
parent 5e39bd6131
commit 53eaf3e4e4
2 changed files with 11 additions and 21 deletions
@@ -49,13 +49,8 @@ RCT_EXPORT_MODULE()
_queue.maxConcurrentOperationCount = 2;
}
__weak NSBlockOperation *weakOp;
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
if (strongOp == nil || [strongOp isCancelled]) {
return;
}
__weak __block NSBlockOperation *weakOp;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// Get mime type
NSRange firstSemicolon = [request.URL.resourceSpecifier rangeOfString:@";"];
NSString *mimeType =
@@ -67,15 +62,15 @@ RCT_EXPORT_MODULE()
expectedContentLength:-1
textEncodingName:nil];
[delegate URLRequest:strongOp didReceiveResponse:response];
[delegate URLRequest:weakOp didReceiveResponse:response];
// Load data
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
if (data) {
[delegate URLRequest:strongOp didReceiveData:data];
[delegate URLRequest:weakOp didReceiveData:data];
}
[delegate URLRequest:strongOp didCompleteWithError:error];
[delegate URLRequest:weakOp didCompleteWithError:error];
}];
weakOp = op;
@@ -53,19 +53,14 @@ RCT_EXPORT_MODULE()
_fileQueue.maxConcurrentOperationCount = 4;
}
__weak NSBlockOperation *weakOp;
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
if (strongOp == nil || [strongOp isCancelled]) {
return;
}
__weak __block NSBlockOperation *weakOp;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// 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];
[delegate URLRequest:weakOp didCompleteWithError:error];
return;
}
@@ -82,14 +77,14 @@ RCT_EXPORT_MODULE()
expectedContentLength:[fileAttributes[NSFileSize] ?: @-1 integerValue]
textEncodingName:nil];
[delegate URLRequest:strongOp didReceiveResponse:response];
[delegate URLRequest:weakOp didReceiveResponse:response];
// Load data
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
if (data) {
[delegate URLRequest:strongOp didReceiveData:data];
[delegate URLRequest:weakOp didReceiveData:data];
}
[delegate URLRequest:strongOp didCompleteWithError:error];
[delegate URLRequest:weakOp didCompleteWithError:error];
}];
weakOp = op;