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

[iOS] Fixes race condition of Network module #25156

Closed
wants to merge 3 commits into from
Closed
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
33 changes: 14 additions & 19 deletions Libraries/Network/RCTHTTPRequestHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ @implementation RCTHTTPRequestHandler
{
NSMapTable *_delegates;
NSURLSession *_session;
std::mutex _mutex;
std::mutex _delegatesMutex;
std::mutex _sessionMutex;
}

@synthesize bridge = _bridge;
Expand All @@ -29,12 +30,12 @@ @implementation RCTHTTPRequestHandler

- (void)invalidate
{
dispatch_async(self->_methodQueue, ^{
[self->_session invalidateAndCancel];
self->_session = nil;
});
std::lock_guard<std::mutex> lock(_sessionMutex);
[self->_session invalidateAndCancel];
self->_session = nil;
}

// Needs to lock before call this method.
- (BOOL)isValid
{
// if session == nil and delegates != nil, we've been invalidated
Expand All @@ -58,6 +59,7 @@ - (BOOL)canHandleRequest:(NSURLRequest *)request
- (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request
withDelegate:(id<RCTURLRequestDelegate>)delegate
{
std::lock_guard<std::mutex> sessionLock(_sessionMutex), delegatesLock(_delegatesMutex);
zhongwuzw marked this conversation as resolved.
Show resolved Hide resolved
// Lazy setup
if (!_session && [self isValid]) {
// You can override default NSURLSession instance property allowsCellularAccess (default value YES)
Expand All @@ -83,27 +85,20 @@ - (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request
delegate:self
delegateQueue:callbackQueue];

std::lock_guard<std::mutex> lock(_mutex);
_delegates = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory
valueOptions:NSPointerFunctionsStrongMemory
capacity:0];
}
__block NSURLSessionDataTask *task = nil;
dispatch_sync(self->_methodQueue, ^{
task = [self->_session dataTaskWithRequest:request];
});
{
std::lock_guard<std::mutex> lock(_mutex);
[_delegates setObject:delegate forKey:task];
}
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request];
[_delegates setObject:delegate forKey:task];
[task resume];
return task;
}

- (void)cancelRequest:(NSURLSessionDataTask *)task
{
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_delegatesMutex);
[_delegates removeObjectForKey:task];
}
[task cancel];
Expand All @@ -119,7 +114,7 @@ - (void)URLSession:(NSURLSession *)session
{
id<RCTURLRequestDelegate> delegate;
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_delegatesMutex);
delegate = [_delegates objectForKey:task];
}
[delegate URLRequest:task didSendDataWithProgress:totalBytesSent];
Expand Down Expand Up @@ -147,7 +142,7 @@ - (void)URLSession:(NSURLSession *)session
{
id<RCTURLRequestDelegate> delegate;
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_delegatesMutex);
delegate = [_delegates objectForKey:task];
}
[delegate URLRequest:task didReceiveResponse:response];
Expand All @@ -160,7 +155,7 @@ - (void)URLSession:(NSURLSession *)session
{
id<RCTURLRequestDelegate> delegate;
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_delegatesMutex);
delegate = [_delegates objectForKey:task];
}
[delegate URLRequest:task didReceiveData:data];
Expand All @@ -170,7 +165,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
{
id<RCTURLRequestDelegate> delegate;
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_delegatesMutex);
delegate = [_delegates objectForKey:task];
[_delegates removeObjectForKey:task];
}
Expand Down