diff --git a/packages/react-native/Libraries/Network/RCTDataRequestHandler.mm b/packages/react-native/Libraries/Network/RCTDataRequestHandler.mm index f95d0c078e0..2aff977cf65 100644 --- a/packages/react-native/Libraries/Network/RCTDataRequestHandler.mm +++ b/packages/react-native/Libraries/Network/RCTDataRequestHandler.mm @@ -8,6 +8,8 @@ #import #import +#import + #import "RCTNetworkPlugins.h" @interface RCTDataRequestHandler () @@ -15,14 +17,22 @@ @implementation RCTDataRequestHandler { NSOperationQueue *_queue; + std::mutex _operationHandlerMutexLock; } RCT_EXPORT_MODULE() - (void)invalidate { - [_queue cancelAllOperations]; - _queue = nil; + std::lock_guard lock(_operationHandlerMutexLock); + if (_queue) { + for (NSOperation *operation in _queue.operations) { + if (!operation.isCancelled && !operation.isFinished) { + [operation cancel]; + } + } + _queue = nil; + } } - (BOOL)canHandleRequest:(NSURLRequest *)request @@ -32,6 +42,7 @@ RCT_EXPORT_MODULE() - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id)delegate { + std::lock_guard lock(_operationHandlerMutexLock); // Lazy setup if (!_queue) { _queue = [NSOperationQueue new]; @@ -69,7 +80,10 @@ RCT_EXPORT_MODULE() - (void)cancelRequest:(NSOperation *)op { - [op cancel]; + std::lock_guard lock(_operationHandlerMutexLock); + if (!op.isCancelled && !op.isFinished) { + [op cancel]; + } } - (std::shared_ptr)getTurboModule: diff --git a/packages/react-native/Libraries/Network/RCTFileRequestHandler.mm b/packages/react-native/Libraries/Network/RCTFileRequestHandler.mm index 19d025c51e1..4ca36256c68 100644 --- a/packages/react-native/Libraries/Network/RCTFileRequestHandler.mm +++ b/packages/react-native/Libraries/Network/RCTFileRequestHandler.mm @@ -7,6 +7,8 @@ #import +#import + #import #import @@ -19,14 +21,22 @@ @implementation RCTFileRequestHandler { NSOperationQueue *_fileQueue; + std::mutex _operationHandlerMutexLock; } RCT_EXPORT_MODULE() - (void)invalidate { - [_fileQueue cancelAllOperations]; - _fileQueue = nil; + std::lock_guard lock(_operationHandlerMutexLock); + if (_fileQueue) { + for (NSOperation *operation in _fileQueue.operations) { + if (!operation.isCancelled && !operation.isFinished) { + [operation cancel]; + } + } + _fileQueue = nil; + } } - (BOOL)canHandleRequest:(NSURLRequest *)request @@ -36,6 +46,7 @@ RCT_EXPORT_MODULE() - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id)delegate { + std::lock_guard lock(_operationHandlerMutexLock); // Lazy setup if (!_fileQueue) { _fileQueue = [NSOperationQueue new]; @@ -83,7 +94,10 @@ RCT_EXPORT_MODULE() - (void)cancelRequest:(NSOperation *)op { - [op cancel]; + std::lock_guard lock(_operationHandlerMutexLock); + if (!op.isCancelled && !op.isFinished) { + [op cancel]; + } } - (std::shared_ptr)getTurboModule: