{"action":"closed","issue":{"url":"https://api.github.com/repos/Alamofire/Alamofire/issues/872","labels_url":"https://api.github.com/repos/Alamofire/Alamofire/issues/872/labels{/name}","comments_url":"https://api.github.com/repos/Alamofire/Alamofire/issues/872/comments","events_url":"https://api.github.com/repos/Alamofire/Alamofire/issues/872/events","html_url":"https://github.com/Alamofire/Alamofire/issues/872","id":112200098,"number":872,"title":"\"Network connection lost\" after locking device","user":{"login":"drichardson","id":36176,"avatar_url":"https://avatars.githubusercontent.com/u/36176?v=3","gravatar_id":"","url":"https://api.github.com/users/drichardson","html_url":"https://github.com/drichardson","followers_url":"https://api.github.com/users/drichardson/followers","following_url":"https://api.github.com/users/drichardson/following{/other_user}","gists_url":"https://api.github.com/users/drichardson/gists{/gist_id}","starred_url":"https://api.github.com/users/drichardson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drichardson/subscriptions","organizations_url":"https://api.github.com/users/drichardson/orgs","repos_url":"https://api.github.com/users/drichardson/repos","events_url":"https://api.github.com/users/drichardson/events{/privacy}","received_events_url":"https://api.github.com/users/drichardson/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Alamofire/Alamofire/labels/needs%20feedback","name":"needs feedback","color":"9559F0"}],"state":"closed","locked":false,"assignee":{"login":"cnoon","id":169110,"avatar_url":"https://avatars.githubusercontent.com/u/169110?v=3","gravatar_id":"","url":"https://api.github.com/users/cnoon","html_url":"https://github.com/cnoon","followers_url":"https://api.github.com/users/cnoon/followers","following_url":"https://api.github.com/users/cnoon/following{/other_user}","gists_url":"https://api.github.com/users/cnoon/gists{/gist_id}","starred_url":"https://api.github.com/users/cnoon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cnoon/subscriptions","organizations_url":"https://api.github.com/users/cnoon/orgs","repos_url":"https://api.github.com/users/cnoon/repos","events_url":"https://api.github.com/users/cnoon/events{/privacy}","received_events_url":"https://api.github.com/users/cnoon/received_events","type":"User","site_admin":false},"milestone":null,"comments":3,"created_at":"2015-10-19T18:02:05Z","updated_at":"2015-10-20T06:11:13Z","closed_at":"2015-10-20T06:11:13Z","body":"On iOS 9.0.1 using Alamofire 3.0, if I made some requests (which worked), locked the device, and then opened the again immediately the first request would consistently fail. The next time I retried the request it worked.\r\n\r\nI have a singleton that caches an Alamofire.Manager. The request would fail with \"connection closed\" -1005  NSURLErrorNetworkConnectionLost.\r\n\r\nAfter searching for anyone seeing a similar issue, I found [Apple Tech Note TN2277](https://developer.apple.com/library/ios/technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841-CH1-SUBSECTION9), which says that the kernel can reclaim sockets out from under apps without them having any idea what happens. Here's the relevant section:\r\n\r\n> **Testing Socket Reclaim**\r\n> If you're going to write code that handles a socket's resources being reclaimed by the kernel, you have to figure out how to test it. The exact circumstances under which the system might reclaim a socket's resources are purposely not documented; this gives us the flexibility to improve things in the future. However, on current systems (iOS 4.0 through iOS 4.3), you can get the system to reclaim resources from the sockets in your app by:\r\n>\r\n> 1. putting your app in the background\r\n> 2. ensuring that the app is suspended\r\n> 3. locking the screen\r\n>\r\n> When your app resumes execution it will find that it's sockets have been reclaimed.\r\n\r\nMy guess is that NSURLSession (which Alamofire.Manager is caching), has one or more sockets it keeps open (possibly to avoid renegotiating TLS handshakes). Then, when the device is locked, the OS reclaims these sockets. The next request on them results in an EBADF errno from the system call, which gets translated into an NSError with localized description \"connection closed\".\r\n\r\nTo workaround this, I created a new Alamofire.Manager whenever my AppDelegate's `applicationWillEnterForeground` is called. This completely resolved the issue.\r\n\r\nLike I said earlier, this is just my best guess of what is happening under the hood, take it with a grain of salt, but wanted to bring it up at least for discussion to see if there was a more general way of solving the problem."}}