Is it safe to retry -1005 error code on network request

Hi, I am getting -1005 and -1009 error codes intermittently on network requests for iOS10 on both iPhone and iPad. One suggested fix online is to simply perform a retry. Does either of these error codes along with status code 0 guarantee the request never hit the server and therefore always safe to perform a retry (particularly concerned about POST requests as they are changing state on server side)?

Post not yet marked as solved Up vote post of james8912 Down vote post of james8912
7.5k views

Replies

Does either of these error codes along with status code 0 guarantee the request never hit the server and therefore always safe to perform a retry …

Error -1005 is

NSURLErrorNetworkConnectionLost
and it’s definitely not safe to retry non-idempotent requests in that case. This error indicates that the TCP connection carrying the HTTP request tore before we got the response. There’s two possibilities:
  • The request did not make it to the server.

  • The request made it to the server but the response didn’t make it back to the client.

In the second case a blind retry will cause problems. To avoid this, you must implement app-specific logic to query the server to see if the first request made it.

In contrast, error -1009 is

NSURLErrorNotConnectedToInternet
, which means the request probably didn’t make it off the device. However, given that you have to implement app-specific recovery logic to handle
NSURLErrorNetworkConnectionLost
, it’s better IMO to ignore the specific error code and use that logic always.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Eskimo. Along the same lines, is it safe or not to blindly retry on error codes -1001 and –1003?

And one other question, when a server response has a connection header of "keep-alive, close" it causes -1005 errors for requests. How would you expect iOS NSURLSession to interpret such a response? My expectation would be that it would close the connection.

is it safe or not to blindly retry on error codes -1001 and –1003?

Let’s break this down by code:

  • NSURLErrorTimedOut
    (-1001) — It’s definitely not safe to retry this one. It shows up in all sorts of situations but one common case is that the client sends a request to the server and the server just doesn’t respond. Eventually the client times out with this error, and it’s impossible to know whether the server acted on the request.
  • NSURLErrorCannotFindHost
    (-1003) — It should be safe to automatically retry this one; if the client couldn’t find the host, it can’t possibly have sent the request to it.

when a server response has a connection header of "keep-alive, close" it causes -1005 errors for requests.

I wouldn’t expect that. Normally the client knows whether the response is complete or not based on HTTP 1.1 framing, so values in the

Connection
header shouldn’t trigger
NSURLErrorNetworkConnectionLost
. I suspect that the server isn’t doing HTTP 1.1 properly, but that’s just a guess; you’d have to have a detailed look at the packets on the wire to see what’s happening here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks again for your insights.

Are there any guidelines on configuring a web server to correctly interact with an iOS client in relation to keep-alives? Just that others are reporting similar issues on StackOverflow e.g. http://stackoverflow.com/questions/25372318/error-domain-nsurlerrordomain-code-1005-the-network-connection-was-lost

If you’re talking about an HTTP 1.1 server then iOS expects the server to following the rules defined in RFC 7230 and friends. No specific configuration should be required. If the server is following the RFCs and iOS is still generated unexpected

NSURLErrorNetworkConnectionLost
errors, that’s a bug that needs to be investigated.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"