From 1d293ca4095b68b9d57db7af41c3a595fb63b120 Mon Sep 17 00:00:00 2001 From: Russell Smith Date: Thu, 12 Dec 2013 03:33:20 +1100 Subject: [PATCH] MDL-43421 logging: Ignore race condition in access update. --- lib/datalib.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/datalib.php b/lib/datalib.php index 91d54ce291b..1775ef6e224 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1700,7 +1700,19 @@ function user_accesstime_log($courseid=0) { $last->userid = $USER->id; $last->courseid = $courseid; $last->timeaccess = $timenow; - $DB->insert_record_raw('user_lastaccess', $last, false); + try { + $DB->insert_record_raw('user_lastaccess', $last, false); + } catch (dml_write_exception $e) { + // During a race condition we can fail to find the data, then it appears. + // If we still can't find it, rethrow the exception. + $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid' => $USER->id, + 'courseid' => $courseid)); + if ($lastaccess === false) { + throw $e; + } + // If we did find it, the race condition was true and another thread has inserted the time for us. + // We can just continue without having to do anything. + } } else if ($timenow - $lastaccess < LASTACCESS_UPDATE_SECS) { // no need to update now, it was updated recently in concurrent login ;-)