diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 5c274caa1bf..094a6478115 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6511,6 +6511,31 @@ eval(' '); } +/* + * This function expects to called during shutdown + * should be set via register_shutdown_function() + * in lib/setup.php . + * + * Right now we do it only if we are under apache, to + * make sure apache children that hog too much mem are + * killed. + * + */ +function moodle_request_shutdown() { + + // initially, we are only ever called under apache + // but check just in case + if (function_exists('apache_child_terminate') && function_exists('memory_get_usage')) { + if (empty($CFG->apachemaxmem)) { + $CFG->apachemaxmem = 10000000; // default 10MiB + } + if (memory_get_usage() > (int)$CFG->apachemaxmem) { + trigger_error('Mem usage over $CFG->apachemaxmem: marking child for reaping.'); + apache_child_terminate(); + } + } +} + /** * This function will make a complete copy of anything it's given, * regardless of whether it's an object or not. diff --git a/lib/setup.php b/lib/setup.php index 3b44e275815..ac700d32d63 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -234,6 +234,10 @@ global $HTTPSPAGEREQUIRED; $originaldatabasedebug = -1; } +/// For now, only needed under apache (and probably unstable in other contexts) + if (function_exists('apache_child_terminate')) { + register_shutdown_function('moodle_request_shutdown'); + } //// Defining the site if ($SITE = get_site()) {