Permalink
Browse files

Cancel network requests from the correct queue

Summary: Fix suggested by sooth-sayer (#10280)

Reviewed By: mmmulani

Differential Revision: D4001618

fbshipit-source-id: cc28d19d02a29b62d2bdbddcd30f94b1c1bcfd76
  • Loading branch information...
1 parent 84eaeb0 commit 26be005b0a8349afc7f00d8eb0bfe92be85021af @javache javache committed with Facebook Github Bot Oct 11, 2016
Showing with 23 additions and 4 deletions.
  1. +13 −4 Libraries/Image/RCTImageLoader.m
  2. +10 −0 Libraries/Network/RCTNetworkTask.m
@@ -444,7 +444,9 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
// Download image
__weak __typeof(self) weakSelf = self;
- RCTNetworkTask *task = [networking networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
+ __block RCTNetworkTask *task =
+ [networking networkTaskWithRequest:request
+ completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
__typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
@@ -488,8 +490,15 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
}
return ^{
- [task cancel];
- [weakSelf dequeueTasks];
+ __typeof(self) strongSelf = weakSelf;
+ if (!strongSelf || !task) {
+ return;
+ }
+ dispatch_async(strongSelf->_URLRequestQueue, ^{
+ [task cancel];
+ task = nil;
+ });
+ [strongSelf dequeueTasks];
};
}
@@ -505,7 +514,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
__block volatile uint32_t cancelled = 0;
__block dispatch_block_t cancelLoad = nil;
dispatch_block_t cancellationBlock = ^{
- if (cancelLoad) {
+ if (cancelLoad && !cancelled) {
cancelLoad();
}
OSAtomicOr32Barrier(1, &cancelled);
@@ -53,6 +53,7 @@ - (void)invalidate
_incrementalDataBlock = nil;
_responseBlock = nil;
_uploadProgressBlock = nil;
+ _requestToken = nil;
}
- (void)dispatchCallback:(dispatch_block_t)callback
@@ -66,6 +67,11 @@ - (void)dispatchCallback:(dispatch_block_t)callback
- (void)start
{
+ if (_status != RCTNetworkTaskPending) {
+ RCTLogError(@"RCTNetworkTask was already started or completed");
+ return;
+ }
+
if (_requestToken == nil) {
id token = [_handler sendRequest:_request withDelegate:self];
if ([self validateRequestToken:token]) {
@@ -77,6 +83,10 @@ - (void)start
- (void)cancel
{
+ if (_status == RCTNetworkTaskFinished) {
+ return;
+ }
+
_status = RCTNetworkTaskFinished;
id token = _requestToken;
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {

2 comments on commit 26be005

@JonathanUsername
JonathanUsername commented on 26be005 Oct 14, 2016 edited

Hey, we were getting a lot of crashes on 0.34.1 related to image loading (RCTImageLoader.m). We upgraded to the latest we could find (0.36-rc.1), but we're still getting these errors:
image
This seems to occur when loading multiple images on a feed page.

@shawnXiao

we have a lot of crashes on 0.36

Please sign in to comment.