Crash when watching -[NSURLSessionTask state] with KVO

Originator:tangphillip
Number:rdar://18419882 Date Originated:22-Sep-2014 07:17 PM
Status:Duplicate of 15513481 (Open) Resolved:24-Sep-2014 07:03 PM
Product:iOS SDK Product Version:7.0.4, 7.1.1, 7.1.2, 8.0
Classification:Crash/Hang/Data Loss Reproducible:Unable
 
Summary:
AFNetworking 2, a very popular networking library for iOS and OS X, watches each of its NSURLSessionTasks' states, in order to activate the network activity indicator on iOS, among other reasons. It does so with Key-Value Observing, which makes perfect sense.

Here's the problem: When you watch `state`, there is an occasional crash when the state changes.

Related:

- Possibly rdar://problem/15513481 (I can't tell, because I can't read the bug.)
- https://devforums.apple.com/message/920269#920269
- https://github.com/AFNetworking/AFNetworking/issues/1477
- https://github.com/AFNetworking/AFNetworking/blob/5732fab409a85ca1f4dc55f013b4d5522d92eed2/AFNetworking/AFURLSessionManager.m#L351

Steps to Reproduce:
Use the following code:

static void * AFTaskStateChangedContext = &AFTaskStateChangedContext;
NSURLSessionTask *task;

[task addObserver:self forKeyPath:NSStringFromSelector(@selector(state)) options:(NSKeyValueObservingOptions)(NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew) context:AFTaskStateChangedContext];

Then, release it in a widely-used production app.
Then, receive crash reports.

Expected Results:
KVO should not generate a crash.

Actual Results:
KVO crashes.

Version:
iOS 7.0.4, 7.1.1, 7.1.2, 8.0

Notes:
Attached are four partial crash reports. Crashlytics won't give us full crash reports, or I'd send them up too.

Configuration:
iPhone 4S, 5, 5c, 5s; iPad 2, 3, mini, mini w/Retina display, Air

Attachments:
'four-crash-reports.txt' was successfully uploaded.

Comments

Crashlytics recommended sending Apple the Shareable link for each of our crashes. Here they are:

32-bit iOS 7: http://crashes.to/s/8af25232de0

32-bit iOS 8: http://crashes.to/s/fbcc25aeaa4

64-bit iOS 7: http://crashes.to/s/6cb51419b39

64-bit iOS 8: http://crashes.to/s/17c34576037

By tangphillip at Oct. 20, 2014, 5:20 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!