NetBSD Problem Report #17517
Received: (qmail 25138 invoked by uid 605); 8 Jul 2002 13:08:50 -0000
Message-Id: <200207081310.g68DAc200554@snowdrop.l8s.co.uk>
Date: Mon, 8 Jul 2002 14:10:38 +0100 (BST)
From: dsl@l8s.co.uk
Sender: gnats-bugs-owner@netbsd.org
Reply-To: dsl@l8s.co.uk
To: gnats-bugs@gnats.netbsd.org
Subject: poll() doesn't handle 3+ way collisions
X-Send-Pr-Version: 3.95
>Number: 17517
>Category: kern
>Synopsis: poll() doesn't handle 3+ way collisions
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: jdolecek
>State: closed
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Mon Jul 08 13:09:12 +0000 2002
>Closed-Date: Tue Jan 21 08:27:42 +0000 2003
>Last-Modified: Sat Apr 29 07:29:33 +0000 2006
>Originator: David Laight
>Release: NetBSD 1.6B July 4 2002
>Organization:
none
>Environment:
System: NetBSD snowdrop 1.6B NetBSD 1.6B (GENERIC) #5: Mon Jul 8 13:17:37 BST 2002
dsl@snowdrop:/oldroot/usr/bsd-current/src/sys/arch/i386/compile/GENERIC i386
Architecture: i386
Machine: i386
>Description:
If two processes are blocked in poll() on the same file,
then one of them times out,
then another poll request comes in for the same file,
and a wakeup event happens
then the second process isn't woken up.
>How-To-Repeat:
Run the program below:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#define nelem(x) (sizeof (x) / sizeof *(x))
typedef struct cmd_t {
int delay;
int tmo;
} cmd_t;
static int
child( int id, int ctl_fd, int data_fd )
{
cmd_t cmd;
char ch;
struct pollfd pfd;
while (read( ctl_fd, &cmd, sizeof cmd ) == sizeof cmd) {
printf( "child %d: delay %d\n", id, cmd.delay );
sleep( cmd.delay );
printf( "child %d: poll %d\n", id, cmd.tmo );
pfd.fd = data_fd;
pfd.events = POLLIN;
switch (poll( &pfd, 1, cmd.tmo )) {
case 0:
printf( "child %d: poll timedout\n", id );
break;
case 1:
if (read( data_fd, &ch, 1 ) == 1)
printf( "child %d: got data %c\n", id, ch );
else
printf( "child %d: no data available\n", id );
break;
default:
printf( "child %d: poll failed\n", id );
}
}
printf( "child %d: exiting\n", id );
return 0;
}
static void
send_command( int fd, int delay, int tmo )
{
cmd_t cmd;
cmd.delay = delay;
cmd.tmo = tmo;
write( fd, &cmd, sizeof cmd );
}
int
main( int argc, char **argv )
{
int pipes[ 8 ];
int i, c;
char dump[4];
setlinebuf( stdout );
pipe( pipes );
pipe( pipes + 2 );
pipe( pipes + 4 );
pipe( pipes + 6 );
fcntl( pipes[0], F_SETFL, fcntl( pipes[0], F_GETFL, 0 ) | O_NONBLOCK );
for (c = 1; c <= 3; c++) {
if (!fork()) {
for (i = 1; i < nelem( pipes ); i += 2) {
close( pipes[ i ] );
}
return child( c, pipes[ c * 2 ], pipes[ 0 ] );
}
}
send_command( pipes[3], 0, 2000 );
send_command( pipes[5], 1, 10000 );
send_command( pipes[7], 3, 10000 );
sleep( 5 );
printf( "parent: writing 2 bytes...\n" );
write( pipes[ 1 ], "ab", 2 );
sleep( 10 );
if (read( pipes[0], dump, sizeof dump ) > 0)
printf( "parent: data left on pipe\n" );
return 0;
}
>Fix:
Don't clear SI_COLL until a wakeup event happens.
(The change to selwakeup is a gratuitous optimisation)
Index: sys_generic.c
===================================================================
RCS file: /cvsroot/syssrc/sys/kern/sys_generic.c,v
retrieving revision 1.62
diff -u -r1.62 sys_generic.c
--- sys_generic.c 2002/03/22 18:58:59 1.62
+++ sys_generic.c 2002/07/08 13:04:31
@@ -929,13 +929,11 @@
mypid = selector->p_pid;
if (sip->si_pid == mypid)
return;
- if (sip->si_pid && (p = pfind(sip->si_pid)) &&
- p->p_wchan == (caddr_t)&selwait)
+ if (sip->si_pid && !(sip->si_flags & SI_COLL)
+ && (p = pfind(sip->si_pid))
+ && p->p_wchan == (caddr_t)&selwait)
sip->si_flags |= SI_COLL;
- else {
- sip->si_flags &= ~SI_COLL;
- sip->si_pid = mypid;
- }
+ sip->si_pid = mypid;
}
/*
@@ -951,9 +949,11 @@
if (sip->si_pid == 0)
return;
if (sip->si_flags & SI_COLL) {
+ sip->si_pid = 0;
nselcoll++;
sip->si_flags &= ~SI_COLL;
wakeup((caddr_t)&selwait);
+ return;
}
p = pfind(sip->si_pid);
sip->si_pid = 0;
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: kern-bug-people->jdolecek
Responsible-Changed-By: jdolecek
Responsible-Changed-When: Tue Jul 23 01:41:23 PDT 2002
Responsible-Changed-Why:
I'll look at this.
State-Changed-From-To: open->feedback
State-Changed-By: jdolecek
State-Changed-When: Sat Nov 30 05:47:52 PST 2002
State-Changed-Why:
A fix was committed in rev. 1.68 of syssrc/sys/kern/sys_generic.c
The committed change is a bit different, I basically just put it
into same shape as was before rev. 1.57 of the file (or in 4.4BSD).
Can you confirm it fixes your problem?
From: Jaromir Dolecek <jdolecek@netbsd.org>
To: gnats-bugs@gnats.netbsd.org
Cc:
Subject: pr/17517 CVS commit: syssrc/sys/kern
Date: Sat, 30 Nov 2002 15:47:00 +0200 (EET)
Module Name: syssrc
Committed By: jdolecek
Date: Sat Nov 30 13:46:59 UTC 2002
Modified Files:
syssrc/sys/kern: sys_generic.c
Log Message:
Unfortunately, we can't really know if select collision is needed
until after wakeup event, so we can't clear the SI_COLL flag
in selrecord(). Thus, effectively back rev. 1.57 off.
Problem reported in PR kern/17517 by David Laight, program triggering
the problem is in regress/sys/kern/poll/poll3w.c.
To generate a diff of this commit:
cvs rdiff -r1.67 -r1.68 syssrc/sys/kern/sys_generic.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
State-Changed-From-To: feedback->closed
State-Changed-By: jdolecek
State-Changed-When: Tue Jan 21 00:26:53 PST 2003
State-Changed-Why:
No feedback in almost two months, change assumed okay.
Thanks for report.
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.