Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

lib/setup moodelib: Introducing moodle_request_shutdown()

Now Moodle will have a 'shutdown_function' function. Right now
registered (and useful) only when running under apache + mod_php.

This initial function checks whether memory usage in this process exceeded
10MiB or $CFG->apachemaxmem (int, in bytes). This is really useful to prevent
using more RAM than we have.
  • Loading branch information...
commit 17b39b6c951407cc62e95877c5e9b83b9cb695cf 1 parent a1cac4a
martinlanghoff authored
Showing with 29 additions and 0 deletions.
  1. +25 −0 lib/moodlelib.php
  2. +4 −0 lib/setup.php
View
25 lib/moodlelib.php
@@ -6450,6 +6450,31 @@ function httpsrequired() {
}
}
+/*
+ * 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();
+ }
+ }
+}
+
/**
* For outputting debugging info
*
View
4 lib/setup.php
@@ -179,6 +179,10 @@
raise_memory_limit('64M'); // We should never NEED this much but just in case...
+/// For now, only needed under apache (and probably unstable in other contexts)
+ if (function_exists('apache_child_terminate')) {
+ register_shutdown_function('moodle_request_shutdown');
+ }
/// If $CFG->unicodedb is not set, get it from database or calculate it because we need
/// to know it to "set names" properly.
Please sign in to comment.
Something went wrong with that request. Please try again.