Moosh redis issue
Hi Thomas,
Get issues with moosh v. 1.27 with backup restore on Moodle 4.5.1 (PHP 8.2) in ANSI mode with Redis caching activated. (No issue when Redis unactivated). This seems to be linked with some change on the way Moodle 4.5 use Redis and manage Redis sessions. Seems a lot of site get same issues and some patch moosh install to workaround. Any idea if possible to solve or what to do to Patching ?
Here below mosh error log copy.
3:18:16 ✔ m4lts.didaxo.tvфdidaxo01.cblue.be /var/www/m4lts.didaxo.tv/www $ moosh course-backup 21 PHP Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active in /var/www/m4lts.didaxo.tv/www/lib/classes/session/redis.php on line 235
Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active in /var/www/m4lts.didaxo.tv/www/lib/classes/session/redis.php on line 235 Default exception handler: La configuration de redis session a échoué. Veuillez en informer l’administrateur du serveur. Debug: Error code: redissessionhandlerproblem
- line 237 of /lib/classes/session/redis.php: core\session\exception thrown
- line 750 of /lib/classes/session/redis.php: call to core\session\redis->init()
- line 688 of /lib/classes/session/redis.php: call to core\session\redis->init_redis_if_required()
- line 996 of /lib/classes/session/manager.php: call to core\session\redis->destroy()
- line 636 of /lib/classes/session/manager.php: call to core\session\manager::destroy()
- line 4078 of /lib/moodlelib.php: call to core\session\manager::login_user()
- line 313 of /opt/moosh/moosh.php: call to complete_user_login()
!!! La configuration de redis session a échoué. Veuillez en informer l’administrateur du serveur. !!!
Please try with no login option: moosh -l course-backup 21
Hi Tomas,
I found this previously and tried already But this causes unfortunately security issues
Here is log after this try ;
Log error category-export : $ moosh -l course-backup 2 Default exception handler: error/backup_check_user_not_exists Debug: Error code: backup_check_user_not_exists $a contents: 0
- line 81 of /backup/util/checks/backup_check.class.php: backup_controller_exception thrown
- line 183 of /backup/controller/backup_controller.class.php: call to backup_check::check_user()
- line 75 of /opt/moosh/Moosh/Command/Moodle39/Course/CourseBackup.php: call to backup_controller->__construct()
- line 376 of /opt/moosh/moosh.php: call to Moosh\Command\Moodle39\Course\CourseBackup->execute()
!!! error/backup_check_user_not_exists !!!
Any possible workaround ? Thanks for all your help.
Benoit Ter Burg DiDaXo & DiDaCast Sent using Mobile Mail
On 16 Mar 2025, at 15:01, Tomasz Muras @.***> wrote:
Please try with no login option: moosh -l course-backup 21
— Reply to this email directly, view it on GitHubhttps://github.com/tmuras/moosh/issues/514#issuecomment-2727456234, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANHQLWLUL4P2NGTUYNRGOFL2UV72HAVCNFSM6AAAAABYZKKOJWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMRXGQ2TMMRTGQ. You are receiving this because you authored the thread.Message ID: @.***>
[tmuras]tmuras left a comment (tmuras/moosh#514)https://github.com/tmuras/moosh/issues/514#issuecomment-2727456234
Please try with no login option: moosh -l course-backup 21
— Reply to this email directly, view it on GitHubhttps://github.com/tmuras/moosh/issues/514#issuecomment-2727456234, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANHQLWLUL4P2NGTUYNRGOFL2UV72HAVCNFSM6AAAAABYZKKOJWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMRXGQ2TMMRTGQ. You are receiving this because you authored the thread.Message ID: @.***>
So how about: moosh -o session_handler_class=unset course-backup 21
Thanks Tomasz
Seems OK No more error message and log says course has been back up successfully. Anyway there is no back up file in usual default destination dir.
Version used is an ANSI MOOSH version, installed onto my Debian VM allowing to act on any moodle URL on same VM. Maybe this way to do change destination or impeach file writing in default destination dir.
I will check with my hoster.
More to come Benoit
From: Tomasz Muras @.> Reply to: tmuras/moosh @.> Date: Sunday, 16 March 2025 at 15:51 To: tmuras/moosh @.> Cc: Benoit Ter Burg @.>, Author @.***> Subject: Re: [tmuras/moosh] Moosh redis issue (Issue #514)
So how about: moosh -o session_handler_class=unset course-backup 21
— Reply to this email directly, view it on GitHubhttps://github.com/tmuras/moosh/issues/514#issuecomment-2727482064, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANHQLWJC55LYE3SJ7AFVQKD2UWFWFAVCNFSM6AAAAABYZKKOJWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMRXGQ4DEMBWGQ. You are receiving this because you authored the thread.Message ID: @.***> [Image removed by sender. tmuras]tmuras left a comment (tmuras/moosh#514)https://github.com/tmuras/moosh/issues/514#issuecomment-2727482064
So how about: moosh -o session_handler_class=unset course-backup 21
— Reply to this email directly, view it on GitHubhttps://github.com/tmuras/moosh/issues/514#issuecomment-2727482064, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANHQLWJC55LYE3SJ7AFVQKD2UWFWFAVCNFSM6AAAAABYZKKOJWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMRXGQ4DEMBWGQ. You are receiving this because you authored the thread.Message ID: @.***>
Hi Tomasz,
Good News moosh -o session_handler_class=unset course-backup 21 Is working very well Last issue (back up file not available) was related to a small FTP configuration Issue from our side. Sorry.
So I confirm this workaround is working very well with Redis activated.
Thanks for your time and follow up !
Hi Thomas,
Semi-related, I just encountered this error once upgrading to Moodle 4.5. I am running Moosh 1.32. The error seems to appear when running:
moosh cache-clear
and
moosh plugin-install -dr {version} {pluginname}
After reading this thread, I was able to get the cache-clear command to work using:
moosh -l cache-clear
as you suggested.
Any thoughts on the plugin-install command? Also, I wonder if you could explain what the "no login" option does differently than the standard cache-clear. Is moosh actually operating as a local admin user or something?
Thanks so much!
We are encountering the same error with course-reset. I tried the session_handler_class option and now get this error:
Default exception handler: Exception - Call to undefined function Moosh\Command\Moodle39\C ourse\print_error() Debug: Error code: generalexceptionmessage
- line 30 of /usr/local/share/moosh/Moosh/Command/Moodle39/Course/CourseReset.php: Error t hrown
- line 376 of /usr/local/share/moosh/moosh.php: call to Moosh\Command\Moodle39\Course\Cour seReset->execute()
!!! Exception - Call to undefined function Moosh\Command\Moodle39\Course\print_error() !!! !! Error code: generalexceptionmessage !! !! Stack trace: * line 30 of /usr/local/share/moosh/Moosh/Command/Moodle39/Course/CourseRe set.php: Error thrown
- line 376 of /usr/local/share/moosh/moosh.php: call to Moosh\Command\Moodle39\Course\Cour seReset->execute() !!
We are on Moodle 4.5 with Moosh just updated to 1.31 today. Any other workarounds please?
Hi all,
after encountering the same issue, I made the following workaround:
- call moosh with the flag
-lto disable login as user (handles the Redis session handler problem)- or comment out the login code in
moosh.php
- or comment out the login code in
- in the Moosh/Command/Moodle39/Course/CourseRestore.php file, hardcode the global
$USERto the admin user:$USER = get_admin();
The course could then be restored.
EDIT:
The -o session_handler_class=unset method did not work in my case, because it used mysql for session storage and tried to insert a duplicate (empty) key for the session.
We are encountering the same error with course-reset. I tried the session_handler_class option and now get this error:
Default exception handler: Exception - Call to undefined function Moosh\Command\Moodle39\C ourse\print_error() Debug: Error code: generalexceptionmessage
* line 30 of /usr/local/share/moosh/Moosh/Command/Moodle39/Course/CourseReset.php: Error t hrown * line 376 of /usr/local/share/moosh/moosh.php: call to Moosh\Command\Moodle39\Course\Cour seReset->execute()!!! Exception - Call to undefined function Moosh\Command\Moodle39\Course\print_error() !!! !! Error code: generalexceptionmessage !! !! Stack trace: * line 30 of /usr/local/share/moosh/Moosh/Command/Moodle39/Course/CourseRe set.php: Error thrown
* line 376 of /usr/local/share/moosh/moosh.php: call to Moosh\Command\Moodle39\Course\Cour seReset->execute() !!We are on Moodle 4.5 with Moosh just updated to 1.31 today. Any other workarounds please?
This doesn't seem like the same error; though the function is defined in lib/setuplib.php in moodle and probably only needs a backslash before the call to make sure it is called from the root namespace. As in:
// before:
print_error("invalidcourseid");
// after:
\print_error("invalidcourseid");
Also - It seems like you passed an invalid course ID to Moosh in the command.
@1katoda Thank you, silly mistakes on my part, but these fixes worked! Thank you very much!
@haietza No problem, I'm glad it worked.
@1katoda Thank you for all the additional suggestions. We are encountering the same trouble with course-move. Unfortunately, this command doesn't seem to have a $USER var that is referenced, just the global $DB var.
Running moosh -l course-move results:
Default exception handler: You don't have permission to view this list of courses. Debug:
Error code: cannotviewcategory
* line 281 of /course/classes/category.php: core\exception\moodle_exception thrown
* line 37 of /usr/share/moosh/Moosh/Command/Moodle39/Course/CourseMove.php: call to core_course_category::get()
* line 377 of /usr/bin/moosh: call to Moosh\Command\Moodle39\Course\CourseMove->execute()
!!! You don't have permission to view this list of courses. !!!
This makes sense, since we aren't logged in.
Running moosh --user=ADMIN course-move results in the original error:
PHP Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active in /opt/moodletest2/web/lib/classes/session/redis.php on line 243
Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active in /opt/moodletest2/web/lib/classes/session/redis.php on line 243
Default exception handler: Setting up of redis session failed. Please notify the server administrator. Debug:
Error code: redissessionhandlerproblem
* line 245 of /lib/classes/session/redis.php: core\session\exception thrown
* line 759 of /lib/classes/session/redis.php: call to core\session\redis->init()
* line 697 of /lib/classes/session/redis.php: call to core\session\redis->init_redis_if_required()
* line 996 of /lib/classes/session/manager.php: call to core\session\redis->destroy()
* line 636 of /lib/classes/session/manager.php: call to core\session\manager::destroy()
* line 4089 of /lib/moodlelib.php: call to core\session\manager::login_user()
* line 314 of /usr/bin/moosh: call to complete_user_login()
!!! Setting up of redis session failed. Please notify the server administrator. !!!
Running moosh -o session_handler_class=unset course-move results:
After evaluating config.php, $CFG is not set
I'm open to any other suggestions or ideas. We are also running Moodle 4.5 and Moosh 1.35.
@rrsiegfried I'm just writing this off the top of my head, but I guess a possible workaround would be the same as my original comment (running moosh with "no-login"/-l), you would just have to define the global $USERvariable as admin in the execute function of the CourseMove command, since it is not already defined. Like
...
public function execute()
{
global $USER, $DB;
$USER = get_admin();
$courseIdsArg = $this->arguments[0];
$categoryId = $this->arguments[1];
...
The following functions should then use admin as the logged in user, as it is in a global variable.
@1katoda @tmuras
Thank you for the additional insight!
My colleague and I did a lot of testing this afternoon and moosh appears to be incompatible with the Redis session handler. We tried the moosh -o session_handler_class=unset and this appeared to unset the Moodle $CFG object.
We added an environment variable to our Moosh wrapper script that bypasses setting the Redis session handling settings in Moodle's config.php. We felt this was a cleaner alternative to hard coding the $USER object into all the individual Moosh commands that we use - or might use.
Our Moosh wrapper script:
#!/bin/bash
export MOOSH=1
/usr/bin/moosh -p <path_to_moodle> -n $@
Moodle config.php:
if (!getenv('MOOSH')) {
$CFG->session_handler_class = '\core\session\redis';
$CFG->session_redis_host = <redis_host>;
$CFG->session_redis_prefix = <redis_prefix>;
$CFG->session_redis_acquire_lock_timeout = 120;
}
This causes Moodle to fall back to the default file session handler only for Moosh.
I hope this is helpful to others!
@rrsiegfried Thank you, I think your solution is better, as it only requires a config change.
Thank you for the additional insight!
My colleague and I did a lot of testing this afternoon and moosh appears to be incompatible with the Redis session handler. We tried the
moosh -o session_handler_class=unsetand this appeared to unset the Moodle $CFG object.We added an environment variable to our Moosh wrapper script that bypasses setting the Redis session handling settings in Moodle's
config.php. We felt this was a cleaner alternative to hard coding the$USERobject into all the individual Moosh commands that we use - or might use.Our Moosh wrapper script:
#!/bin/bash export MOOSH=1 /usr/bin/moosh -p <path_to_moodle> -n $@Moodle config.php:
if (!getenv('MOOSH')) { $CFG->session_handler_class = '\core\session\redis'; $CFG->session_redis_host = <redis_host>; $CFG->session_redis_prefix = <redis_prefix>; $CFG->session_redis_acquire_lock_timeout = 120; }This causes Moodle to fall back to the default file session handler only for Moosh.
I hope this is helpful to others!
If you aren't using a wrapper script, adding this to the top of the moosh executable does the same job.
putenv('MOOSH=1');