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

Move the navcache out of $SESSION and into MUC session cache

XMLWordPrintable

    • MOODLE_403_STABLE
    • MOODLE_403_STABLE, MOODLE_404_STABLE
    • MDL-79628-move-navcache-to-muc-feature-MOODLE_403_STABLE
    • MDL-79628-move-navcache-to-muc-feature-MOODLE_404_STABLE
    • MDL-79628-move-navcache-to-muc-feature
    • Hide

      Down MDL-79628-navcache.php into the root of the Moodle installation

      Confirm the new nav cache is working correctly:

      1. Open the MDL-79628-navcache.php file in your browser
      2. Login to Moodle as a registered user.
      3. Open the Moodle home page to ensure the navigation cache is generated.
      4. Navigate back to the MDL-79628-navcache.php
      5. Confirm that the navigation cache is displayed on the page.
      6. Open the MDL-79628-navcache.php file in another browser or incognito window.
      7. Login as another user In a new browser or incognito window, login as a different user.
      8. Refresh  the Moodle home page in the new browser or incognito window.
      9. Navigate back to the MDL-79628-navcache.php tab in the new browser and confirm that the navigation cache is displayed for the second user.
      10.  Uncomment the `$navcache->clear();` line in the MDL-79628-navcache.php file.
      11.  Refresh the `MDL-79628-navcache.php` page in your browser.
      12. Confirm that the navigation cache is cleared on the `MDL-79628-navcache.php` page.
      13.  Comment the `$navcache->clear();` line by adding `//` at the beginning
      14. Refresh the page MDL-79628-navcache.php page in another browser or an incognito window.
      15. Confirm that the navigation cache is not cleared for the another user.
      16. Uncomment the line `// $navcache->volatile();` by removing the `//` characters.
      17. Repeat steps 12 to 13 for the `$navcache->volatile();` line and confirm that the navigation cache is cleared when the line is uncommented.
      18. Comment the `$navcache->volatile();` line by adding `//` at the beginning.
      19. Repeat steps 15 to 16 for the commented state of the `$navcache->volatile();` line and confirm that the navigation cache is not cleared for the another user.

      Confirm the original read only session bug no longer appears:

      1. Go to https://example.com/cache/admin.php
      2. Add an instance of File cache under {}Installed cache stores{} with any name you want.
      3. Scroll to the bottom of the page to the {}*Stores used when no mapping is present* section and click on {}Edit mappings{}.
      4. Update Cache administration of Session to the new file cache that you have created in step #2 and Save Changes.
      5. Purge Session Cache Manually: Navigate to the moodledata directory, locate the sessions directory, and delete the contents to clear the session.
      rm -rf /path/to/moodledata/sessions/*
       
      6. Enable readonly sessions and sessions debug in config.php and visit the site.

      $CFG->enable_read_only_sessions = true;
      $CFG->enable_read_only_sessions_debug = true;

       7. Create a `step-one.php` file in the root of your Moodle directory with the following content which simulates the default state the session is in before the navigation state is cached:

      <?php
      require('config.php');
      unset($SESSION->navcache);
      echo "Step one"; 
      

      8. Create a `step-two.php` file in the root of your Moodle directory with the following content which simulates the cache being initialized, which counts as a write to the session manager:

      <?php
      define('READ_ONLY_SESSION', true);
      require('config.php');
      navigation_cache::destroy_volatile_caches();
      echo "Step two";

       9. Open a terminal and tail the error logs
      10. Visit `step-one.php` and `step-two.php` in the browser in order with the following URLs:

      • https://<wwwroot_url>/step-one.php
      • https://<wwwroot_url>/step-two.php

      Confirm you do not see any error message in the error logs

      Show
      Down MDL-79628 -navcache.php into the root of the Moodle installation Confirm the new nav cache is working correctly: Open the MDL-79628 -navcache.php file in your browser Login to Moodle as a registered user. Open the Moodle home page to ensure the navigation cache is generated. Navigate back to the MDL-79628 -navcache.php Confirm  that the navigation cache is displayed on the page. Open the MDL-79628 -navcache.php file in another browser or incognito window. Login as another user In a new browser or incognito window, login as a different user. Refresh  the Moodle home page in the new browser or incognito window. Navigate back to the MDL-79628 -navcache.php tab in the new browser and c onfirm  that the navigation cache is displayed for the second user.  Uncomment the `$navcache->clear();` line in the MDL-79628 -navcache.php file.  Refresh the ` MDL-79628 -navcache.php` page in your browser. Confirm that the navigation cache is cleared on the ` MDL-79628 -navcache.php` page.  Comment the `$navcache->clear();` line by adding `//` at the beginning Refresh the page MDL-79628 -navcache.php page in another browser or an incognito window. Confirm that the navigation cache is not cleared for the another user. Uncomment the line `// $navcache->volatile();` by removing the `//` characters. Repeat steps 12 to 13 for the `$navcache->volatile();` line and confirm  that the navigation cache is cleared when the line is uncommented. Comment the `$navcache->volatile();` line by adding `//` at the beginning. Repeat steps 15 to 16 for the commented state of the `$navcache->volatile();` line and confirm  that the navigation cache is not cleared for the another user. Confirm the original read only session bug no longer appears: 1. Go to  https://example.com/cache/admin.php 2. Add an instance of  File cache  under  { } Installed cache stores { }  with any name you want. 3. Scroll to the bottom of the page to the { } *Stores used when no mapping is present * section and click on  { } Edit mappings { } . 4. Update Cache administration of  Session  to the new file cache that you have created in step  #2  and Save Changes. 5. Purge Session Cache Manually: Navigate to the  moodledata  directory, locate the sessions directory, and delete the contents to clear the session. rm -rf /path/to/moodledata/sessions/*   6. Enable readonly sessions and sessions debug in config.php and visit the site. $CFG->enable_read_only_sessions = true ; $CFG->enable_read_only_sessions_debug = true ;  7. Create a `step-one.php` file in the root of your Moodle directory with the following content which simulates the default state the session is in before the navigation state is cached: <?php require( 'config.php' ); unset($SESSION->navcache); echo "Step one" ; 8. Create a `step-two.php` file in the root of your Moodle directory with the following content which simulates the cache being initialized, which counts as a write to the session manager: <?php define( 'READ_ONLY_SESSION' , true ); require( 'config.php' ); navigation_cache::destroy_volatile_caches(); echo "Step two" ;  9. Open a terminal and tail the error logs 10. Visit `step-one.php` and `step-two.php` in the browser in order with the following URLs: https://<wwwroot_url>/step-one.php https://<wwwroot_url>/step-two.php Confirm you do not see any error message in the error logs

      There are various places eg MDL-79617, MDL-73317 where read only sessions is detecting session mutations because the navcache is being either set or reset. But the nav cache should be a cache and it should be safe to reset it at any point in time. The root cause is that it is directly stored in $SESSION rather than it being managed the same as any other cache via MUC where it could be correctly mapped to another place outside of the session when readonly sessions is configured and enabled.

      It should be a fairly easy refactor to swap it over to MUC, this is pretty old code and just predates MUC I don't think there is any deeper reason why it isn't already using MUC.

      https://github.com/moodle/moodle/blob/master/lib/navigationlib.php#L6010-L6027

       

      Reproduction steps:

       
      1. Go to https://example.com/cache/admin.php
      2. Add an instance of File cache under {}Installed cache stores{} with any name you want.
      3. Scroll to the bottom of the page to the *Stores used when no mapping is present* section and click on {}Edit mappings{}.
      4. Update Cache administration of Session to the new file cache that you have created in step #2 and Save Changes.
      5. Purge Session Cache Manually: Navigate to the moodledata directory, locate the sessions directory, and delete the contents to clear the session.
      rm -rf /path/to/moodledata/sessions/*
       
      6. Enable readonly sessions and sessions debug in config.php and visit the site.
       
       

      $CFG->enable_read_only_sessions = true;
      $CFG->enable_read_only_sessions_debug = true;
      

       
      7. Create a `step-one.php` file in the root of your Moodle directory with the following content which simulates the default state the session is in before the navigation state is cached:
       

      require(__DIR__ . '/config.php');
      unset($SESSION->navcache); 
      echo "Step one";
      

      8. Create a `step-two.php` file in the root of your Moodle directory with the following content which simulates the cache being initialized, which counts as a write to the session manager:
       

      define('READ_ONLY_SESSION', true);
      require(__DIR__ . '/config.php');
      navigation_cache::destroy_volatile_caches();
      echo "Step two";
      

       
      9. Open a terminal and tail the error logs
      10. Visit `step-one.php` and `step-two.php` in the browser in order with the following URLs:

      • https://<wwwroot_url>/step-one.php
      • https://<wwwroot_url>/step-two.php

      Now, you will notice a similar error message on the terminal:

      [02-Jul-2024 13:05:28] WARNING: [pool www] child 9 said into stderr: "NOTICE: PHP message: Script /step-two.php defined READ_ONLY_SESSION but the following SESSION attributes were changed: $SESSION->navcache"

            rajandangi Rajan Dangi
            brendanheywood Brendan Heywood
            Owen Herbert Owen Herbert
            Jun Pataleta Jun Pataleta
            Kim Jared Lucas Kim Jared Lucas
            Votes:
            2 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 4 hours, 52 minutes
                4h 52m

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