Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-77619

Duplicate course grade items and grade categories possible by race condition.





      We've noticed on a few sites occasionally the gradebook page will display an error "Found more than one record in fetch() !"

      This error comes from there being multiple grade_items of type "course" in the grade_items table.

      This is likely happening because of a race condition and has become more likely because of MDL-19711 .

      The grade category has a function that checks if any grade items exist, and if none exist it creates one. This means its possible for two scripts to race and the check for existing grade items doesn't work and two grade items are created, leading to errors elsewhere.

      It is quite rare, but does still happen. Which makes me think it's usually when backing up / restoring courses.

      How to reproduce

      • Setup a Moodle site
      • Create a course
      • In this file, https://github.com/moodle/moodle/blob/master/lib/grade/grade_category.php#L2275 under this line add the line: sleep(5)
      • Open two tabs / sessions and login to the site and go to the course.
      • In the first tab, open the gradebook and quickly switch to the second tab and open the gradebook as well.
      • Wait a couple of seconds for the sleep to pass
      • The second tab will give an exception "Found more than one record in fetch()"
      • If you check the grade_items table, you can see two grade_items created for the course


      • I only tested course grade items (categories). But it seems this is able to affect anything that uses this function. So its likely it also affects activity grade items as well


        Issue Links



              Unassigned Unassigned
              matthewhilton Matthew Hilton
              2 Vote for this issue
              5 Start watching this issue