NSURLSession times out after about 100 URLTasks

So I'm getting some weird behavior when using an NSURLSession. Our app uses two separate sessions. And we aren't doing anything special when making them. This is it:

        let configuration = URLSessionConfiguration.default
        configuration.waitsForConnectivity = false
        configuration.timeoutIntervalForResource = 30
        self.init(configuration: configuration)

All we do is create data tasks using

session.dataTask(with: request)

After creating about 100+ data tasks and having them complete successfully with no problems, the session will hang for the full 30 seconds and return a -1001 "The request timed out." error. The next few tasks on this session will fail, and then all of a sudden the NSURLSession will start working again.

Now the weird part is... since I have two NSURLSessions, only the one that has hit 100+ tasks times out. The other NSURLSession can still contact the same server URL and run tasks just fine.

I did put this through the Network Instrument and one thing I did see is that up until the timeout happens the NSURLSession will use a single connection as expected. But after the 30s timeout and the recovery it looks like the NSURLSession decides behind the scenes to make a brand new connection and that's where the "recovery" comes from.

Out server team swears this isn't them as other non-iOS clients hitting the same endpoint don't have this error. Any help?

(Side note: I can fix this in my app by just making a new NSURLSession every 100 tasks, but that doesn't really help me understand what could be going wrong in the first place).

  • Everyone can ignore this. It turned out to be a problem with the Web Access Firewall we were using just deciding to block traffic at some point and not a code issue.

Add a Comment

Replies

This is HTTP/2, right?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"