From c413b141b6851b955d58e3280d6dfb4e618c9d79 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Tue, 24 Sep 2013 12:15:38 -0700 Subject: [PATCH] [Issue #1241] Fixing potential race condition in cancelConnection --- AFNetworking/AFURLConnectionOperation.m | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 6de15a6..92dad72 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -510,7 +510,7 @@ static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) { - (void)operationDidStart { [self.lock lock]; - if (! [self isCancelled]) { + if (![self isCancelled]) { self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO]; NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; @@ -533,6 +533,7 @@ static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) { userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; } self.error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]; + [self finish]; } } @@ -564,13 +565,13 @@ static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) { if ([self.request URL]) { userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; } - self.error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]; + NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]; - if (self.connection) { + if (![self isFinished] && self.connection) { [self.connection cancel]; // Manually send this delegate message since `[self.connection cancel]` causes the connection to never send another message to its delegate - [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:self.error]; + [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:error]; } }