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

MDL-44065 Course: _add_instance exception leads to inconsistent db

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • 2.5.5, 2.6.2
    • 2.6
    • Course
    • MOODLE_26_STABLE
    • MOODLE_25_STABLE, MOODLE_26_STABLE
    • MDL-44065-master
    • Hide

      1. Run this query in your database (replace prefix_ with your table prefix):

      select count(1) from prefix_course_modules where instance=0
      

      Note the number. (It should be 0 if this problem has never occurred in the database.)

      2. To simulate a complex third-party module which may throw an exception during creation, edit mod/label/lib.php. Find the label_add_instance function. As the first line of this function, add the following:

      throw new coding_exception('testing');
      

      3. On any course website, add a label. Observe that the exception occurs.

      4. Repeat the database query from step 1.

      EXPECTED: There should be the same number of entries matching the query (0 if you are lucky).

      BEFORE FIX: Doing this sequence used to result in an additional, bogus entry in the course_modules table.

      5. Remove the exception code from mod/label/lib.php (replace with standard code).

      6. Create another label.

      EXPECTED: The label should now be created successfully, indicating that the general create process still works.

      Show
      1. Run this query in your database (replace prefix_ with your table prefix): select count(1) from prefix_course_modules where instance=0 Note the number. (It should be 0 if this problem has never occurred in the database.) 2. To simulate a complex third-party module which may throw an exception during creation, edit mod/label/lib.php. Find the label_add_instance function. As the first line of this function, add the following: throw new coding_exception('testing'); 3. On any course website, add a label. Observe that the exception occurs. 4. Repeat the database query from step 1. EXPECTED: There should be the same number of entries matching the query (0 if you are lucky). BEFORE FIX: Doing this sequence used to result in an additional, bogus entry in the course_modules table. 5. Remove the exception code from mod/label/lib.php (replace with standard code). 6. Create another label. EXPECTED: The label should now be created successfully, indicating that the general create process still works.

      When creating a module, the following sequence of events happens in the database:

      (...)
      1. mdl_course_modules row created with instance = 0
      2. (module table row created via module_add_instance)
      3. mdl_course_modules instance value set
      (...)

      If an exception is thrown during the module_add_instance function, this can result in the situation where a mdl_course_modules row with instance = 0 exists. Generally this row is ignored but it can cause problems with custom modules - for example, the module_get_coursemodule_info function will be called when rebuilding course cache, and if that function fails to handle the case where instance is 0, then the course page cannot be viewed.

      While this inconsistent data does not cause obvious problems for core Moodle (as noted, we saw this issue with a custom module that was not tolerant of the situation - probably core modules are designed to allow it), I suggest that to support those Moodle users who care about database consistency and reduce the likelihood of this problem occurring for custom plugin developers, we should fix this problem.

      Note: In OU installation of Moodle, we have evidence for this problem occurring in 9 modules of which 2 are core (resource, url) and one is non-OU third party; the others are all our modules. However I suspect the core ones may have been introduced by events in our custom code, or by custom repository code, etc. and are not actually possible in core Moodle.

            quen Sam Marshall
            quen Sam Marshall
            Frédéric Massart Frédéric Massart
            Dan Poltawski Dan Poltawski
            Dan Poltawski Dan Poltawski
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.