From nobody@FreeBSD.org  Sat Aug 21 20:33:11 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id B329E1065674
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 21 Aug 2010 20:33:11 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 885578FC0C
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 21 Aug 2010 20:33:11 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o7LKXBxp042580
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 21 Aug 2010 20:33:11 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o7LKXBxx042579;
	Sat, 21 Aug 2010 20:33:11 GMT
	(envelope-from nobody)
Message-Id: <201008212033.o7LKXBxx042579@www.freebsd.org>
Date: Sat, 21 Aug 2010 20:33:11 GMT
From: Volodymyr Kostyrko <c.kworr@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: kqueue not reporting EOF under certain circumstances
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         149857
>Category:       kern
>Synopsis:       [kqueue] kqueue not reporting EOF under certain circumstances
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Aug 21 20:40:01 UTC 2010
>Closed-Date:    Tue Jul 17 21:48:13 UTC 2012
>Last-Modified:  Tue Jul 17 21:48:13 UTC 2012
>Originator:     Volodymyr Kostyrko
>Release:        RELENG_8
>Organization:
>Environment:
FreeBSD limbo.lan 8.1-STABLE FreeBSD 8.1-STABLE #0 r211484M: Thu Aug 19 10:06:29 EEST 2010     arcade@limbo.lan:/usr/obj/usr/src/sys/MINIMAL_8  i386
>Description:
kqueue ignores EOF event in some cases. I've tried to ping python people thinking that this one can be python based but they don't think this happens in python. In any case http://bugs.python.org/issue9591 is the place for this bug in python roundup.
>How-To-Repeat:
Sorry for the python code, I'm bad at C.

Generally this program only eats everything what comes on stdin, logs any kevents faced and exits on stream end. Two EOF cases are checked - when kqueue returns 1 >> 15 (32768) or when we receive 0 bytes of data instead of value given in kevent.

---------- test.py
#!/usr/bin/env python3.1

import select, sys

kq = select.kqueue()
assert kq.fileno() != -1, "Fatal error: can't initialise kqueue."

kq.control([select.kevent(sys.stdin, select.KQ_FILTER_READ, select.KQ_EV_ADD)], 0)

eof = False
while True:
        kevs = kq.control(None, 1, None)

        for kev in kevs:
                print(kev.filter, kev.data, kev.flags, kev.ident, kev.fflags)
                if kev.filter == select.KQ_FILTER_READ and kev.data > 0:
                        buffer = sys.stdin.read(kev.data)
                        if len(buffer) == 0:
                                print('eof')
                                eof = True
                if kev.flags >> 15 == 1 or eof:
                        eof = True
                        kq.control([select.kevent(sys.stdin, select.KQ_FILTER_READ, select.KQ_EV_DELETE)], 0)

        if len(kevs) == 0 or eof:
                break
---------- test.py

Now let's have some fun:

# cat test.py | ./test.py
-1 684 32768 0 0
#

This hangs:
# ./test.py < test.py
-1 684 0 0 0
<- We are hanging and waiting for other data.

I have tested that on RELENG_8 and RELENG_8_1 and results are the same.
>Fix:
None.

>Release-Note:
>Audit-Trail:

From: Volodymyr Kostyrko <c.kworr@gmail.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/149857: [kqueue] kqueue not reporting EOF under certain
 circumstances
Date: Tue, 7 Feb 2012 03:06:16 +0200

 Pinging up:
  * reproducible on python 3.2 / RELENG_9;
  * still present after r228510.
 
 -- 
 Sphinx of black quartz judge my vow.

From: Volodymyr Kostyrko <c.kworr@gmail.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/149857: [kqueue] kqueue not reporting EOF under certain
 circumstances
