title: How to solve timeout issues caused by bad HTTP persistent connection?
body: <p>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: </p>
  
  <ol>
  <li>It is an iOS app.</li>
  <li>Most users are running iOS 8.</li>
  <li>I'm using <code>NSURLConnection</code>.</li>
  <li>iOS 8 has one <a href="https://github.com/AFNetworking/AFNetworking/issues/2314" rel="nofollow">known <code>keep alive</code> bug</a> but mine is a different issue. More specifically, that bug causes <code>NSURLErrorNetworkConnectionLost</code> but my error is <code>NSURLErrorTimedOut</code>. However, I'm not sure whether my issue is caused by another bug of iOS 8.</li>
  <li>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 <code>NSURLErrorTimedOut</code>, and all following(not too far away from the last one in time to reuse the persistent connection) requests would causes <code>NSURLErrorTimedOut</code>.</li>
  <li>Some working workaround:
  
  <ol>
  <li>Kill and restart the app.</li>
  <li>Turn WiFi connection off on iPhone to force using 3G/4G.</li>
  <li>Turn air mode on and then turn it off.</li>
  </ol></li>
  <li>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 <code>NSURLErrorTimedOut</code>. 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.</li>
  </ol>
  
  <p>My questions:</p>
  
  <ol>
  <li>Have anyone else encountered this issue?</li>
  <li>Is it a known bug of iOS 8?</li>
  <li>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?</li>
  <li>Is there any way to force <code>NSURLConnection</code> 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?</li>
  </ol>
  
  <hr>
  
  <h3>Update:</h3>
  
  <p>I successfully mitigated this issue on iOS 8 by using <code>CFNetwork</code> and controlling <code>Connection</code> header directly. However it seems the issue becomes worse on iOS 9.</p>
  
  <p>Since my hope that Apple would fix it on iOS 9 is broken I finally fired a radar: <a href="http://www.openradar.me/22770738" rel="nofollow">http://www.openradar.me/22770738</a>.</p>
  
  <p>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.</p>