This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: losing events with EPOLLONESHOT and cancellation


On Tue, Jun 11, 2013 at 8:38 PM, Eric Wong <normalperson@yhbt.net> wrote:
> I seem to be encountering lost events with epoll_wait/epoll_pwait when
> using EPOLLONESHOT and pthreads cancellation.
>
> Based on my reading of sysdeps/unix/sysv/linux/epoll_pwait.c; if the
> result of epoll_pwait is greater than 0 (events are available), it is
> still possible for LIBC_CANCEL_RESET to cancel the thread and cause
> result to never be returned (to other, uncancelled threads running
> epoll_pwait).
>
> Is my observation correct?
>
> relevant code in sysdeps/unix/sysv/linux/epoll_pwait.c
> ---------------------------------8<--------------------------------
>   int oldtype = LIBC_CANCEL_ASYNC ();
>
>   int result = INLINE_SYSCALL (epoll_pwait, 6, epfd, events, maxevents,
>                                timeout, set, _NSIG / 8);
>
>   LIBC_CANCEL_RESET (oldtype);
>
>   return result;
> ---------------------------------8<--------------------------------
>
> Similarly, ports/sysdeps/unix/sysv/linux/generic/recv.c implements
> identical logic for recv, so am I correct data could be lost (and not
> readable by another, non-cancelled thread) if a recv caller is
> cancelled when recv is successful?

You are correct.

POSIX has this to say about the cancellation and the waiting event:
~~~
... if the thread is suspended at a cancellation point and the
event for which it is waiting occurs before the cancellation request
is acted upon, it is unspecified whether the cancellation request is
acted upon or whether the cancellation request remains pending
and the thread resumes normal execution. ...
~~~
Therefore you must accept that it could be possible to get the data
and then be cancelled.

The only recourse that I am aware of is to use a cancellation cleanup
handler to attempt to restore the lost data.

See pthread_cleanup_push and pthread_cleanup_pop:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cleanup_push.html

Does that answer your question?

Cheers,
Carlos.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]