socketpair failed 24 (too many open files)

If my app runs for a longer period of time, I get the following error over and over again:


<Error>: tcp_connection_host_start 7181 DNSServiceGetAddrInfo failed: -65537

<Warning>: dnssd_clientstub deliver_request: socketpair failed 24 (Too many open files)


I have to completely kill the app and start it again. I didn't find a way to reproduce this except for using the app for more than an hour.


I'm using Alamofire and MDWamp to make REST calls and send push messages.


Can somebody tell me what this error means exactly? Or ideas on how to reproduce it?


Kind regards,

Matthias

Accepted Reply

I FINALLY figured it out. MDWamp was using up File Descriptors like crazy because SSL Handshakes failed and I was reconnecting all the time.

This was really hard to reproduce because it takes a while until the maximum alllowed number of File Descriptors are used up and also when the build Scheme was set to Debug, the SSL Handshake did not fail... Well maybe somebody else would've guessed it in my position, but it certainly kept me busy for a while 🙂


For anybody who stumbles upon this and is interested, here is my GitHub issue for MDWamp documenting the problem and solution:

https://github.com/mogui/MDWamp/issues/42


Eskimo: Thank you so much for your help, without your descriptor counting code I wouldn't know where I would be 😝 Also the release is planned in one week so I'm just really glad I finally figured it our!

Replies

Someone is leaking file descriptors within your process. You should run your app under load and periodically dump the file descriptor table to see if you can determine what type of file descriptor is leaking. Once you know that you can go hunting for the cause of the leak.

If the problem reproduces on the simulator you can dump the file descriptor table for your process using

lsof
. If you can only reproduce the problem on the device then things get a bit more complex. Post back in that case and I’ll explain the details.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi,

thank you for your answer.


I used this code to print my file descriptors:

http://stackoverflow.com/questions/4083608/on-ios-iphone-too-many-open-files-need-to-list-open-files-like-lsof/8153026#8153026


Is that the right approach on the device?

Currently the amount of file descriptors does not increase over time, but I also couldn't reproduce the bug yet.
So maybe my bug leads to a sudden increase in used file descriptors.


I will post any updates on this.


Kind regards

I used this code to print my file descriptors:

Is that the right approach on the device?

That code is kinda broken. There are two specific issues:

  • FD_SETSIZE
    is not an appropriate substitute for
    getdtablesize
    .
  • F_GETPATH
    will only work for files; to test whether a descriptor is valid, you can use
    F_GETFL
    .

Here’s some code (copied from an old DevForums post) that does the right thing.

inUseDescCount = 0;
descCount = getdtablesize();
for (descIndex = 0; descIndex < descCount; descIndex++) {
    if ( fcntl(descIndex, F_GETFL) >= 0 ) {
        inUseDescCount += 1;
    }
}

So maybe my bug leads to a sudden increase in used file descriptors.

That’s certainly a possibility.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for the code.


The 'inUseDescCount' does increase until about 2500 and then no calls are made anymore.

Sorry for my late response, we're still trying to find out what is causing it. The whole bug only has a chance of appearing after the app ran a long time so this is rather annoying to reproduce.


Kind regards

Can you get it to happen on the simulator? If so, that offers of extra debugging tools (most notably, DTrace).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I FINALLY figured it out. MDWamp was using up File Descriptors like crazy because SSL Handshakes failed and I was reconnecting all the time.

This was really hard to reproduce because it takes a while until the maximum alllowed number of File Descriptors are used up and also when the build Scheme was set to Debug, the SSL Handshake did not fail... Well maybe somebody else would've guessed it in my position, but it certainly kept me busy for a while 🙂


For anybody who stumbles upon this and is interested, here is my GitHub issue for MDWamp documenting the problem and solution:

https://github.com/mogui/MDWamp/issues/42


Eskimo: Thank you so much for your help, without your descriptor counting code I wouldn't know where I would be 😝 Also the release is planned in one week so I'm just really glad I finally figured it our!