X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:mime-version:from:date:message-id:subject:to
	:content-type; q=dns; s=default; b=RWQint1Io1VpkMf769WHbgiKCePSt
	xaQf5iVTLrHurlIlt1Id6kJgXWnKuVN+gRdjCAy0d5GOGm4L7nxRcOUFeaCQrAWy
	2SE82tdjHSoNV3yErAxzfUtgSbdkp6za+bMUD6jdHQVF+bIRMRGUXNAxq+h6bL5A
	/nA/7UNez9unUY=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:mime-version:from:date:message-id:subject:to
	:content-type; s=default; bh=cNHy2rdSsH6Ydk/ceAdu/YrWbEA=; b=qlB
	Gu30Ro8deqWZWC1cBmTrQB5ee3E+jwhdjZpxmJ1DfSJAG+UdW1swrVGN+YvoC7vb
	usQy7N8BI273wLZJGmR3ymIK289q+H8gIzD4e5GDxOALxa1iRhp2sPOAli00t3SL
	uBfIKksplg40CPJlT6+IBswA3Q/tnmt9HNkSojVA=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-0.3 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS,UNSUBSCRIBE_BODY autolearn=no version=3.3.2 spammy=conveniently, tricky, Hx-languages-length:2147, states
X-HELO: mail-io0-f172.google.com
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=1e100.net; s=20161025;        h=x-gm-message-state:mime-version:from:date:message-id:subject:to;        bh=KTDT68+wtNom7a8RswFCkRNN0gga/jezK7NcJGTiIdo=;        b=D7dMPkbPktlXebXHBNG2j1I7xeq6E8LRrDipZT6QZ0CRuKE1R4mc6HeRboaw2LYK0H         PnCCNC5xGD38pGz/nVEg5puHQ7nHxnCrWdBS5riPT6ccVSsRJ4zaagWr3M+Tu50seOYe         eUAa1kZYcZSyPFg2V8AWstkAuEDORoZq7iF4nbxWpHmDmSLt1GIfe/cWLpgostw4rQJH         R3ZSjhOLZ0JzFxTJHBOm4K5wQFqqpXuhhuevOTKIP5tqMwKcZdiprmdEQQ8hXHtKoTZx         EfwfvzOfQjrvNaMAVzDZFN+j0HwG6KdmGW+rz+BXvOHfJ5nbS9a/e24d/m5fgMOX9jRT         hM3w==
X-Gm-Message-State: AMCzsaUuLW2umnMLbwW1UoWdfpGM9qS4tpUH5h1VXMn5nOTxI5lrGq1V	iJO1vJQmVmiSykNfqz78w3175qN2Y4wsQMVsB9t328g+
X-Google-Smtp-Source: ABhQp+SPFTW1vMLuXZVNBnDplMRK5SVmCR/6tJqKr/Z5SIwR4fAuSqUDk9nwAj94lWD3X83gCpw0PvAmZ0iPNkIHDRQ=
X-Received: by 10.107.20.137 with SMTP id 131mr24261084iou.137.1510072788458; Tue, 07 Nov 2017 08:39:48 -0800 (PST)
MIME-Version: 1.0
From: Erik Bray <erik.m.bray@gmail.com>
Date: Tue, 7 Nov 2017 17:39:48 +0100
Message-ID: <CAOTD34Z_0Y65PhF9b3wPsUOUnLD1B1U=qSaFPq3usBo6dqhmRg@mail.gmail.com>
Subject: Bug in poll/select readable state on write end of pipe
To: cygwin@cygwin.com
Content-Type: text/plain; charset="UTF-8"
X-IsSubscribed: yes

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

