Avoid race conditions when setting RCTNetworkTask status #28962
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
While using a library that made a call to React Native's "loadImageWithURLRequest", I noticed that once in every 5 calls, we'd get the following error message:
This was despite everything working just fine: the image load would call the success callback without any problems.
For more context:
loadImageWithURLRequest
was called from a thread that had a lower priority than default.After investigating this by stepping through with XCode, what was happening was that:
loadImageWithURLRequest
would call[task start]
on a NetworkTask to send the request for the image[task start]
would send the request using the corresponding handlerph://
photo/asset library URL, which gets handled byRCTAssetsLibraryRequestHandler
. In this case, even though the request was sent from a background thread, the result seems to always be returned on the main threadRCTAssetsLibraryRequestHandler
will callNetworkTask
's(void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
RCTNetworkTaskFinished
NetworkTask
'sinvalidate
_requestToken
tonil
RCTNetworkTaskInProgress
, even though it's actually already finished!requestToken
yet it's marked as in progress.This may address some, but not all causes of #12152
Changelog
[iOS] [Fixed] - Fix race conditions in NetworkTask that caused ImageLoader to report Task Orphaned
Test Plan
Tested using
RNTester
; also, verified that the warning messages for "Task orphaned" no longer appear in test app