Duplicate Session error in error log

Duplicate Session error in error log

by Scott Karren -
Number of replies: 5
Everyone,

I am using Moodle 2.8.5+ on a Windows 2012 server with IIS, PHP 5.5.11, and MySQL 5.6.17.  I have started seeing the below error in my error logs and I am confused because this tells me that sessions are being stored in the database, but I do not have use db sessions selected in Admin>Session Handling.  Any ideas on what would be causing these errors?

[14-May-2015 10:00:40 America/Denver] Default exception handler: Error writing to database Debug: Duplicate entry 'osbn4d1bqttcmvk01tg5qa4lh3' for key 'mdl_sess_sid_uix'
INSERT INTO mdl_sessions (state,sid,sessdata,userid,timemodified,timecreated,lastip,firstip) VALUES(?,?,?,?,?,?,?,?)
[array (
  0 => 0,
  1 => 'osbn4d1bqttcmvk01tg5qa4lh3',
  2 => NULL,
  3 => 0,
  4 => 1431619240,
  5 => 1431619240,
  6 => '10.65.0.84',
  7 => '10.65.0.84',
)]
Error code: dmlwriteexception
* line 446 of \lib\dml\moodle_database.php: dml_write_exception thrown
* line 1164 of \lib\dml\mysqli_native_moodle_database.php: call to moodle_database->query_end()
* line 1210 of \lib\dml\mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->insert_record_raw()
* line 422 of \lib\classes\session\manager.php: call to mysqli_native_moodle_database->insert_record()
* line 399 of \lib\classes\session\manager.php: call to core\session\manager::add_session_record()
* line 79 of \lib\classes\session\manager.php: call to core\session\manager::initialise_user_session()
* line 773 of \lib\setup.php: call to core\session\manager::start()
* line 550 of \config.php: call to require_once()
* line 30 of \draftfile.php: call to require_once()

Scott


Average of ratings: -
In reply to Scott Karren

Session caching in sessions dir, database, or both???

by Scott Karren -

After some digging I happened on this paragraph in the Moodle caching documentation.

"The second cache type is the session cache. This is just like the PHP session that you will already be familiar with, in fact it uses the PHP session by default. You may be wondering why we have this cache type at all, but the answer is simple. MUC provides a managed means of storing, and removing information that is required between requests. It offers developers a framework to use rather than having to re-invent the wheel and ensures that we have access to a controlled means of managing the cache as required.
Its important to note that this isn't a frequently used cache type as by default session cache data is stored in the PHP session and the PHP session is stored in the database. Uses of the session cache type are limited to small datasets as we don't want to bloat sessions and thus put strain on the database."

Does this mean that regardless of the setting in Admin>Session Handling that session data is stored in the database?

Anyone have any additional insights into this?

In reply to Scott Karren

Re: Session caching in sessions dir, database, or both???

by Ken Task -
Picture of Particularly helpful Moodlers

Run Linux so can't speak to any other platform quirks, but ... the sessions data related to user logging into Moodle.   Can re-call default for earlier versions of 2 was DB.  After folks had been running for a while, errors/issues started to appear.  Especially problematic for Admin level users as their session information would have more data.   Large session table in a short time.   Many moodle tables maintain indexes.   So folks started recommending to use file based sessions.   It's been my experience that when the Moodle Admin switched to file, no more data was being written to the tables.  One could see, a sessions folder created in moodledata and session files would appear as users logged on.   The newer versions of Moodle now have DB off and file based sessions by default.

So, you could remove all rows in the table for moodle sessions safely and that should also clean up the index file ... the mdl_sess_sid_uix error you saw.

To double check to make sure Moodle is using file based sessions, query the mdl_config table for all values.  dbsessions should be = 0 (ie, off).

Would also recommend (for admins) to clear their browser cookies as that's the first item in any cookie sent by server ... sessions - even to guest/visitors.

'spirit of sharing', Ken

In reply to Ken Task

Re: Session caching in sessions dir, database, or both???

by Scott Karren -

Ken,

Thanks for your suggestions.  I have double checked the db and dbsessions is indeed off.  Out of paranoia I also added a line to my config.php to disable dbsessions.

Now the odd part, when I login as admin or a test user or have another person login I see a session file created in the sessions dir .  I also see a record in mdl_sessions created for that user. This is what prompted my second post asking about caching.  I have caching configured and it appears that something in there is using db sessions.

Not exactly where to take it from here as the error comes up intermittently.

Scott

In reply to Scott Karren

Re: Session caching in sessions dir, database, or both???

by Ken Task -
Picture of Particularly helpful Moodlers

Well, that's 'special'! :\  What version of Moodle?

How about purging the cache?

You've also got opache running, correct?   Clear that as well.

In a version 2.9,  In moodledata//cache/cachestore_file/default_application/ there is a core_cache directory containing .cache files and what appears to be like info.

Manually remove any files you find in

moodledata/cache/cachestore_file/default_application/core_cache/core_config/

In a 2.9 I witnessed the same ... but only one record created!!!!   Strange!

Comment on page setting sessions to DB which is off by default says:

Other session drivers can be configured directly in config.php, see config-dist.php for more information. This option disappears if you specify session driver in config.php file.

Got anything else in your config.php file from config-dist.php?  Mine is generic ... nothing added to it.

'spirit of sharing', Ken


In reply to Ken Task

Re: Session caching in sessions dir, database, or both???

by Scott Karren -

Ken,

Using 2.8.5+.  I have purged the cache, cleared opcache, didn't have a core_cache directory but purging the cache removed everything in cachestore_file/default_application.  As far as the config.php, other than the added dbsessions=0 everything else is generic.

Stranger and stranger. I think I may have it narrowed down to wincache and opcache.  I currently use both with wincache being the default store in MUC for sessions, problem is wincache doesn't support sessions very well.  Need to do some more testing but my thought at this point is that it may be defaulting to dbsessions because wincache doesn't know how to handle the session correctly.

Scott