I've been struggling with an HTTP timeout issue recently. After more than one month of investigation I'm quite sure that it is caused by bad HTTP persistent connections. Details are as follows:
- It is an iOS app.
- Most users are running iOS 8.
- I'm using
NSURLConnection
. - iOS 8 has one known
keep alive
bug but mine is a different issue. More specifically, that bug causesNSURLErrorNetworkConnectionLost
but my error isNSURLErrorTimedOut
. However, I'm not sure whether my issue is caused by another bug of iOS 8. - The behavior of my issue: After some time of using — after some HTTP requests are successfully sent and corresponding responses received — one request would cause
NSURLErrorTimedOut
, and all following(not too far away from the last one in time to reuse the persistent connection) requests would causesNSURLErrorTimedOut
. - Some working workaround:
- Kill and restart the app.
- Turn WiFi connection off on iPhone to force using 3G/4G.
- Turn air mode on and then turn it off.
- My analysis: From the behavior the issue seems to be caused by a gone-bad persistent connection. All the subsequent requests keep using this persistent connection so all fail with
NSURLErrorTimedOut
. From the workaround we can see all of them work because they cause the bad persistent connection to be dropped and a new persistent connection to be created.
My questions:
- Have anyone else encountered this issue?
- Is it a known bug of iOS 8?
- Is it caused by some unconventional configuration of the servers? I don't control the servers but I know they use nginx 1.6.1 and their engineers are working with me in investigating this issue. What information should I ask them for?
- Is there any way to force
NSURLConnection
to not reuse the current persistent connection but to create a new one so I can work around this issue after I detect it in my code?
Update:
I successfully mitigated this issue on iOS 8 by using CFNetwork
and controlling Connection
header directly. However it seems the issue becomes worse on iOS 9.
Since my hope that Apple would fix it on iOS 9 is broken I finally fired a radar: http://www.openradar.me/22770738.
If you also encounter this issue please duplicate my radar, or even better, fire your own radar if you have a more reliably reproducible sample.
Connection: close
header.This will make server drop the current connection and try again it might work – Waldropconnection
header toclose
, but unfortunately it is impossible withNSMutableURLRequest
. – Topographer