Date: Tue, 10 Jul 2012 14:41:04 +0300

 This is a multi-part message in MIME format.
 --------------050400060102030803040405
 Content-Type: text/plain; charset=UTF-8; format=flowed
 Content-Transfer-Encoding: 7bit
 
 Hi all. Long time no see... :)
 
 Here's a C sample of the same code. I was wrong initially, kqueue 
 doesn't ignore EOF, it doesn't return it even if it was told to do so.
 
   Highlights:
 
   1. When feeding data through pipe kqueue reports EOF.
   2. When redirecting stdin from any file kqueue doesn't report EOF.
 
 -- 
 Sphinx of black quartz judge my vow.
 
 --------------050400060102030803040405
 Content-Type: text/plain; charset=UTF-8;
  name="test.c"
 Content-Transfer-Encoding: base64
 Content-Disposition: attachment;
  filename="test.c"
 
 I2luY2x1ZGUgPHN5cy90eXBlcy5oPgojaW5jbHVkZSA8c3lzL2V2ZW50Lmg+CiNpbmNsdWRl
 IDxzeXMvdGltZS5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoK
 ZXh0ZXJuIEZJTEUgKnN0ZGVyciwgKnN0ZGluLCAqc3Rkb3V0OwoKaW50IG1haW4odm9pZCkg
 ewoJaW50IGtxID0ga3F1ZXVlKCksIGtldnM7CglzdHJ1Y3Qga2V2ZW50IGtldjsKCWNoYXIg
 YnVmZmVyWzQwOTZdOwoKCWlmKGtxID09IC0xKSB7CgkJcGVycm9yKCJGYXRhbCBlcnJvcjog
 Y2FuJ3QgaW5pdGlhbGlzZSBrcXVldWUiKTsKCQlyZXR1cm4oMSk7Cgl9CgoJRVZfU0VUKCZr
 ZXYsIGZpbGVubyhzdGRpbiksIEVWRklMVF9SRUFELCBFVl9BREQgfCBFVl9FTkFCTEUgfCBF
 Vl9FT0YsIDAsIDAsIE5VTEwpOwoJa2V2cyA9IGtldmVudChrcSwgJmtldiwgMSwgTlVMTCwg
 MCwgTlVMTCk7CgoJZm9yICg7OykgewoJCWtldnMgPSBrZXZlbnQoa3EsIE5VTEwsIDAsICZr
 ZXYsIDEsIE5VTEwpOwoJCXByaW50ZigiJWQgJWxkICVkICVsZCAlZFxuIiwga2V2LmZpbHRl
 ciwga2V2LmRhdGEsIGtldi5mbGFncywga2V2LmlkZW50LCBrZXYuZmZsYWdzKTsKCQlpZihr
 ZXYuZmlsdGVyID09IEVWRklMVF9SRUFEKSB7CgkJCWlmKGtldi5kYXRhID4gMCkgewoJCQkJ
 aWYoIWZnZXRzKGJ1ZmZlciwga2V2LmRhdGEsIHN0ZGluKSkgewoJCQkJCXByaW50ZigiaW5w
 dXQgZmFpbGVkXG4iKTsKCQkJCX0KCQkJfQoJCQlpZihrZXYuZmxhZ3MgPj4gMTUgPT0gMSkg
 ewoJCQkJRVZfU0VUKCZrZXYsIGZpbGVubyhzdGRpbiksIEVWRklMVF9SRUFELCBFVl9ERUxF
 VEUsIDAsIDAsIE5VTEwpOwoJCQkJa2V2cyA9IGtldmVudChrcSwgJmtldiwgMSwgTlVMTCwg
 MCwgTlVMTCk7CgkJCQlyZXR1cm4oMCk7CgkJCX0KCQl9Cgl9CgoJcmV0dXJuKDApOwp9Cg==
 --------------050400060102030803040405--

From: Volodymyr Kostyrko <c.kworr@gmail.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/149857: [kqueue] kqueue not reporting EOF under certain
 circumstances
Date: Tue, 17 Jul 2012 13:21:28 +0300

 This one can be closed. The major difference between pipe and 
 redirection from file is type of file object on which we are working. 
 While pipe behaves like pipe, file works exactly like vnode object. Care 
 should be taken manually to detect file position in this case.
 
 -- 
 Sphinx of black quartz judge my vow.
State-Changed-From-To: open->closed 
State-Changed-By: linimon 
State-Changed-When: Tue Jul 17 21:47:58 UTC 2012 
State-Changed-Why:  
Closed at submitter's request. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=149857 
>Unformatted:
