httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Trawick <>
Subject Re: Terminating cgi scripts
Date Sat, 12 Feb 2005 14:43:52 GMT
On Thu, 10 Feb 2005 18:09:46 +0530, Kiran Mendonce <> wrote:
> We had a customer scenario (on HP-UX) recently where cgi scripts that
> were spawned by mod_cgid continued to run even when httpd was stopped.
> The ppid of these scripts was automatically 1 when the httpd processes
> terminated. The customer does not find any reason why these processes
> need to continue to run especially since the scripts need to be run
> again when httpd restarts. This does seem like a reasonable demand.


> The problem occurs both with mod_cgi and mod_cgid. Being a novice, I
> have the following questions. It would be really helpful to get your
> take on the following :
> 1. How do I get all the child pids spawned by the cgid daemon and the
> httpd child (mod_cgi) so I can kill them when I get a signal ? Is it
> okay if I modify apachectl so that I can get all the processes that are
> owned by 'www' and still running using the 'ps' command and then kill
> them after "httpd -k stop" ?

I wouldn't pursue that unless there is absolutely no practical way to
fix the code.  In the worst case, can't the MPM figure out right
before exiting that it is the parent of some active processes, and
continuing to exit will keep them stranded?  But I'd suggest pursuing
the mod_cgid issues first.

> 2. If that is not acceptable, I would need to change the code. I looked
> at the mod_cgid sources and I found that the pids of all the processes
> that cgid spawns are hashed in the hash table script_hash. If I could
> make script_hash a static global variable, I could get the pids in the
> signal handler and kill the processes. I have changed the code and it is
> working. Would I be breaking something ?

If the cgid daemon is about to exit, it is reasonable for it to wipe
out any processes it has created and which are still active.  But do
as little as possible in a signal handler (i.e., avoid adding any
logic there).  When the existing signal hander is called, it sets a
flag which causes the mainline logic to exit.  You should see mod_cgid
exiting the loop "while (!daemon_should_exit) {", and while still in
that function you have addressibility to the hash table.

> 3. We use the worker MPM. For mod_cgi, I would need to make changes in
> worker.c since there is no daemon here.

It is a bad thing if worker.c has logic specific to CGIs.  Also, it is
invalid to use mod_cgi with a threaded MPM on Unix.  Maybe it will
seem to work, but bad things can happen under heavy load, with
descriptors getting inherited by the wrong child processes.

> In mod_cgi code, I find that
> everytime a process is spawned,  apr_pool_note_subprocess() is called.

On Unix, mod_cgi should only be used with a non-threaded MPM.  And
that non-threaded MPM (prefork) child process will not exit* without
cleaning up the pool that the processes have been associated with.

*if something is stalling and keeping the child process from exiting
in a timely manner, the Apache parent will kill the child process,
such that this cleanup will not be performed.

View raw message