You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@spamassassin.apache.org by bu...@bugzilla.spamassassin.org on 2005/11/22 20:53:41 UTC

[Bug 4703] New: process leak for helper applications (pyzor)

http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703

           Summary: process leak for helper applications (pyzor)
           Product: Spamassassin
           Version: 3.0.3
          Platform: All
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P5
         Component: Libraries
        AssignedTo: dev@spamassassin.apache.org
        ReportedBy: ccontavalli@commedia.it
                CC: ccontavalli@commedia.it


With a mail server with quite some load, there is a small process leak when
using pyzor (by looking at the code, I believe also with some other helpers, but
haven't seen the problem). On a highly loaded server, this small leak causes lot
of zombie and not so zombie pyzor processes to be left over hanging around the
system (a couple hundreds a day). Over a week period, if not properly limited,
those process may cause several damages due to resource exaustion and so on... 

The problem is due the timeout handler, which doesn't cleanly terminate the
process in case the timeout expires (there is no waitpid if the timeout expires,
and no kill is performed). The attached patch has solved the problem on many
installations and setups I've been working on.



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From ccontavalli@commedia.it  2005-11-23 13:35 -------
(In reply to comment #10)
> I don't know. Why aren't people seeing the zombies in 3.1 after the patch in
> 4518 was applied? I notice that the 3.1 code comments out the calls to
> cleanup_kids. How does it get away with that?
I just downloaded 3.1. I see lot has changed in terms of children handling. 
If you take a look just before the end of the eval block (in Pyzor.pm, where the
alarm signal is handled, around line 383), you can see a waitpid in place of a
cleanup_kids call. So, afaik, the zombie is leftover only when the alarm is
delivered, and waitpid not called (the patch in bug 4518 only takes care of
closing file handles), which happens _very_ rarely in low/medium loaded mail
servers.

By looking in the cleanup_kids code, I believe it has been replaced with the
simple waitpid call since in enter_helper_run_mode in Dns.pm the CHLD handler is
always set to DEFAULT. cleanup_kids thus becomes useless, since it only checks
if $SIG{'CHLD'} is unset or set to IGNORE.

In practice, to avoid zombies, every forked children must be waited for, or
SIG{'CHLD'} set to 'IGNORE'. Currently, I believe that SIG{'CHLD'} is set to
DEFAULT, and when the alarm is delivered, the eval block leaves in advance, and
the process is never waited for. I really don't know if there is something
around the spamassassin code that once in a while loops over all dead children
and waits for them ... (I can see a SIG{'CHLD'} handler in the spamassassin
parent, but that is just to handle forking and other connection handlers) but in:
+    if ($pid) {
+      if (kill('TERM',$pid)) { dbg("pyzor: killed stale helper [$pid]") }
+      else { dbg("pyzor: killing helper application [$pid] failed: $!") }
+    }

I'd add something like:
  if ($pid) {
    if (kill('TERM',$pid)) { 
      dbg("pyzor: succesfully sent TERM signal to helper [$pid]"); 
      sleep(1);  # give it the time to really exit
    } else { 
      # Note that the process may have died by
      # itself after the alarm but before the kill, and
      # note that a succesfully kill call doesn't mean the
      # process was killed...
      dbg("pyzor: couldn't send kill signal to helper" . 
          "application [$pid] failed: $! [maybe it died in the mean time??]");
    }

      # Check if the process has died, and get its 
      # status (cleaning up any eventual zombie)
    if(waitpid($pid, WNOHANG) == 0) {
        # it is still alive
      kill('KILL', $pid);
        # Now, the pid will die for sure, so, do a
        # blocking wait
      waitpid($pid, 0);
      dbg("pyzor: [$pid] has been killed with SIGKILL");
    } else {
      dbg("pyzor: [$pid] died nicely with $? status" . 
          " after SIGTERM was delivered");
    }
  }

To avoid the sleep, the first waitpid could be replaced by a waitpid($pid, 0),
with a timeout implemented once again using alarms or SIGCHLD handlers.
  If a timeout is not implemented, in case the helper does not terminate when
SIGTERM is received, the spamd process would hang until the child really dies
(which could take forever). 

Cheers,
Carlo



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703


wtogami@redhat.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
   Target Milestone|Undefined                   |3.0.5




------- Additional Comments From wtogami@redhat.com  2005-11-22 21:13 -------
Interesting, I've been seeing occasional signs of this on my medium loaded
server.  How long have you been testing this patch?

I'd like to get this into 3.0.5 if the spamassassin developers deem it safe.




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From wtogami@redhat.com  2005-11-23 04:44 -------
Oh, I am just too exhausted today.  The actual filename is Dns.pm while I
searched for DNS.pm.

Developers, can someone verify the correctness of this patch implementation?



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From sidney@sidney.com  2005-11-23 12:35 -------
Carlo,

I copied the code from the 3.1 tree, but I really need someone with more
expertise than I have in signals and processes in perl to look at this.

> I cannot see a waitpid for the terminated process. Wouldn't that leave a
> zombie running? isn't a cleanup_kids call missing from outside the eval block?

I don't know. Why aren't people seeing the zombies in 3.1 after the patch in
4518 was applied? I notice that the 3.1 code comments out the calls to
cleanup_kids. How does it get away with that?

Suggestions are welcome.




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703


wtogami@redhat.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|3.0.5                       |---






------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From ccontavalli@commedia.it  2005-11-23 10:28 -------
(In reply to comment #6)
> This patch doesn't prevent the consumption of file handles, does it?
> 
> You may want to take a look at revision 233991, bug 4518.
no, you are right. This patch doesn't close file descriptors. The problem is
always the same: the alarm signal handler  doesn't perform any form of cleanup,
and 1 - file descriptors are left open and 2 - processes are not killed, 3 - nor
waited for (to terminate zombies). 

The patch attached improves the signal handler by "properly" terminating the
process and waiting for it. A close to the file descriptor could be added, I
believe... 

The patch on bug 4518 does almost the same.. if the DCC file descriptor has been
left open (which implies that the alarm signal handler was called and no cleanup
was performed), kill the process and clean up. The only thing which I cannot see
is a waitpid for the killed process (it may as well have been reaped by the
SIGCHLD handler) and no attempt to send a -9 if the process doesn't terminate is
perfromed (is a SIGTERM enough to terminate the 'hanged' process?).

I've used my patch on production for a while, without fd problems.. probably,
thanks to perl autoclosing them.

I'd either add a close to my patch, or a waitpid and kill('KILL' to the patch of
bug 4518, making sure the same thing is performed for pyzor. 

My 2 cents..

Cheers,
Carlo





------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From jm@jmason.org  2005-11-23 21:34 -------
'I really don't know if there is something around the spamassassin code that
once in a while loops over all dead children and waits for them'

yep, there is, in spamd.

I think the fix from bug 4518 is better than patch 3266; with that, the
processes are killed, which is the most important thing, the FDs leak is
plugged, and while the leftover zombie processes are not cleaned up
immediately, they do get cleaned up eventually by spamd.

Patch 3271 may be better, but unfortunately *includes* the bug 4518 fix instead
of building from that, so it's quite hard to read, and will not apply to 3.1.x
or trunk.   This is bad.  bear in mind 3.0.x is not a supported distro, so
going to that much effort before fixing the 2 supported ones is going
overboard!

I'm -1 on applying these patches as a result.

I would prefer to see a new patch, building on top of the patch from bug 4518,
which adds:

    - the same code, or calls to a shared Util subroutine, in the other modules
that use helper processes, of course
    - a waitpid() call after SIGTERM
    - possibly, a SIGKILL, but I'm really not sure if this is really necessary

(ps: response to a question --  we cannot use CHLD=IGNORE, due to other bugs on
other platforms, iirc; so we always need to wait for children somewhere.)




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From ccontavalli@commedia.it  2005-11-23 11:20 -------
> This patch is the one for bug 4518 adapted for 3.0. I think it better deals
> with the source of the problem, avoiding leaking open file handles as well as
> processes.
uhm .. I cannot see a waitpid for the terminated process. Wouldn't that leave a
zombie running? isn't a cleanup_kids call missing from outside the eval block?

Also, if the kid is really hang, for whichever reason, the TERM would leave it
there .. wouldn't it be better to add a waitpid with WNOHANG parameter and if it
still is alive send a KILL signal? (an attempt to do this in my previous patch)

Cheers,
Carlo 



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From ccontavalli@commedia.it  2005-11-23 00:29 -------
I'm under the impression my email didn't get here .. ok, this was the reply ...

> Interesting, I've been seeing occasional signs of this on my medium loaded
> server.  How long have you been testing this patch?
about 3 weeks. I had posted something about it in the ml, and wrote the
patch right after. Before the patch, on some machines, I had to manually kill
spamd about once a day, freeing up about 300/400 M of memory. Since the
patch has been put in production (about 11 servers), this problem
completely disappeared, and haven't seen other oddities. (hope it's just not me
not being able to notice them)

> I'd like to get this into 3.0.5 if the spamassassin developers deem it safe.
sorry, you're right, it is version 3.0.2. On my Debian system, it is
in /usr/share/perl5/Mail/SpamAssassin/Dns.pm. I had this problem on 3.0.4
on another machine too. The provided patch applies cleanly on 3.0.4 too.
I usually do something like:

cd /usr/share/perl5/Mail/SpamAssassin/
patch -p1 < patch.diff

Probably, the sleep(1) in the patch could be replaced by something
smarter, but I'm not too confident about adding more code in the
alarm signal handler. Hope it is not debian specific.

Hope it helps,
if you need further details, I'm here :) If I can be of any coding
help, just let me know...

Cheers,
Carlo





------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From ccontavalli@commedia.it  2005-11-22 20:55 -------
Created an attachment (id=3266)
 --> (http://issues.apache.org/SpamAssassin/attachment.cgi?id=3266&action=view)
cleanly terminates pyzor processes in case the alarm expires

it's been working for me on many servers. By taking a look around the code, it
looks to me other helpers may suffer for the same problem.



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From spamassassin@dostech.ca  2005-11-23 05:29 -------
This patch doesn't prevent the consumption of file handles, does it?

You may want to take a look at revision 233991, bug 4518.



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From jm@jmason.org  2005-11-23 23:21 -------
'Are you
saying that the fix for bug 4518 does not fix the zombie problem that this bug
is talking about and that we should first come up with a patch for that against
trunk and 3.1  and then perhaps port the fix to 3.0 if we want to?'

the fix for bug 4518 fixes the zombie problem -- more or less -- by letting
spamd clean them up.  it's not as elegant as this one.



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From sidney@sidney.com  2005-11-23 23:08 -------
Justin, should this bug be retargeted as a current trunk bug, then? Are you
saying that the fix for bug 4518 does not fix the zombie problem that this bug
is talking about and that we should first come up with a patch for that against
trunk and 3.1  and then perhaps port the fix to 3.0 if we want to?

If that's the case then the version and target milestone of this bug needs to be
changed.






------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From sidney@sidney.com  2005-11-23 10:51 -------
Created an attachment (id=3271)
 --> (http://issues.apache.org/SpamAssassin/attachment.cgi?id=3271&action=view)
Alternative patch based on bug 4518 code

This patch is the one for bug 4518 adapted for 3.0. I think it better deals
with the source of the problem, avoiding leaking open file handles as well as
processes.

I can't do much testing beyond verifying that it build and runs and passes our
make tests.

Please see if this solves the problem.




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4703] process leak for helper applications (pyzor)

Posted by bu...@bugzilla.spamassassin.org.
http://issues.apache.org/SpamAssassin/show_bug.cgi?id=4703





------- Additional Comments From wtogami@redhat.com  2005-11-22 22:54 -------
I am confused.  spamassassin-3.0.4 doesn't contain a DNS.pm.  Are you sure you
are using 3.0.3 as you indicate in the Version Bugzilla field?




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.