linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: Sargun Dhillon <sargun@sargun.me>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	viro@zeniv.linux.org.uk, akpm@linux-foundation.org,
	willy@infradead.org, arnd@kernel.org,
	Willem de Bruijn <willemb@google.com>
Subject: Re: epoll may leak events on dup
Date: Sun, 31 Oct 2021 07:39:23 +0000	[thread overview]
Message-ID: <20211031073923.M174137@dcvr> (raw)
In-Reply-To: <20211030100319.GA11526@ircssh-3.c.rugged-nimbus-611.internal>

Sargun Dhillon <sargun@sargun.me> wrote:
> I discovered an interesting behaviour in epoll today. If I register the same 
> file twice, under two different file descriptor numbers, and then I close one of 
> the two file descriptors, epoll "leaks" the first event. This is fine, because 
> one would think I could just go ahead and remove the event, but alas, that isn't 
> the case. Some example python code follows to show the issue at hand.
>
> I'm not sure if this is really considered a "bug" or just "interesting epoll
> behaviour", but in my opinion this is kind of a bug, especially because leaks
> may happen by accident -- especially if files are not immediately freed.

"Interesting epoll behavior" combined with a quirk with the
Python wrapper for epoll.  It passes the FD as epoll_event.data
(.data could also be any void *ptr, a u64, or u32).

Not knowing Python myself (but knowing Ruby and Perl5 well); I
assume Python developers chose the safest route in passing an
integer FD for .data.  Passing a pointer to an arbitrary
Perl/Ruby object would cause tricky lifetime issues with the
automatic memory management of those languages; I expect Python
would have the same problem.

> I'm also not sure why epoll events are registered by file, and not just fd.
> Is the expectation that you can share a single epoll amongst multiple
> "users" and register different files that have the same file descriptor

No, the other way around.  Different FDs for the same file.

Having registration keyed by [file+fd] allows users to pass
different pointers for different events to the same file;
which could have its uses.

Registering by FD alone isn't enough; since the epoll FD itself
can be shared across fork (which is of limited usefulness[1]).
Originaly iterations of epoll were keyed only by the file;
with the FD being added later.

> number (at least for purposes other than CRIU). Maybe someone can shed
> light on the behaviour.

CRIU?  Checkpoint/Restore In Userspace?


[1] In contrast, kqueue has a unique close-on-fork behavior
    which greatly simplifies usage from C code (but less so
    for high-level runtimes which auto-close FDs).

  reply	other threads:[~2021-10-31  7:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-30 10:03 epoll may leak events on dup Sargun Dhillon
2021-10-31  7:39 ` Eric Wong [this message]
2021-10-31  9:53   ` Sargun Dhillon
2021-10-31 10:50     ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211031073923.M174137@dcvr \
    --to=e@80x24.org \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sargun@sargun.me \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willemb@google.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).