Skip to content

Commit 1129083

Browse files
Saadnajmizhongwuzw
andauthored
fix: cherry pick "[iOS] Reland: avoid race condition crash in [RCTDataRequestHandler invalidate]" (#2450)
Cherry pick facebook#50342 , which fixes a bug with our fix from #2383 I chose to omit the macOS tags as this is a cherry-pick. Co-authored-by: zhongwuzw <[email protected]>
1 parent 1d0c636 commit 1129083

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

packages/react-native/Libraries/Network/RCTDataRequestHandler.mm

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
3838
_queue.maxConcurrentOperationCount = 2;
3939
}
4040

41-
__weak __block NSBlockOperation *weakOp;
42-
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
43-
// [macOS
44-
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
41+
NSBlockOperation *op = [NSBlockOperation new];
42+
__weak NSBlockOperation *weakOp = op;
43+
[op addExecutionBlock:^{
44+
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
4545
if (strongOp == nil || [strongOp isCancelled]) {
4646
return;
4747
}
48-
// macOS]
49-
5048
// Get mime type
5149
NSRange firstSemicolon = [request.URL.resourceSpecifier rangeOfString:@";"];
5250
NSString *mimeType =
@@ -58,18 +56,17 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
5856
expectedContentLength:-1
5957
textEncodingName:nil];
6058

61-
[delegate URLRequest:strongOp didReceiveResponse:response]; // [macOS]
59+
[delegate URLRequest:strongOp didReceiveResponse:response];
6260

6361
// Load data
6462
NSError *error;
6563
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
6664
if (data) {
67-
[delegate URLRequest:strongOp didReceiveData:data]; // [macOS]
65+
[delegate URLRequest:strongOp didReceiveData:data];
6866
}
69-
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
67+
[delegate URLRequest:strongOp didCompleteWithError:error];
7068
}];
7169

72-
weakOp = op;
7370
[_queue addOperation:op];
7471
return op;
7572
}

packages/react-native/Libraries/Network/RCTFileRequestHandler.mm

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,19 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
4646
_fileQueue.maxConcurrentOperationCount = 4;
4747
}
4848

49-
__weak __block NSBlockOperation *weakOp;
50-
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
51-
// [macOS
52-
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
49+
NSBlockOperation *op = [NSBlockOperation new];
50+
__weak NSBlockOperation *weakOp = op;
51+
[op addExecutionBlock:^{
52+
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
5353
if (strongOp == nil || [strongOp isCancelled]) {
5454
return;
5555
}
56-
// macOS]
57-
5856
// Get content length
5957
NSError *error = nil;
6058
NSFileManager *fileManager = [NSFileManager new];
6159
NSDictionary<NSString *, id> *fileAttributes = [fileManager attributesOfItemAtPath:request.URL.path error:&error];
6260
if (!fileAttributes) {
63-
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
61+
[delegate URLRequest:strongOp didCompleteWithError:error];
6462
return;
6563
}
6664

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

80-
[delegate URLRequest:strongOp didReceiveResponse:response]; // [macOS]
78+
[delegate URLRequest:strongOp didReceiveResponse:response];
8179

8280
// Load data
8381
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
8482
if (data) {
85-
[delegate URLRequest:strongOp didReceiveData:data]; // [macOS]
83+
[delegate URLRequest:strongOp didReceiveData:data];
8684
}
87-
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
85+
[delegate URLRequest:strongOp didCompleteWithError:error];
8886
}];
8987

90-
weakOp = op;
9188
[_fileQueue addOperation:op];
9289
return op;
9390
}

0 commit comments

Comments
 (0)