I maintain a large production app which uses background-configured URLSession
to upload large numbers of files concurrently to our servers using PUT requests. These are file based uploads (https://developer.apple.com/documentation/foundation/urlsession/1411550-uploadtask) as required by background URLSessions to be support. We concurrently upload up to 16 files at once, although the configuration specifies only configuration.httpMaximumConnectionsPerHost = 4
When our servers migrated to Http2, I noticed that users who were uploading concurrently were returned many errors by URLSession. Specially through the delegate method urlSession(session: task: didCompleteWithError:)
, the error returned was NSURLErrorCannotParseResponse
which has little documentation or public discussion.
When our servers reverted this Http2 change, back to regular Http1.1, this stopped happening. No change in the actual server response was made. Since iOS negotiates the protocol the connection uses without our applications involvement, I cannot seem to choose a network protocol to force http1.1. I also, cannot see any other details from this NSError (no underlying error or other obvious issues). All I can do is log the network protocol via task metrics.
It seems that when this error occurs, its as if the urlsessiond
has crashed or the network stack just falls apart and I get many NSURLErrorCannotParseResponse
all at once before users can manually retry these failures.
This URLSession is configured by
let configuration = URLSessionConfiguration.background(withIdentifier: backgroundIdentifier)
configuration.waitsForConnectivity = true
configuration.allowsExpensiveNetworkAccess = true
configuration.httpMaximumConnectionsPerHost = 4
This is not a good experience and I have no other way to solve these errors. Does anyone have advice for this scenario?