On iPhone App Restart: File Descriptors Exhausted & Failed HTTP Requests

BEHAVIOR

  • App runs great on first install.
  • If I close the app and reopen, many times the network requests fail, most likely due to too many open files.
  • If I restart the app 4 times, everything seems to load fine (until next time). A fresh install works as well.

APP

  • Flutter app. Utilizes flutter map package, which displays map tile layers. Otherwise, pulls JSON/API data every so often.

  • Heavy/frequent pulling of tile images (typically ~1000 during a single pan).

PROBLEM DEVICES:

  • Issues ONLY happens on physical iPhones (tested on 11 and 15).
  • iOS simulators work fine.
  • Androids work fine
  • On the Androids or simulator, I can pan the map and pull 3000+ tile images, and overlay data, with no issues.

TESTING

  • I have inspected disposal methods, closing network clients, even tried "exit(0)" in various places. Have tried app lifecycle widgets on paused, detached, resumed. Nothing changes the behavior.

  • At one point, I thought I had the issue fixed when I changed my DNS from 1.1.1.1 to automatic, since all the working devices seemed to have router-defined DNS and my test device had manual IP. But then the problem came back again.

COMMON ERRORS (upon restart)

  • SocketException: Connection failed (OS Error: Too many open files, errno = 24)
  • SocketException: Failed host lookup: 'site.com' (OS Error: nodename nor servname provided, or not known, errno = 8)
  • Sometimes failed to load assets as well (icons, etc).

QUESTIONS

  • What is being "fixed" by reopening the app 4 times in a row on the iOS side?
  • Is there anything I can do in the native code, so that the app always restarts fresh, and doesn't "hang on" to anything that may be causing the OS Errors?
  • Could it be an IPv4 / IPv6 issue?

REFERENCES

Answered by DTS Engineer in 803717022

There are lots of layers of third-party code between your code and the Apple APIs that I can help you with. You have a couple of choices here:

  • You can dig through those layers yourself to understand how they’re using Apple’s APIs. If you do that, I can help you understand what’s going on at the API level.

  • You can seek help from your third-party tooling vendor.

Share and Enjoy

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

There are lots of layers of third-party code between your code and the Apple APIs that I can help you with. You have a couple of choices here:

  • You can dig through those layers yourself to understand how they’re using Apple’s APIs. If you do that, I can help you understand what’s going on at the API level.

  • You can seek help from your third-party tooling vendor.

Share and Enjoy

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

Accepted Answer

Fixed it!

I installed native_dio_adapter.

Now no file limit errors. In addition, I refactored some flutter_map code to use a singleton of dio rather than remaking/closing so often. So fast now.

On iPhone App Restart: File Descriptors Exhausted & Failed HTTP Requests
 
 
Q