Possible bug in 5.8.6: accept/fork/wait/exit ?

J

James Marshall

Any multi-process/networking gurus here?

I'm trying to write a simple forking HTTP proxy but am having a problem
when running it with Perl 5.8.6. The program spontaneously exits after
one or a few connections. The problem does not happen when I use Perl
5.6.1. I'm running this on Linux with the 2.6.11.4-20a-default kernel
(SuSE 9.3).

Here's a short program that demonstrates it, between the bars below. To
see the problem: First, run this program from one shell prompt. Then,
from another shell prompt, run "telnet localhost 8001". The connection is
made then closed, as expected. You can see the output in the first shell
window. Now, repeat the telnet command over and over. After 1-20
telnets, the script exits with no message.

----------------------------------------------------------------
#!/usr/bin/perl -w

use strict ;
use Socket ;

use vars qw($LISTEN_PORT $paddr $pid $conn_id) ;

$LISTEN_PORT= shift || 8001 ;

# clean up zombies
$SIG{CHLD}= sub { wait } ;

print "Starting $0, listening on port $LISTEN_PORT.\n" ;

# Set up the listening socket
socket(S_LISTEN, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die ;
setsockopt(S_LISTEN, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) or die ;
bind(S_LISTEN, sockaddr_in($LISTEN_PORT, INADDR_ANY)) or die $! ;
listen(S_LISTEN,SOMAXCONN) or die ;

# Accept one connection at a time, and fork a new process to handle it.
for ( ; $paddr= accept(S_CLIENT, S_LISTEN) ; close S_CLIENT) {
$conn_id++ ;
select((select(S_CLIENT), $|=1)[0]) ; # unbuffer the socket
if ($pid= fork) { # parent process
print "starting parent, conn=$conn_id, pid=$$\n" ;
next ;
} else { # child process
print "starting child, conn=$conn_id, pid=$$\n" ;
exit ;
}
}

----------------------------------------------------------------

The problem goes away if I comment out either the exit statement, or the
$SIG{CHLD} statement. Of course, neither one is acceptable, because all
the extra processes would keep hanging around (either as processes or
zombies). If the for() loop is replaced with a simple "while (1)" loop,
the problem goes away, so the accept() seems to be part of the problem.

Does anyone have any ideas of why this is happening, or suggested
workarounds? Do you see something I'm missing?

Thanks a lot for any help!


James
.............................................................................
James Marshall (e-mail address removed) Berkeley, CA @}-'-,--
"Teach people what you know."
.............................................................................
 
X

xhoster

Any multi-process/networking gurus here?

I'm trying to write a simple forking HTTP proxy but am having a problem
when running it with Perl 5.8.6. The program spontaneously exits after
one or a few connections. The problem does not happen when I use Perl
5.6.1.

I also don't see the problem on 5.8.0 or 5.8.3.

....
shell window. Now, repeat the telnet command over and over. After 1-20
telnets, the script exits with no message.

That is not surprising, since you don't ask for a message.

for ( ; $paddr= accept(S_CLIENT, S_LISTEN) ; close S_CLIENT) {

for ( ; $paddr= accept(S_CLIENT, S_LISTEN) or die $!; close S_CLIENT) {

Xho
 
J

jmarshall.com

(My news server is down, so I'm posting from Google Groups.)

Thanks Xho for the suggestion. It helped lead me to a place in the
Camel book where this very problem is discussed.

The apparent problem was that the accept() system call was getting
interrupted by the CHLD signal, which caused accept() to return undef,
which caused the loop (and thus the program) to exit. According to the
Camel book, this happens on systems without restartable system calls,
which explains why it was a problem on some systems and not others.

The workaround is also discussed in the Camel book. It entails
rewriting the $SIG{CHLD} handler as

sub {$waitedpid= wait}

.... and rewriting the for() loop as

for (waitedpid= 0 ;
($paddr= accept(S_CLIENT, S_LISTEN)) or $waitedpid ;
$waitedpid= 0, close(S_CLIENT) )
{
next if $waitedpid ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,823
Latest member
Nadia88

Latest Threads

Top