Re: BSD socket accept() fd leak on iphone OS
Re: BSD socket accept() fd leak on iphone OS
- Subject: Re: BSD socket accept() fd leak on iphone OS
- From: Terry Simons <email@hidden>
- Date: Fri, 25 Sep 2009 14:57:06 -0600
Did you remove your close() calls in favor of shutdown, or are you
still closing the socket? If you aren't closing the socket, then you
are probably leaking descriptors which might explain why they aren't
getting reused?
Just a guess.
Also, as I understand it you should shutdown() the socket and then
close() it (much of the code on the internet does not do this, but
IIRC, according to the Stevens book this is the proper way), but you
should only shutdown/close when you are done reading/writing the
socket.
Someone correct me if I'm wrong. ;)
- Terry
On Fri, Sep 25, 2009 at 2:01 PM, Jay Bone <email@hidden> wrote:
>
> Thank you for the reply.
>
> On Fri, Sep 25, 2009 at 3:11 AM, Quinn <email@hidden> wrote:
>>
>> I'm trying to write a simple "Hello World" type web server using BSD
>> sockets. When I compile and run this code on a linux box it behaves as I
>> expect, close()'ed file descriptors originally returned from accept() calls
>> are re-used on subsequent connections.
>>
>> When I run this same code on an iPhone, close()'ed fd's are not reused and
>> accept() calls continue to return higher and higher fd's until it reaches
>> around 256 and at that point the application locks up.
>>
>> Is the following code not correct? What am I missing? (Some error
>> handling omitted)
>>
>> That's weird. I can't see anything in your code that would cause this
>> problem. What does it do on the simulator? [If you get the same problem on
>> the simulator you can apply better tools to debug it, namely
>> <x-man-page://8/lsof> and <x-man-page://1/dtrace>.]
>
> I don't have handy access to the simulator at the moment. But when I do I
> will try.
>
> How will lsof help?
> On the device I get the following. Does lsof not work on the device?
> iPod:~ root# lsof -p 493
> lsof: PID 500 information error: Cannot allocate memory
> lsof: PID 493 information error: Cannot allocate memory
> lsof: PID 486 information error: Cannot allocate memory
> lsof: PID 483 information error: Cannot allocate memory
> lsof: PID 482 information error: Cannot allocate memory
> lsof: PID 478 information error: Cannot allocate memory
> lsof: PID 477 information error: Cannot allocate memory
> lsof: PID 447 information error: Cannot allocate memory
> lsof: PID 400 information error: Cannot allocate memory
> lsof: PID 46 information error: Cannot allocate memory
> lsof: PID 31 information error: Cannot allocate memory
> lsof: PID 29 information error: Cannot allocate memory
> lsof: PID 24 information error: Cannot allocate memory
> lsof: PID 23 information error: Cannot allocate memory
> lsof: PID 22 information error: Cannot allocate memory
> lsof: PID 21 information error: Cannot allocate memory
> lsof: PID 18 information error: Cannot allocate memory
> lsof: PID 17 information error: Cannot allocate memory
> lsof: PID 16 information error: Cannot allocate memory
> lsof: PID 15 information error: Cannot allocate memory
> lsof: PID 13 information error: Cannot allocate memory
> lsof: PID 1 information error: Cannot allocate memory
>
> How would I use dtrace to debug this socket issue?
>
>
>>
>> btw I'm confused by the following:
>> if( shutdown(connected, SHUT_RDWR) == -1 ) {
>> // Error
>> }
>>
>> // Read rest of packets...
>> while(1)
>> {
>> int bytes = recv( connected, recv_data, BUFF_SIZE, 0 );
>> [...]
>> }
>> Given that you've shut down the socket for both read and write, the recv
>> is guaranteed to fail, right?
>
> Hm. Right I guess. I only added this shutdown() and this while(1) {
> read-rest-of-packets } code after googling for code on closing sockets and
> trying to debug this fd issue.
> Originally I was just calling close() and thinking that would be enough.
>
> Do you know of any Apple iPhone Example code which creates a simple Hello
> World type web server? I'd prefer a BSD API based example / solution, but if
> I must I'll go to the higher layer CFStream stuff if I have to. At this
> point, anything that works would be helpful :(
>
> -J
>
>
>>
>> S+E
>>
>> --
>>
>> Quinn "The Eskimo!" <http://www.apple.com/developer/>
>> Apple Developer Relations, Developer Technical Support, Core OS/Hardware
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Macnetworkprog mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Macnetworkprog mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden