10 Replies
      Latest reply: Feb 12, 2017 3:01 PM by eskimo RSS
      Rezard Level 1 Level 1 (15 points)

        Hi guys,

         

        I keep stumbling on a weird issue on my devices running iOS 10.

         

        I got an app still using the old (deprecated) NSURLRequest / NSURLConnection APIs for networking, and my requests randomly fail for a reason that seems unclear to me.

        These failures produce some system logs though :

         

        [] nw_connection_read 18 Connection is not ready, sending error callback
        [] nw_connection_write_close 18 Connection is not ready, sending error callback
        [] __tcp_connection_write_eof_block_invoke Write close callback received error: [57] Socket is not connected
        
        

         

        It only happens on my devices running iOS 10 (10.2 beta included), the same app running under iOS 9.3 runs fine, and the weirdest thing is that the bugs only seem to occur when the device is connected on a Wifi network. I never encountered the issue when using a cellular connection yet. I tested it on 3 different Wifi networks at home and at work, and I can confirm that the issue happens on all 3 networks.

         

        Also, the issue happens on my app built with the iOS 9 SDK, and recompiling it with the iOS 10 SDK doesn't solve the issue.

         

        I tried creating a test project that would send the requests to the same server by using the newer NSURLSession APIs, but the issue is still happening, so it would seem like it's not a bug related to NSURLSession.

         

        Have you guys encountered a similar issue on iOS 10 ?

        I tried doing some research but have found very few threads talking about this, with people saying they resolved it with the weirdest fixes, like fixing the App capabilities in Project Settings or adding some missing Privacy Reasons in Info.plist. ( http://stackoverflow.com/questions/39480988/error-received-error-57-socket-is-not-connected-ios-10 )


        Obviously, none of it solved this yet in my case.

        Thanks for your help !

        • Re: Error 57 : "Socket is not connected" since iOS 10
          eskimo Apple Staff Apple Staff (6,470 points)

          The fact that this is happening in NSURLSession and NSURLConnection is not surprising.  Internally they share a lot of common code.

          These failures produce some system logs though …

          Weird system log messages like this are not necessarily a sign that things have failed completely; there are a bunch of error conditions like this that NSURLSession will recover from.

          What is the error you get back from the NSURL{Session,Connection} API itself?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Error 57 : "Socket is not connected" since iOS 10
              Rezard Level 1 Level 1 (15 points)

              Hey Quinn,

               

              Thank you for your quick reply (and sorry for my late answer).

               

              I can confirm that most of the time, NSURLConnection recovers from these errors and everything goes as it should.

               

              However, sometimes, requests behave as if they have failed.

              I just reproduced the issue in my project using NSURLRequest / NSURLConnection, where the delegate method connection:didFailWithError: was called.

               

              [] nw_endpoint_handler_add_write_request [82.1 xxx.xxx.xxx.xxx:443 failed socket-flow (satisfied)] cannot accept write requests
              [] __tcp_connection_write_eof_block_invoke Write close callback received error: [22] Invalid argument
              URLRequest_Users_Profile - <0x174359c80> - URLRequest did receive HTTP response : 200
              URLRequest_Users_Profile - Request did fail with Error (Code 200) : La connexion réseau a été perdue.
              

               

              The 3rd line is logged when connection:didReceiveResponse: is called, we can see that the server is returning a 200 HTTP status code.

              The 4th line is logged within connection:didFailWithError:, with the localized description of the error (original error should be "Network connection has been lost" or something like that).

               

              I don't know if that helps.

              I'll keep running tests on a very light project using NSURLSession on the same server endpoints to see if I can find additional information about these errors.

               

              Thanks for your help anyway !

               

              Take care

                • Re: Error 57 : "Socket is not connected" since iOS 10
                  eskimo Apple Staff Apple Staff (6,470 points)

                  “Network connection has been lost” is probably NSURLErrorNetworkConnectionLost, which means that the network connection just disappeared out from underneath NSURL{Session,Connection}.  In some cases the system will recover from this automatically (by retrying) but HTTP rules require it to pass the error up to you in other cases (specifically, if the request is not idempotent and the connection got dropped after the request was sent down the connection).

                  As to what’s going on, you’re going to have to look at a packet trace to see if this is a server-side problem or not.  If you can temporarily disable TLS (that is, use HTTP not HTTPS) and packet trace the problem, you should be able to tell from the trace whether the server really is dropping connections.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: Error 57 : "Socket is not connected" since iOS 10
                      Rezard Level 1 Level 1 (15 points)

                      Hey Quinn,

                       

                      Again, thank you for your reply, but I'm sorry to say that didn't help much, unfortunately.

                      I spent several days checking everything that came to my mind regarding this issue, but I haven't be able to fix it this far.

                       

                      However, I'm now sure that it isn't code-related, since I was able to reproduce the issue on a lightweight project (100-ish lines in the only file that actually does something), by using the latest network APIs (can confirm that it isn't related to the deprecated NSURLConnection APIs at all).

                       

                      Here are the facts I know about the issue :

                       

                      - It triggers an NSURLError -1005 "The network connection was lost", even though it received a HTTP 200 server response (requests are also logged server-side, and I'm sure the server response was valid).

                      - Before the error is actually triggered, we see system error logs such as the ones I quoted in my first post ; they don't necessarily mean that the request will fail though.

                      - It only happens on iOS 10 devices (tested under iOS 10.2 and iOS 10.1.1), on all kinds of devices (tested on iPhone 7, iPhone SE, iPhone 6s, iPhone 6, iPad mini 3) ; I wasn't able to trigger the error on an iOS 9 device.

                      - It doesn't happen on other OS devices such as Android, even though the Android app access the exact same endpoints.

                      - However, it DOES happen when I access the same URL using Safari on iOS 10. Just like on my test app, it is not systematic, but it occasionnaly happens. The same error is logged : "com.apple.WebKit.Networking(CFNetwork)[2381] <Error>: NSURLSessionTask finished with error - code: -1005"

                      - It only happens on real devices, iOS 10.2 simulator is doing just fine.

                      - It only happens over Wifi, never over cellular connections.

                      - It only happens when I'm sending requests with TLS to our server endpoint ; the resource is also accessible with HTTP, and we see no connection error or system logs when sending HTTP requests.

                      - ATS settings seem to have no effect on the issue.

                       

                      Here are other WEIRD facts :

                       

                      - It doesn't happen on every Wifi networks. I can reproduce the issue at work, but can't reproduce it at home. However, several of my co-workers can trigger the issue on their home networks.

                      - It seems that the error triggers only when the server response data exceeds a certain size, around just over 3500 bytes. If the server response is around 1KB, it still triggers system error logs ("[57] Socket is not connected", "Write request has 0 frame count, 0 byte count", "Write close callback received error: [89] Operation canceled"...), but the requests never fail.

                      - Actually, setting the HTTP header "Accept-Encoding" to "deflate" on my requests helps, since the server is forced to send a larger response.

                      - After playing around with the Network Link Conditioner on my device, it turned out that the "In-delay" and "Out-delay" values had a great impact on the occurences of the issue. Actually, after setting the delay values to 50ms, I no longer see any system logs, and the requests are processed just fine. If I set it to 1ms (minimum value), the system logs re-appear, and my requests will eventually start to fail again.

                       

                       

                      Sorry, that's a lot of information to process, but this issue is driving me nuts, I spent days looking for the root of the problem and searching for a way to fix it.

                      In the end, it would seem that the only thing I can do about it is implementing a way to "slow down" the requests, but that doesn't sound like the right thing to do...

                       

                      I don't know if you'll be able to help, but anyway, thanks for reading.

                      I obfuscated the server address for obvious reasons, but if you want to perform some tests on your end, don't hesitate to ask and I'll drop you a mail.

                       

                       

                      Here is some additional info on the Error received when a request fail :

                       

                      (lldb) po error!
                      Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x17005f170 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x1700886b0 [0x1a6e09bb8]>{length = 16, capacity = 16, bytes = 0x100201bb3ed2d2d10000000000000000}, _kCFStreamErrorCodeKey=54, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=https://xxx.xxx.com:443/api/home, NSErrorFailingURLKey=https://xxx.xxx.com:443/api/home, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=54, NSLocalizedDescription=The network connection was lost.}
                      
                      
                      

                       

                      I "TLSTooled" our server to check that TLS setup wasn't at fault :

                       

                      ./TLSTool s_client -connect xxx.xxx.com:443
                      *  input stream did open
                      * output stream did open
                      * output stream has space
                      * protocol: TLS 1.2
                      * cipher: ECDHE_RSA_WITH_AES_128_GCM_SHA256
                      * trust result: unspecified
                      * certificate info:
                      *   0 + rsaEncryption 2048 sha256-with-rsa-signature '*.xxx.com'
                      *   1 + rsaEncryption 2048 sha256-with-rsa-signature 'RapidSSL SHA256 CA - G3'
                      *   2 + rsaEncryption 2048 sha1-with-rsa-signature 'GeoTrust Global CA'
                      ^C
                      
                      
                      
                • Re: Error 57 : "Socket is not connected" since iOS 10
                  lishuo7 Level 1 Level 1 (0 points)

                  i also happen this issue , the log is  nw_connection_read 284 connection is not ready,sending error callback

                  • Re: Error 57 : "Socket is not connected" since iOS 10
                    bwilesvysk Level 1 Level 1 (0 points)

                    Has there been any resolution to this issue?  I have a support incident in with Apple on a similar issue that might be the same cause.  In my case, it's a VoIP app, and the error happens over a regular socket, not just HTTP connections.  Thanks!

                      • Re: Error 57 : "Socket is not connected" since iOS 10
                        eskimo Apple Staff Apple Staff (6,470 points)

                        If you were using HTTP[S], my general advice on this issue is in QA1941 Handling “The network connection was lost” Errors.  I am aware of at least one bug (r. 28762259) that causes this error unnecessarily in CFNetwork’s HTTP implementation on iOS 10.  That bug should be fixed in the current 10.3 beta seed.

                        However, it sounds like you’re not using HTTP[S], in which case the above does not apply.  You wrote:

                        … the error happens over a regular socket …

                        Define “regular socket”.  A BSD Socket?  Or something at a higher level, like CFSocketStream (accessed via the NSStream or CFStream API)?

                        Share and Enjoy

                        Quinn “The Eskimo!”
                        Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                        let myEmail = "eskimo" + "1" + "@apple.com"