http://www.perlmonks.org?node_id=884333


in reply to Stopping an HTTP::Server::Simple server

I'm facing the same problem, though not in a testing situation.

Following Corion's advice, I'm trying to pass the server's PID (returned by its background() method) to a handler that will be responsible for killing it.

My problem is that I can't find a way to pass the PID to the handler.

I've tried several things which all end up the same way when I access the url associated to the handler: the PID's value has not been passed and Perl "Can't kill a non-numeric process ID".

I've tried this (where the setup is maximally contrived for brevity):

A) Store PID as a global in the main package and access it from the server package.

In test.pl:
use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); our $pid = $server->background();
And in Webserver.pm:
package MyWebServer; use strict; use warnings; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, $main::pid; } 1;

B) Store PID as a global in the server package and set it in the main package.

In test.pl:
use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); $MyWebServer::pid = $server->background();
And in Webserver.pm:
package MyWebServer; use strict; use warnings; our $pid; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, $pid; } 1;

C) Store the PID in a separate module used by test.pl and webserver.pm.

In test.pl:
use strict; use warnings; use Pid; use MyWebServer; my $server = MyWebServer->new(8080); Pid->set($server->background());
In Webserver.pm:
package MyWebServer; use strict; use warnings; use Pid; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, Pid->get(); } 1;
And in Pid.pm:
package Pid; use strict; use warnings; my $pid; sub get { my $package = shift; return $pid; } sub set { my $package = shift; $pid = shift; } 1;

I've tried even more esoteric things until I started to feel like Wile E. Coyote, so I think it's time I submit this problem to you.

Any advice would be welcome, and so would any explanation as to what I fail to understand.

Replies are listed 'Best First'.
Re^2: Stopping an HTTP::Server::Simple server
by Corion (Patriarch) on Jan 26, 2011 at 13:14 UTC

    Maybe take a look at what you actually get in $pid before trying to kill it? .

      It's actually not defined.

        This is surprising, as HTTP::Server::Simple should then die in the parent with Can't fork: ..., which you should have seen:

        sub background { my $self = shift; my $child = fork; croak "Can't fork: $!" unless defined($child); return $child if $child; srand(); # after a fork, we need to reset the random seed # or we'll get the same numbers in both branches if ( $^O !~ /MSWin32/ ) { require POSIX; POSIX::setsid() or croak "Can't start a new session: $!"; } $self->run(@_); # should never return exit; # just to be sure }

        So, either you're looking at a webserver that is not the webserver you launched, or the $pid is not where you store the information.

        If you're trying to kill the child from within the child, $pid will also not be defined there (see fork). To kill yourself, use $$ as the pid.