[mod_python] remnant 'orphan' apache subprocesses

Graham Dumpleton graham.dumpleton at gmail.com
Thu Jan 24 02:16:37 EST 2008


On 24/01/2008, Alec Matusis <matusis at yahoo.com> wrote:
> > Which almost suggests it was actually a fork of an Apache child
> > process as that is possibly only way the other threads could have been
> > killed off.
>
> I think this is right. I caught this evidence:
>
> # ps -ef | grep http
> root     16197     1  0 Jan21 ?
> nobody     787 16197  9 18:06 ?
> nobody    1096 16197  9 18:09 ?
> nobody    1240 16197  8 18:12 ?
> nobody    1389 16197  8 18:13 ?
> nobody    1525 16197  9 18:14 ?
> nobody    1732  1525  0 18:16 ?
> ....
>
> Note pid 1732 with 0 CPU total being a child of a sub process (pid 1525)
> 20 minutes later, the parent sub process pid 1525 exited, and pid 1732
> became an orphan:
>
> # ps -ef | grep http
> root     16197     1  0 Jan21 ?
> nobody    1732     1  0 18:16 ?
> nobody    9895 16197  6 20:07 ?
> .....
>
> > I can't find though any evidence of fork() being used direct in PIL
> > code at least, but then the popen2 module uses fork() in Popen3 and
> > Popen4 classes.
>
> I found that we send email like this:
>
> try:
>      handle = os.popen2('/usr/sbin/sendmail.postfix -f mailzero -t')
>      handle[0].write(msg)
>      handle[0].write("\n.\n")
>  finally :
>      if handle:
>          handle[0].close()
>          handle[1].close()
>
> Could this be the culprit?

It could be depending on what happens on your specific platform. One
of the fallbacks for popen2() is actually to use Popen3 class.

    def popen2(cmd, bufsize=-1, mode='t'):
        """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
        be a sequence, in which case arguments will be passed directly to the
        program without shell intervention (as with os.spawnv()). If 'cmd' is a
        string it will be passed to the shell (as with os.system()). If
        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
        file objects (child_stdout, child_stdin) are returned."""
        inst = Popen3(cmd, False, bufsize)
        return inst.fromchild, inst.tochild

> PS. I finally obtained lsof for an orphan:
>
> httpd   552 nobody    0r  FIFO    0,5                    4292655466 pipe
> httpd   552 nobody    1w  FIFO    0,5                    4292655467 pipe

Pipe to subprocess. That this exists may mean it isn't collecting exit
status properly or sub process hasn't really exited.

Can you easily change your code which invokes sendmail to use smtplib
module instead? If it is easy, no harm in trying and if things work
okay then, then even better. :-)

Graham


More information about the Mod_python mailing list