natebailey has asked for the wisdom of the Perl Monks concerning the following question:

I am writing my test harnesses and I'd like one script to make a server and then run the client against the server. Server creation happens fine, and the client connects and does it's stuff fine, but termination (i.e. completion/exiting) never happens. I suspect this is because of open filehandles/sockets, but I can't work out how to fix it. An extract of the relevant forking code is below -- any advice? (full code in http://search.cpan.org/author/NATE/WWW-SearchBroker-0.07/, extract from Broker.pm)
carp '[BROKER: Forking infinite loop]' if DEBUG >= DEBUG_MEDIU +M; my $ret; if ($ret = fork()) { carp "[BROKER: Parent returning (child PID=$ret)]" if +DEBUG >= D EBUG_MEDIUM; # perldoc -f fork tells us to reopen to /dev/null, wil +l this wor k? close($self->{_server}); $self->DESTROY; return $ret; } carp '[BROKER: Child looping]' if DEBUG >= DEBUG_MEDIUM; while ($self->event_loop()) { } return undef;

Replies are listed 'Best First'.
Re: Forking with a socket
by Zaxo (Archbishop) on Jul 07, 2003 at 01:15 UTC

    In your last line, you return to the caller in the child process. That leaves the child continuing with a copy of the remaining parent code. Change to

    while ($self->event_loop()) { } exit 0;

    After Compline,
    Zaxo

Re: Forking with a socket
by Corion (Patriarch) on Jul 07, 2003 at 11:00 UTC

    For something similar (but not quite easily adapted to other cases, as the server used is too simple) take a look at Test::HTTP::LocalServer, which I use for end-to-end tests of WWW::Mechanize::Shell.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web