modperl_recognizing_server_shutdown

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: mod_perl list <modperl@apache.org>
From: Dave Rolsky <autarch@urth.org>
Subject: How to recognize server shutdown?
Date: Wed, 10 Jan 2001 02:03:23 -0600 (CST)

Is there any way to distinguish between a child being shutdown (say
maxrequests has been exceeded) versus all of Apache going down (kill
signal sent to the original process or something).

The reason I ask is that while I can do:

BEGIN
{
     # make a file
}

I can't do:

END
{
     # delete a file
}

and get good results.


I suspect the answer to this is no but I'm curious as to whether anybody's
come up with a way to detect a 'real' shutdown.

===

To: Dave Rolsky <autarch@urth.org>
From: Danny Rathjens <dkr@hq.mycity.com>
Subject: Re: How to recognize server shutdown?
Date: Wed, 10 Jan 2001 18:55:08 +0000

Dave Rolsky wrote:
> 
> On Wed, 10 Jan 2001, G.W. Haywood wrote:
> 
> > Don't you get a message in error_log to the effect that a signal has
> > been received?
> 
> Sure, but I don't think that would help me do what I want.
> 
> Let me illustrate:
> 
> 1.  server is started
> 2.  config is read, modules are loaded, BEGIN blocks are run in this
> process (just once)
> 3.  X children are created
> 4.  children are born, they die, yadda yadda.  The END block is run
> whenever a child dies.
> 5.  the server is told to shut down
> 6.  the first server to start (the one running as root or whatever) sends
> signals to the children telling them to shutdown.  END blocks run in all
> children.
> 7.  the first server shuts down - I'd like to run something here because
> it should only be happening to during a 'final' shutdown and only after
> the children have finished serving their last requests.
> 
Perhaps you could send a USR1 prior to your TERM signal and have your
END blocks perform your shutdown tasks if they see the USR1 signal.
But then you have the problem of new children being started due to the
USR1
not to mention it would preclude using USR1 for doing normal graceful
restarts, 8^)

Hrm, would be nice if $r->server had a method to tell you if apache is
in the process of shutting down or not.

===
To: Danny Rathjens <dkr@hq.mycity.com>
From: Robin Berjon <robin@knowscape.com>
Subject: Re: How to recognize server shutdown?
Date: Wed, 10 Jan 2001 20:01:13 +0100

At 18:55 10/01/2001 +0000, Danny Rathjens wrote:
>Perhaps you could send a USR1 prior to your TERM signal and have your
>END blocks perform your shutdown tasks if they see the USR1 signal.
>But then you have the problem of new children being started due to the
>USR1
>not to mention it would preclude using USR1 for doing normal graceful
>restarts, 8^)

What about USR2 ? Afair it isn't used and could probably be caught. In that
case it would be possible to use it prior to a TERM to signal imminent
shutdown.

===
To: Dave Rolsky <autarch@urth.org>
From: Stas Bekman <stas@stason.org>
Subject: Re: How to recognize server shutdown?
Date: Wed, 10 Jan 2001 23:02:26 +0100 (CET)

On Wed, 10 Jan 2001, Dave Rolsky wrote:

> On Wed, 10 Jan 2001, G.W. Haywood wrote:
>
> > Don't you get a message in error_log to the effect that a signal has
> > been received?
>
> Sure, but I don't think that would help me do what I want.
>
> Let me illustrate:
>
> 1.  server is started
> 2.  config is read, modules are loaded, BEGIN blocks are run in this
> process (just once)
> 3.  X children are created
> 4.  children are born, they die, yadda yadda.  The END block is run
> whenever a child dies.
> 5.  the server is told to shut down
> 6.  the first server to start (the one running as root or whatever) sends
> signals to the children telling them to shutdown.  END blocks run in all
> children.
> 7.  the first server shuts down - I'd like to run something here because
> it should only be happening to during a 'final' shutdown and only after
> the children have finished serving their last requests.

All we need is to add a $Apache::Server::Quitting or alike, in addition to
the existing $Apache::Server::Starting and $Apache::Server::ReStarting,
should be an easy patch in XS.

===

To: Dave Rolsky <autarch@urth.org>
From: Perrin Harkins <perrin@primenet.com>
Subject: Re: How to recognize server shutdown?
Date: Wed, 10 Jan 2001 14:50:39 -0800 (PST)

On Wed, 10 Jan 2001, Dave Rolsky wrote:
> Is there any way to distinguish between a child being shutdown (say
> maxrequests has been exceeded) versus all of Apache going down (kill
> signal sent to the original process or something).

Register an END block in your startup.pl, and have it check it's PID to
see if it's the parent.

===

To: Perrin Harkins <perrin@primenet.com>
From: Stas Bekman <stas@stason.org>
Subject: Re: How to recognize server shutdown?
Date: Thu, 11 Jan 2001 01:21:40 +0100 (CET)

On Wed, 10 Jan 2001, Perrin Harkins wrote:

> On Wed, 10 Jan 2001, Dave Rolsky wrote:
> > Is there any way to distinguish between a child being shutdown (say
> > maxrequests has been exceeded) versus all of Apache going down (kill
> > signal sent to the original process or something).
>
> Register an END block in your startup.pl, and have it check it's PID to
> see if it's the parent.

It doesn't work. I've tested:

startup.pl:
 -----------
package Cleanup;

$Cleanup::parent_pid = $$;

END{

    eval q{
           if ($$ == $Cleanup::parent_pid) {
               `echo $Cleanup::parent_pid $$ >> /tmp/parent`;
           } else {
               `echo $Cleanup::parent_pid $$ >> /tmp/children`;
           }
          };
}

