I am having the following error, for which I have no explanation since the network is present and other requests work fine. What could be the issue? Any hint how to debug further? Alamofire 5.9.1. Thank you!
Not really much more info I can provide, aside from the suggestion to investigate your connection. I believe _kCFStreamErrorCodeKey": 57 is the underlying socket error, ENOTCONN, which can sometimes mean that the system tried to reuse an existing connection to the backend but that connection was disconnected. The system is supposed to make a new connection when that happens but sometimes it doesn't. You should investigate whether the backend is terminating connections after a certain amount of time or has some other issue. On the Alamofire side you should investigate using a retrier, which would automatically retry the request after such failure.
Other causes appear to include failures in connect() and shutdown(), such as a partial shutdown() (one side only) or that there was data left in the buffer when shutting down. All of the causes I could find indicate OS or Foundation issues, as Alamofire simply wraps URLSession, which does the socket handling itself, or delegates to Network.framework. So like I said, there's nothing Alamofire can really do here except provide the retriers that may work around the issue. FYI, all of my projects use a RetryPolicy by default, just to provide that little bit of resiliency.
Thank you for the detailed answer. We use RetryPolicy with count = 3. What I noticed is that the first time this request is made, from cold start, it is working. (The flow is that the app starts from a deeplink and this request will soft log in a user from the deeplink user token). However, for subsequent deeplink starts from warm state (app in background / foreground), this request will fail, so I suspect there is some issue with how the Alamofire session is kept in memory and reused (unfortunately we are still using a singleton for the Networking stack). Investigating the backend side and the logs, it seems that the failing requests are NOT reaching the backend.
I'll keep investigating this.
You’re supposed to keep a singleton for Alamofire and URLSession instances. Two things to try: use a new instance only for the deep links, or add the proper HTTP header to the deep link requests to disable connection reuse. I’ll look up exact details later. Sounds like the OS isn’t handling the socket created from the background correctly when you handle the deep link.