[Libevent-users] event_add() failure return handling

M.H.VanLeeuwen mhvl at sbcglobal.net
Wed Sep 19 01:24:47 EDT 2007


Christopher Layne wrote:
> On Tue, Sep 18, 2007 at 10:10:42PM -0500, M.H.VanLeeuwen wrote:
>   
>> Hi,
>>
>> When event_add(&ev, &tv) returns -1, what is the proper way to clean up.
>>
>> For example function XYZZY() takes socket fd as an argument
>>
>> XYZZY(int sockfd)
>> {
>> allocate event structure related memory...
>> ...
>>       event_set(&(event->event), sockfd, EV_TIMEOUT | EV_WRITE | 
>> EV_PERSIST, connected, &(event->event));
>>       ret = event_base_set(event_base, &(event->event));
>>
>>       if (ret == 0)
>>       {
>>           ret = event_add(&(event->event), &CONNECT_WAIT);
>>       }
>>
>>      if (ret != 0)
>>      {
>> // What is the proper action to take here?
>>             return FAIL;
>>       }
>> ...
>>      return SUCCESS;
>> }
>>     
>
> The only way to receive a non-zero retval from event_add is if the
> underlying event notification mechanism returns a non-zero value
> to event_add. In order to determine which event mechanism that is,
> we need more information like which OS you're experiencing this under
> and/or the particular event mechanism you're using (set the following
> shell variable before executing your code: EVENT_SHOW_METHOD=1).
>
> Without knowing more than that all one can say is that if event_add()
> returns -1, it is programming error, somewhere. I speculate possibly
> your fd is invalid or previously closed or something else is wrong.
>
>   
Hi Chris,

Linux 2.6 kernel, on _rare_ occasions I get errno=2 back from epoll_add().
#define ENOENT    2      /* No such file or directory */

I'd have expected EBADF (9) if the sockfd was invalid.

I've not gone to look at why this is being returned yet, any ideas?

So the question really is, does one need to call event_del() to cleanup
or should the event_add() cleanup the timer before returning an error?
The change/fix seems simple enough.

Agreed, the occurrence is quite rare indeed, and likely a bug in my code.

Thanks,
Martin
>> The reason I ask is that it seems event_add() will continue to time for 
>> CONNECT_WAIT time
>> even if it returns -1, failure.  Shouldn't event_add(), on return of -1, 
>> unwind from the timer that
>> it has started?
>>     
>
> This is valid. However it's kind of a rare case.
>
> -cl
>   



More information about the Libevent-users mailing list