public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Erik Bray <erik.m.bray@gmail.com>
To: cygwin@cygwin.com
Subject: Bug in poll/select readable state on write end of pipe
Date: Tue, 07 Nov 2017 16:39:00 -0000	[thread overview]
Message-ID: <CAOTD34Z_0Y65PhF9b3wPsUOUnLD1B1U=qSaFPq3usBo6dqhmRg@mail.gmail.com> (raw)

Hi,

I encountered this bug through a hang in Twisted, but narrowed it down
to a simple example (in Python here, but the same would work in C).

Basically, in Twisted's system process runner, there's a "hack" [1]
which basically assumes readability (as in a poll() loop) of the
writable end of a pipe indicates that the pipe is closed and/or
there's an error.  Although the "hack" is disabled by default, in
practice it is always forced to be enabled [2].  One could maybe
consider this a bug in Twisted since I can't find anything in POSIX
which states this behavior (though I could be missing it).

Nevertheless, on Linux this *is* the behavior:

>>> import os
>>> import select
>>> r, w = os.pipe()
>>> poller = select.poll()
>>> os.close(r)
>>> poller.register(w, select.POLLIN)
>>> print(poller.poll(1000))
[(4, 8)]

where 8 indicates that POLLERR is set on fd 4, indicating in this case
that if we tried to write to the pipe we would get a broken pipe
error.

However, on Cygwin the same code returns an empty list.  I don't know
if this *should* be fixed, but it would be nice.  It's slightly tricky
though.  In Cygwin's poll there's a line [3] that does something
similar for sockets--if the socket is not connected it sets POLLERR in
the results.  One could do something similar for pipes, but there
isn't an existing internal API to do this conveniently.  What one
might want is something that calls NtQueryInformationFile like in
pipe_data_available [4], and checks the NamedPipeState flag.  But that
something doesn't exist yet.

Any ideas?

Erik


[1] https://github.com/twisted/twisted/blob/bb371b3b602b1547c6e603d3b234bb18ded6d67c/src/twisted/internet/process.py#L139

[2] https://github.com/twisted/twisted/blob/bb371b3b602b1547c6e603d3b234bb18ded6d67c/src/twisted/internet/process.py#L764

[3] https://cygwin.com/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/poll.cc;h=ea45b70adacaca96edbc46d7b8ffe1cd8a94270e;hb=HEAD#l122

[4] https://cygwin.com/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/select.cc;h=f5a993850408477a65d9ecda444785d0db1f35a8;hb=HEAD#l567

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

             reply	other threads:[~2017-11-07 16:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-07 16:39 Erik Bray [this message]
2017-11-14  9:42 ` Corinna Vinschen

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='CAOTD34Z_0Y65PhF9b3wPsUOUnLD1B1U=qSaFPq3usBo6dqhmRg@mail.gmail.com' \
    --to=erik.m.bray@gmail.com \
    --cc=cygwin@cygwin.com \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).