Re: [Bug] epoll_wait return EPOLLOUT even with EPOLLET flag

From: Jamie Lokier
Date: Thu Nov 17 2011 - 11:00:12 EST


Robin Dong wrote:
> Hi,
>
> I am a linux user and I found some question when using epoll with EPOLLET.
>
> My program's step:
>
> 1. create a socket (sfd) and connect to server
> 2. create epoll (efd)
> 3. add socket descriptor (sfd) to epoll (efd) with flag EPOLLET
> 4. get a EPOLLOUT event through epoll_wait
>
> After that, the program will nerver get a EPOLLOUT event because ituse
> EPOLLET mode (the same event will not be got twice)

Hi Robin,

With EPOLLET|EPOLLOUT, you should get a message from epoll whenever
the fd's status changes from "cannot output" to "can output".

So you should get a message when sfd transitions from write()
returning EAGAIN to write() accepting more data.

Until you get EAGAIN while writing, your program should assume the
socket is still writable. "man epoll" has a few things to say about
this (look for all the places here it says EAGAIN).

> BUTïwhen a message comes from serverïthe client socket will get aevent
> and this event contains EPOLLOUT and EPOLLIN, looks epoll_waitreturn
> too much events than it should

The message always contains the latest status, so says EPOLLOUT|EPOLLIN.

It's doesn't mean there's an EPOLLOUT "event", it just means a message
is triggered (by the socket becoming readable) so you get a status
update. In theory the program doesn't need to be told about EPOLLOUT
here (it should be assuming the socket is writable already), but it
doesn't do any harm.

EPOLLET is just a way to reduce the number of messages from epoll when
you call epoll_wait(). In effect you are telling the kernel "don't
send me repeated status messages and I'll maintain my own flags". But
it doesn't change the status value in messages that are sent.

Enjoy,
-- Jamie
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/