Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Stopping an HTTP::Server::Simple server

by Corion (Pope)
on Jan 26, 2011 at 14:48 UTC ( #884359=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Stopping an HTTP::Server::Simple server
in thread Stopping an HTTP::Server::Simple server

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.


Comment on Re^4: Stopping an HTTP::Server::Simple server
Select or Download Code
Re^5: Stopping an HTTP::Server::Simple server
by tilly (Archbishop) on Jan 26, 2011 at 14:54 UTC
    Another possibility is that the op is trying to launch the webserver in one process invocation, and then kill it in another. In which case $pid will have to be written to/from a file.
      Thanks a lot for your replies.

      Sorry, my previous answer was ambiguous: $pid is undefined in the handle_request () method of MyWebServer, but it is correctly set to something like "-1470" in test.pl.

      In fact, writing this in test.pl effectively stops the server immediately:
      use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); my $pid = $server->background(); kill 9, $pid;

      But in order to execute this instruction in response to some HTTP request, (I think) I need to call it from within a handler in MyWebServer.

      I find that the following:
      kill 9, $$;
      effectively stops the server if it is added anywhere in MyWebServer.pm's "body" (e.g. at the first line), but not if it is added in the handle_request() method.

      I've also tried tilly's suggestion to have test.pl write the PID in a file, then have handle_request() read this file. It does succeed at passing the PID, and I do not get the "Can't kill a non-numeric process ID" message anymore, however the call to kill does not stop the server in this case.

        ->handle_request() will be called in a child process, so it's no surprise that $pid is undefined there (see previous discussion), as $pid is only set in the parent. kill 9, $$ only kills the child process, so that won't work either.

        You haven't shown the code that you used to write the PID to a file. You should show that code, or output more diagnostics while running your code to track what values $$ and $pid have at which time, and also what the values read from the .pid file are. Do you check for failure when opening the file in the child?

        If your server is non-forking, you should be able just call exit to stop the server. That will do the same thing as the kill line.

        If the server is forking under the hood, then in handling a web request you are just killing the child process that is handling that request and another will be spawned shortly. In that case you would need to kill some set of processes to shut it down. What set depends on how it is set up. For instance if it forks for every request received, then killing your parent process (which getppid should give you) will do it. If it preforks and has a pool of processes, then you need to kill them all. Typically you can do that by sending the right signal to the parent process.

        Given your described behavior, it sounds like it may be forking. (That, or kill -9 is failing somehow.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://884359]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-12-19 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (77 votes), past polls