diff --git a/rules.module b/rules.module
index f8ef50f..2196edc 100644
--- a/rules.module
+++ b/rules.module
@@ -1266,6 +1266,10 @@ function rules_show_debug_output() {
  * Implements hook_exit().
  */
 function rules_exit() {
+  // Bail out if this is cached request and modules are not loaded.
+  if (!module_exists('rules') || !module_exists('user')) {
+    return;
+  }
   if (rules_show_debug_output()) {
     if ($log = RulesLog::logger()->render()) {
       // Keep the log in the session so we can show it on the next page.
