Memory limit when editing user

Memory limit when editing user

by Emma Richardson -
Number of replies: 40
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Any time that I try to edit a user, it loads a blank page.  With full debugging on, I get the following error:

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /var/www/moodle/lib/dml/mysqli_native_moodle_database.php on line 1369

The line referenced in the error is related to adjusting the case to lower case.

Weirdly enough the change to the user profile seems to take.  I raised my php memory limit and mysqli buffer but no change...

I went against my own advice (!) and upgraded to latest moodle version (4.5.2).  I rebooted my db server and raised the ram amount on the server.  I cleared caches just because..

Trying to think to what has changed on the site - the main thing I can think of related to user is a new custom profile field.  I went in and limited that to a 10 character entry limit but that didn't affect anything.  It is just a basic text entry.

I am on php 8.1 and mariadb.

Oh, I can edit my own profile without error.  It is just when editing other users' profile.

(Maybe I should have posted in the General forum but I figured I needed help from this group of people!)

Average of ratings: -
In reply to Emma Richardson

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

I deleted the custom profile field and the issue did not get resolved so apparently it is not related.

Does anyone know what limit that is hitting?

In reply to Emma Richardson

Re: Memory limit when editing user

by Ken Task -
Picture of Particularly helpful Moodlers

From CLI, what does: ps aux |grep php look like on the web server?

'SoS', Ken

In reply to Ken Task

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Just getting back to this -
www-data 716952 0.0 0.0 2480 508 ? Ss 06:01 0:00 /bin/sh -c /usr/bin/php /var/www/moodle/admin/cli/cron.php >/dev/null
www-data 716953 51.2 10.4 940948 846140 ? R 06:01 0:18 php /var/www/moodle/admin/cli/cron.php (Feb 21, 06:01:05 Scheduled task: core\task\session_cleanup_task)
root 717080 0.0 0.0 6264 704 pts/0 S+ 06:01 0:00 grep php
In reply to Emma Richardson

Re: Memory limit when editing user

by Ken Task -
Picture of Particularly helpful Moodlers

>/dev/null Ugh!  Sending cron job results off into la-la-land means you can't actually watch the cron job and it's task!

How bout:

touch /var/log/moodecron.log

chown www-data:www-data /var/log/moodecron.log

Then change the redirect of the cron job to

>/var/log/moodlecron.log

One can then watch that log in realtime ...

tail -f /var/log/moodlecron.log

Or, better yet, install multitail and then you an watch web server access and error logs as well as the cron jobs/task in realtime.

Question: if you run cron.php from command line, does it get to adhoc task and then loops back through adhoc task for a 180 times more?  If so, I have observed this behavoir ... about 1/3 of the 180 times more memory usage increased from 30M to 35M+ ... at 2/3 memory usage increases some more to about 40M and when it finally finishes, memory usage is about 50-60+ M (never the same).

Through all of those, there are still 0 adhoc task!

'SoS', Ken

In reply to Ken Task

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Why are we looking at cron - I actually disabled cron to see if that would change anything and it did not..
In reply to Emma Richardson

Re: Memory limit when editing user

by Ken Task -
Picture of Particularly helpful Moodlers

What would it hurt to run the cron.php script from command line to see if your server is exhibiting the same behavior as I have seen with every version 4.4.x and 4.5.x? smile

The error you are seeing is a memory limit ... as described, the cron job/a task within the cron job/the behavior of what I have observed could be pushing memory to the limit and beyond - thus error.

Does disabling cron clear the adhoc_task jobs in it's que?

It's just a quick check of a rabbit hole! smile

'SoS',Ken

In reply to Ken Task

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ok, that makes sense...ran cron from php - it is using 52MB memory - not excessive at all. Disabled, cleared adhoc tasks and retried the user save and same behavior.
In reply to Emma Richardson

Re: Memory limit when editing user

by Ken Task -
Picture of Particularly helpful Moodlers

Disabled what?   Don't disable as that is not the environment when the error occurs.   Am not saying I know for sure that is the issue,  I have a 4.1 production and a 4.5 sandbox on same server that uses same LDAP and a customized field in profile.   No issues.

The other area ... LDAP mappings and setting for checking when user logs in and or edits profile.

'SoS', Ken

In reply to Ken Task

Re: Memory limit when editing user

by Ken Task -
Picture of Particularly helpful Moodlers

