Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Jan 2005 05:32:12 GMT
From:      Daniel Fuller / Greg Ward <defuller@lbl.gov>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   kern/76525: Subsequent calls to select() a FIFO after a previous FIFO EOF causes calling process to hang
Message-ID:  <200501210532.j0L5WC8r022857@www.freebsd.org>
Resent-Message-ID: <200501210540.j0L5eFI1032845@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         76525
>Category:       kern
>Synopsis:       Subsequent calls to select() a FIFO after a previous FIFO EOF causes calling process to hang
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jan 21 05:40:15 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator:     Daniel Fuller / Greg Ward
>Release:        5.3 Release
>Organization:
Berkeley Lab
>Environment:
FreeBSD render08.lbl.gov 5.3-STABLE FreeBSD 5.3-STABLE #2: Tue Jan 18 15:11:54 PST 2005     root@render08.lbl.gov:/farm/FreeBSD/releases/amd64/RELENG_5/obj/farm/FreeBSD/releases/amd64/RELENG_5/src/sys/SMP  amd64
>Description:
(Taken from an email by Greg Ward gward@lmi.net)

Hi Danny,

Well, it only took me 8 hours, but I found the problem with the latest version of [Free]BSD.  I tracked down several false leads before I got on the right track -- it's only named FIFO's that seem to exhibit this problem.  I depend on them for the -P and -PP option of rtrace, which is needed for memory sharing as I've set it up in Radiance.  I don't think named FIFO's are used very often, which might explain why this has gone undetected (or at least unfixed).

There are two test programs that demonstrate the problem.  The first is called pipe.c, and on OS X, it produces the following (correct) output:

pipe available for read
Read 4 bytes from pipe: 'TEST'
pipe available for read
Read 0 bytes from pipe: ''

Under FreeBSD 5.3, for some reason I get an exception condition on my pipe every time, which is strange but not fatal:

Exception on pipe
pipe available for read
Read 4 bytes from pipe: 'TEST'
Exception on pipe
pipe available for read
Read 0 bytes from pipe: ''

On FreeBSD 4.10, I only get an exception at EOF, which I might expect:

pipe available for read
Read 4 bytes from pipe: 'TEST'
Exception on pipe
pipe available for read
Read 0 bytes from pipe: ''

The real trouble begins with the second FIFO test in fifo.c.  Under OS X, I get the correct output:

FIFO available for read
Read 4 bytes from FIFO: 'TEST'
FIFO available for read
Read 0 bytes from FIFO: ''

Under FreeBSD 4.10, I get exactly the same output -- even the exception condition is gone:

FIFO available for read
Read 4 bytes from FIFO: 'TEST'
FIFO available for read
Read 0 bytes from FIFO: ''

However, under FreeBSD 5.3-STABLE, the poor thing hangs at the EOF, and select(2) never returns:

FIFO available for read
Read 4 bytes from FIFO: 'TEST'
(process hangs in second call to select)

Keep in mind that there should be no difference in the behavior between a named FIFO and a pipe -- the only difference is how they are mechanically connected by the two processes.  Having the select() call hang when an EOF condition exists is not acceptable.

I hope you can forward this to the appropriate FreeBSD gurus.

Thanks,
-Greg

>How-To-Repeat:
source code for test programs described above:

/*
 * Check pipe behavior
 *
 * Greg Ward <gward@lmi.net>
 * Compare also fifo.c
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>

look(int fd)
{
	fd_set	readfds, excepfds;

	FD_ZERO(&readfds);
	FD_ZERO(&excepfds);
	FD_SET(fd, &readfds);
	FD_SET(fd, &excepfds);
	if (select(fd+1, &readfds, NULL, &excepfds, NULL) < 0) {
		perror("select");
		exit(1);
	}
	if (FD_ISSET(fd, &excepfds))
		puts("Exception on pipe");
	if (FD_ISSET(fd, &readfds))
		puts("pipe available for read");
}

void
spit(int fd)
{
	char	buf[512];
	int	n = read(fd, buf, sizeof(buf));
	buf[n] = '\0';
	printf("Read %d bytes from pipe: '%s'\n", n, buf);
}


main()
{
	int	pp[2];

	pipe(pp);
	if (fork() == 0) {
		close(pp[0]);
		write(pp[1], "TEST", 4);
		close(pp[1]);
		_exit(0);
	}
	close(pp[1]);
	look(pp[0]);
	spit(pp[0]);
	look(pp[0]);
	spit(pp[0]);
	return(0);
}


..Next Test Program...


/*
 * Reproduce bug in FreeBSD 5.3-STABLE
 *
 * Greg Ward	<gward@lmi.net>
 * See also pipe.c for comparison.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>

const char	FIFO[] = "/tmp/fifo";

void
look(int fd)
{
	fd_set	readfds, excepfds;

	FD_ZERO(&readfds);
	FD_ZERO(&excepfds);
	FD_SET(fd, &readfds);
	FD_SET(fd, &excepfds);
	if (select(fd+1, &readfds, NULL, &excepfds, NULL) < 0) {
		perror("select");
		exit(1);
	}
	if (FD_ISSET(fd, &excepfds))
		puts("Exception on FIFO");
	if (FD_ISSET(fd, &readfds))
		puts("FIFO available for read");
}

void
spit(int fd)
{
	char	buf[512];
	int	n = read(fd, buf, sizeof(buf));
	buf[n] = '\0';
	printf("Read %d bytes from FIFO: '%s'\n", n, buf);
}

main()
{
	int	fifo_fd;

	unlink(FIFO);
	mkfifo(FIFO, 0666);
	if (fork() == 0) {
		fifo_fd = open(FIFO, O_WRONLY);
		write(fifo_fd, "TEST", 4);
		close(fifo_fd);
		_exit(0);
	}
	fifo_fd = open(FIFO, O_RDONLY);
	look(fifo_fd);
	spit(fifo_fd);
	look(fifo_fd);
	spit(fifo_fd);
	return(0);
}

..END...
      

>Fix:
None known for FreeBSD 5.3X.
>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200501210532.j0L5WC8r022857>