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

Inconsistent data treatment in file cache results in exception 'Failed to unserialise data from file. Either failed to read, or failed to write.'

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • None
    • 3.1.5, 3.2, 3.2.1, 3.2.2
    • Caching
    • None

      In file cache/stores/file/lib.php the method prep_data_after_read throws an exception if the unserialized data is identically equal to false. The set method (in the same file) that writes data to the cache has no corresponding check to prevent the writing of false to the cache.

      We noticed this problem when adding a grade item that included weighting to gradebook setup. The method fetch_all in lib/grade/grade_category.php was getting a 'false' record from the database (this is a different issue and could even be corrupt data on our part - but that is irrelevant). This false value was then cached. Subsequent cache reads produce the exception.

      The solution is to either allow boolean false to be read from the cache (remove the check and exception) or not allow false to be written.

      Option 1:

      protected function prep_data_after_read($data) {
              $result = @unserialize($data);
              return $result;
          }
      

      Option 2:

      public function set($key, $data) {
              $this->ensure_path_exists();
              $filename = $key.'.cache';
              $file = $this->file_path_for_key($key, true);
              // prevent writing boolean false to cache since it generates an error on retrieval
              // see prep_data_after_read
              $result = false;
              if (false !== $data) {
                  $result = $this->write_file($file, $this->prep_data_before_save($data));
              }
              if (!$result) {
                  // Couldn't write the file.
                  return false;
              }
              // Record the key if required.
              if ($this->prescan) {
                  $this->keys[$filename] = cache::now() + 1;
              }
              // Return true.. it all worked **miracles**.
              return true;
          }
      

            Unassigned Unassigned
            rbon Ray Bon
            Votes:
            4 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:

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