And a follow up ... am not running anything that would require adhoc task catching up (I don't think).   To stop the un-desired looping, added the two following lines to config.php

$CFG->cron_keepalive='0';
$CFG->task_adhoc_concurrency_limit='1';

No more looping after and no more additional memory usage.

This sure is a strange and evidently specific to a few instances! sad

'SoS', Ken

Average of ratings:Useful (1)
In reply to Ken Task

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
I have been looking at those with a fine tooth comb...the profiling I ran (see above) appears to be referencing the help buttons on the edit profile field as the major source of memory usage. I am going to see if I can update a user via moosh or run update profile via cli maybe for more messages? This is baffling and very annoying ..I have about 8 custom profile fields - going to see if maybe I can cut one or more out - trying to figure out which ones are vital..but not convinced that is the source of the issue anyway...and so weird that it does not happen on your own profile, just when editing someone else's...
In reply to Emma Richardson

Re: Memory limit when editing user

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

What you could do is enable the general log in MySQL/MariaDB which will hopefully help identify the query during the error. Enable this with SET GLOBAL general_log = 'ON';. Do this immediately before the steps causing the error when no one else is using the site and with the cron disabled because it will log every SQL statement (i.e. lots of lines and a large log file). Then after the error turn logging off with SET GLOBAL general_log = 'OFF';. Then check the log file which on my system is in /var/lib/mysql/<hostname>.log. You should see a line where you turned off logging:

250218 14:11:03    252 Query    SET GLOBAL general_log = 'OFF'

If no one else was using the site when the error occurred then hopefully the SQL statement before the above line is the one being run when the fatal error occurs. That may give us a clue as to the cause, but it may just be an innocent query being run after something else has eaten all the memory.

My first thought is some plugin that hooks into the user profile page could be misbehaving.

Average of ratings:Useful (1)
In reply to Leon Stringer

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ok, I tried this. Wow, does a lot happen on user update! Strangely enough this is not a single reference to line 1369 which I thought there would be. I see references to boost union (tried to edit under boost and same behavior) and autogroup but no obvious errors noted - no fatal error in the log. Mosaab - do you have autogroup plugin installed? Just wondering if it is actually my plugin causing this!!
 
In reply to Leon Stringer

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
So I enabled profiling for that page and it appears to be some sort of loop going on through the help buttons...Screenshot of profiling page
In reply to Emma Richardson

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
And then, if I keep drilling down the rabbit hole, I end up here with the array_change_key_case which also concurs with the line of the referenced file which talks about case...my question is how do I find out what is actually causing this?
Screenshot of profiling
In reply to Emma Richardson

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
I did a comparison profiling runs on when you update your own profile and I am now thinking it is the searching the sessions by userid - I just realized that I have over a million rows in the sessions table - I am not using DB for sessions but it is still logging them apparently - can I just truncate that table - shouldn't Moodle be clearing that out?
In reply to Emma Richardson

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ok, found a tracker item that 4.5 is not clearing out the sessions table - that is the problem - as soon as I cleared the table, everything started working correctly again!
https://tracker.moodle.org/browse/MDL-84051
Average of ratings:Useful (3)
In reply to Emma Richardson

Re: Memory limit when editing user

by Ken Task -
Picture of Particularly helpful Moodlers

Congrats!   Strange!!!!

... so site settings had DB sessions=0 and yet the table had entries!!!

Well, one good thing since you are now using git, if your code didn't have the fix but fix was available, easy to get the fix via git.

I have in admin/cli/ of sites a 'cleanup' bash shell script
Contains all the clean up task:

php scheduled_task.php --execute="\logstore_standard\task\cleanup_task";
php scheduled_task.php --execute="\core\task\backup_cleanup_task";
php scheduled_task.php --execute="\core\task\cache_cleanup_task";
php scheduled_task.php --execute="\core\task\file_temp_cleanup_task";
php scheduled_task.php --execute="\core\task\file_trash_cleanup_task";
php scheduled_task.php --execute="\core\task\session_cleanup_task";
php scheduled_task.php --execute="\core_files\task\conversion_cleanup_task";
php scheduled_task.php --execute="\tool_recyclebin\task\cleanup_category_bin";
php scheduled_task.php --execute="\tool_recyclebin\task\cleanup_course_bin";
php scheduled_task.php --execute="\core\task\messaging_cleanup_task";
php scheduled_task.php --execute="\core\task\delete_incomplete_users_task";
php scheduled_task.php --execute="\core\task\delete_unconfirmed_users_task";

i can cat the script and pick/choose the one I want to run via copy/paste to CLI or just run the script.

Mine too was set not to use DB for sessions, but had entries in that table but didn't have the issue you did. ????  

Just ran the script to clean it up.

'SoS', Ken

In reply to Ken Task

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Interestingly enough, it creates an entry for every visit - we had been having some bot hits from russia and france a while back - we got most of those shut down but guessing that is why that table was so huge. Today it is back up to about 3000 entries so it grows pretty fast and I am now thinking I need to look at logs and see if we are getting hit again. Chatgpt is also hitting the site a lot which is interesting.
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
Did this solve the problem?
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Mosaab Alsiddig -
The solution is only one line !!
from database run : delete from mdl_sessions;

Thats all
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Or TRUNCATE TABLE mdl_sessions - they have also just released the fix to github
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
I’m experiencing the same issue. Strangely, the profile updates successfully, but the problem occurs only with the redirect URL.
I’ll keep following this post and hope someone finds a solution.
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Do you use ldap authentication by chance? Please let me know if you figure this out...
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
No I dont
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
How about reengagement plugin - do you have that installed? That might be the last thing happening before the error though cron started running too so it is hard to tell for sure.  If not, can you share a list of your installed plugins - maybe we can narrow it down - I have so many that it might be quicker to look at yours first!
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
I have the issue in two sites, below is the list of additional plugins, but I think it is a cache issue, the problem only in redirect URL


Site 1
======
mod_datalynx
mod_game
mod_hvp
mod_quizgame
booktool_wordimport
block_completion_progress
block_glossary_export_to_quiz
block_mb2azcourses
block_mb2content
qformat_printout
qformat_wordtable
filter_filtercodes
filter_mb2shortcodes
atto_mb2shortcodes
tiny_fontcolor
enrol_attributes
auth_emailadmin
report_benchmark
theme_boost_union
theme_learnr
theme_mb2cg
local_contact
local_glossary_wordimport
local_mb2builder
local_mb2notices
local_mb2slides
local_profilecohort
local_questionbulkupdate
local_sandbox


Site 2
======
mod_attendance
mod_customcert
mod_subcourse
mod_supervideo
mod_zoom
block_grade_me
block_mb2azcourses
filter_mb2shortcodes
filter_multilang2
atto_mb2shortcodes
tiny_fontcolor
enrol_coursecompleted
enrol_groupsync
enrol_myfatoorah
report_benchmark
theme_mb2nl
local_mb2builder
local_mb2coursenotes
local_mb2megamenu
local_mb2reviews
local_mb2slides
local_profilecohort
local_staticpage
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
Try manually adding a new user while selecting the "Generate password and notify user" option.
I encountered the same issue again.
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Yes, that happened on my site too and I have yet to receive the email - did you get the email? You hardly have any plugin overlaps - the only match I see between your two sites and my one is benchmark..think that tells us it is not plugin related. So maybe user profile related - I know that I cleared caches on my site but I have not cleared cloudflare cache - will try that and see...
In reply to Emma Richardson

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Ok, that didn't help.  Something must be causing the page redirect to not work.  Oh, and I did get the email for the new account - ended up in my spam.

In reply to Emma Richardson

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
I am also noticing that if I refresh the page, it redirects to my profile page (which also does NOT error when updating).
In reply to Emma Richardson

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
What text editor are you using? I am using Atto...I am on Moodle 4.5.2+ (this started in 4.5.1 though). I have Google analytics and UserWay accessibility scripts running as well as a help icon from Freshdesk...
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
I am using both Atto and TinyMCE editors, along with Moodle 4.5.1. As far as I remember, I didn’t notice this issue in version 4.4.

When attempting to edit a user’s profile (e.g., user ID 3), Moodle correctly navigates to:
👉 /user/editadvanced.php?id=3&course=1&returnto=profile
After saving the changes, Moodle should redirect back to the user's profile:
👉 /user/profile.php?id=3
However, in our case, the redirection fails, and instead, it navigates to /user/editadvanced.php without parameters, which results in loading the logged-in user's profile edit page instead.
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
That is exactly what is happening with my site. The memory error makes me think that it is not loading correctly because it is timing out due to something going on on the page - my profiling is pointing to the construction of the help buttons on the page maybe...but the fact that this is not happening to everyone else of 4.5.1 or 4.5.2 is very curious so there has to be something unique to our setups..
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
Strangely, I have two Moodle instances running on the same server, utilizing the same MariaDB, PHP, and Nginx resources. However, one of them is experiencing the issue, while the other functions as expected.
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ok, so then it is application based and your two instances have to have the clue somewhere...do you have custom profile fields?  What is different between the two sites?  
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
Yes I have custom profile field
No difference , I did the exact installation process for both of them
In reply to Mosaab Alsiddig

Re: Memory limit when editing user

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
The two moodle instances are identical? Same plugins/same exact setup? Are you getting the exact same error with debugging on as I am? Referencing line 1369?
In reply to Emma Richardson

Re: Memory limit when editing user

by Mosaab Alsiddig -
They are different sites with different content and plugin
I had the memory limit error in a test site, but for these two sites there no error in nginx or database log, and also no debugging error in Moodle