the parent process doesn't run the END block.

===

To: Perrin Harkins <perrin@primenet.com>
From: Dave Rolsky <autarch@urth.org>
Subject: Re: How to recognize server shutdown?
Date: Wed, 10 Jan 2001 20:48:40 -0600 (CST)

On Wed, 10 Jan 2001, Perrin Harkins wrote:

> Randal's solution is probably better, but it's a bummer that the parent
> doesn't run END blocks.  Will it run cleanup handlers?

I'm pretty sure the parent runs END blocks.  I just didn't want to have
the cleanup code run during child shutdown.

What I've done for now is this hack:


use vars qw($PID);

BEGIN
{
    ....
    $PID = $$;
}

END
{
    if ($$ == $PID )
    {
        # do cleanup
    }
}

The only issue with that is what happens if the code is used in an
environment where a process starts up, forks a child, and then dies,
allowing the child to continue.

Hmm, I think I'll have to just doc it.

This is all related to clearing up an shared memory segment (and
semaphores) created by IPC::Shareable.  Except I realized IPC::Shareable
can do it.  Its just that for its boolean options it expects 'yes' or
'no', not 1 or 0.  That's freaking brilliant.  ARGH!

===
To: Perrin Harkins <perrin@primenet.com>
From: Stas Bekman <stas@stason.org>
Subject: Re: How to recognize server shutdown?
Date: Thu, 11 Jan 2001 11:23:51 +0100 (CET)

On Wed, 10 Jan 2001, Perrin Harkins wrote:

> On Thu, 11 Jan 2001, Stas Bekman wrote:
> > the parent process doesn't run the END block.
>
> Randal's solution is probably better,

But it's not a very nice solution if you want to release something on CPAN
that relies on this hack. A support from mod_perl seems like a much
simpler solution.

> but it's a bummer that the parent
> doesn't run END blocks.  Will it run cleanup handlers?

Cleanup handlers are run by child processes. What it has to do with
parent? Or do I miss something?

===

To: "Randal L. Schwartz" <merlyn@stonehenge.com>
From: "G.W. Haywood" <ged@www.jubileegroup.co.uk>
Subject: Re: How to recognize server shutdown?
Date: Thu, 11 Jan 2001 10:24:19 +0000 (GMT)

Hi all,

On 10 Jan 2001, Randal L. Schwartz wrote:

> Here's an idea... in the startup code, create a pipe and fork.
> block the kid on a read.  ni-night, kid.

Nice, Randall!

===

To: "Stas Bekman" <stas@stason.org>
From: "Perrin Harkins" <perrin@primenet.com>
Subject: Re: How to recognize server shutdown?
Date: Thu, 11 Jan 2001 02:47:31 -0800

> > but it's a bummer that the parent
> > doesn't run END blocks.  Will it run cleanup handlers?
>
> Cleanup handlers are run by child processes. What it has to do with
> parent? Or do I miss something?

I meant "is there a way to run a cleanup handler in the parent after it's
work is done?", but I don't see one.  Dave says the END block trick worked
for him, so maybe it only fails under certain circumstances.

===

To: Perrin Harkins <perrin@primenet.com>
From: Doug MacEachern <dougm@covalent.net>
Subject: Re: How to recognize server shutdown?
Date: Thu, 11 Jan 2001 08:38:59 -0800 (PST)

On Thu, 11 Jan 2001, Perrin Harkins wrote:

> > > but it's a bummer that the parent
> > > doesn't run END blocks.  Will it run cleanup handlers?
> >
> > Cleanup handlers are run by child processes. What it has to do with
> > parent? Or do I miss something?

cleanup handlers are run when a pool is cleared.  the request lifetime
pool is one pool, the server lifetime pool is another, etc.

> I meant "is there a way to run a cleanup handler in the parent after it's
> work is done?", but I don't see one.  Dave says the END block trick worked
> for him, so maybe it only fails under certain circumstances.

of course, there is such a "trick"
http://forum.swarthmore.edu/epigone/modperl/thandflunjimp/Pine.LNX.4.10.10012212048330.7347-100000@mojo.covalent.net

===

To: Perrin Harkins <perrin@primenet.com>
From: Dave Rolsky <autarch@urth.org>
Subject: Re: How to recognize server shutdown?
Date: Thu, 11 Jan 2001 10:53:31 -0600 (CST)

On Thu, 11 Jan 2001, Perrin Harkins wrote:

> I meant "is there a way to run a cleanup handler in the parent after it's
> work is done?", but I don't see one.  Dave says the END block trick worked
> for him, so maybe it only fails under certain circumstances.

Actually, I should have pointed out that I haven't yet tested it in
mod_perl.  I tested it in an environment where I was spawning some of my
own proceses.  That worked.  I'll test it in mod_perl soon.


===

To: Stas Bekman <stas@stason.org>
From: Doug MacEachern <dougm@covalent.net>
Subject: Re: How to recognize server shutdown?
Date: Thu, 11 Jan 2001 09:00:23 -0800 (PST)

On Wed, 10 Jan 2001, Stas Bekman wrote:
 
> All we need is to add a $Apache::Server::Quitting or alike, in addition to
> the existing $Apache::Server::Starting and $Apache::Server::ReStarting,
> should be an easy patch in XS.

nooo, as i've mentioned before Starting,ReStarting variables were
mistakes, and they will be either deprecated or more likely non-existent
in 2.0


===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu