Checking what is eating your memory…. Part III

Finally I found out what it was AND it was / is (using this script) a major flaw in the (gz) output handler; a user on the server uses in his code;

ob_start(“ob_gzhandler”, 9);

something();

ob_flush();

If something() generates a certain amounts of data, the ob_gzhandler will get stuck in a loop, eating all memory.

In the user his particular case, because his database was not working, some pages always have this ‘magic size’ (probably some factor of 9 in this case).

I tried this on the latest stable of PHP 5 and the bug is still there; you can crash any php hosting machine on the net with this 🙁

As to prevent this kind of thing from happening, I put the following code on the machines;


#!/usr/bin/perl

$maxmem=10;

while(1) {
@r=`ps auxwhww|awk '{print $4,$2,$11,$12,$13,$15}'|grep httpd-users|grep -v grep`;
foreach(@r) {
chomp;
/(.*?) (.*?) (.*)/;
if ($1>$maxmem) {
print "$_ killedn";
`kill -9 $2`;
}
sleep 1;
}

Update: Only PHP5.x is affected, not lower (tested); didn’t try higher. I have code to reproduce it every run.

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>