From 72a51148e976f95ca6ea9feec6bd18e1a55addb2 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 4 Apr 2023 09:25:04 +0800 Subject: [PATCH] MDL-77837 cron: Ensure user is set when running tasks We should be proactive in ensuring that the environment is clean when running a task. We already ensure that we have a clean renderer and other parts of the output chain, but we were not setting a clean user. This change adds a call to setup the cron user before each task is actually executed. --- lib/cronlib.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/cronlib.php b/lib/cronlib.php index a0d37334de2..21a71603b75 100644 --- a/lib/cronlib.php +++ b/lib/cronlib.php @@ -242,6 +242,10 @@ function cron_run_inner_scheduled_task(\core\task\task_base $task) { $predbqueries = null; $predbqueries = $DB->perf_get_queries(); $pretime = microtime(1); + + // Ensure that we have a clean session with the correct cron user. + cron_setup_user(); + try { get_mailer('buffer'); cron_prepare_core_renderer(); @@ -325,6 +329,10 @@ function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) { } cron_setup_user($user); + } else { + // No user specified, ensure that we have a clean session with the correct cron user. + cron_setup_user(); + } try {