[zeromq-dev] accept() and EMFILE/ENFILE

Dhammika Pathirana dhammika at gmail.com
Fri May 22 09:09:27 CEST 2009


Hi Martin,

We can purge idle clients to accept pending connections.
Limiting listen backlog is another option, TCP sends ECONNRESET if
there's no space in backlog queue.


Dhammika




On Thu, May 21, 2009 at 11:42 PM, Martin Sustrik<sustrik at fastmq.com> wrote:
> Hi Dhammika,
>
>> Can we handle this in client side with a timeout?
>>
>>
>> 1. Server sends ZMQ_SYN cookie on accept()
>>
>> sock = accept (listen_sock, ...);
>> if (sock > 0) {
>>    send (sock, zmq_syn, 1, 0);
>> } else {
>>    switch (errno) {
>>          case EMFILE :
>>          case ENFILE :
>>                temporarily_disable_polling ();
>>                break;
>>          case EAGAIN :
>>          case EWOULDBLOCK :
>>                continue;
>>          default:
>>                return errno;
>>    }
>> }
>>
>>
>> 2. Client waits for ZMQ_SYN cookie on connect()
>>
>> if (connect (sock, ..) == 0) {
>>    fd_set rfds;
>>    int ready;
>>
>>    FD_ZERO (&rfds);
>>    FD_SET (sock, &rfds)
>>
>>    struct timeval time_out = {zmq_timeout, 0};
>>
>>    ready = select (sock + 1, &rfds, NULL, NULL, &time_out);
>>    if (ready == 1 && FD_ISSET (sock, &rfds)) {
>>        /* Sweet zmq cookie */
>>
>>    } else if (ready == 0) {
>>        /* Timeout */
>>        shutdown (sock, SHUT_RDWR);
>>    }
>> }
>>
>>
>> We don't have to tweak TCP flags or need well known ports for this.
>> Also zmq transport is opaque, so we can do this without API changes.
>
> Hm, what about DoS attacks? If the client intentionally keeps the connection
> open rather than timeouting it it can starve the server out of file
> descriptors, no?
>
> Martin
>



More information about the zeromq-dev mailing list