Skip to content
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

fix(0.76): avoid race condition crash in [RCTDataRequestHandler invalidate] #2385

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ

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

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

[delegate URLRequest:weakOp didReceiveResponse:response];
[delegate URLRequest:strongOp didReceiveResponse:response];

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

weakOp = op;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ

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

// 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:weakOp didCompleteWithError:error];
[delegate URLRequest:strongOp didCompleteWithError:error];
return;
}

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

[delegate URLRequest:weakOp didReceiveResponse:response];
[delegate URLRequest:strongOp didReceiveResponse:response];

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

weakOp = op;
Expand Down