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

postgres_lock_factory allows multiple of same lock to be taken in the same DB session

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • 4.5
    • Other
    • None
    • MOODLE_405_STABLE

      It seem the postgres_lock_factory allows multiple of the same lock to be taken out.

      For example, if you run this as a unit test, it will fail, because $lock2 will be successfully obtained.

      public function test_pg_locks() {
          $factory1 = new \core\lock\postgres_lock_factory('test');
          $lock1 = $factory1->get_lock('myresource', 0);
          $this->assertNotEmpty($lock1);
       
          $factory2 = new \core\lock\postgres_lock_factory('test');
          $lock2 = $factory2->get_lock('myresource', 0);
          $this->assertFalse($lock2);
       
          $lock1->release();
       }

      To me, if the namespace + resource is the same, the lock should fail to be obtained regardless of the method.

      I believe this fails above because of a few preconditions that aren't normally fulfilled:
      1. The factory instance being different. This is because the factory does an internal check if it already has the lock (without even going to postgres). Making a new factory instance skips this.

      2. The locks being obtained in the same DB session. I'm not super familiar with the internals of pg locks, but I believe if the session is the same postgres will return any advisory locks taken in the same session.

      If you change the factory to something else e.g. \core\lock\file_lock_factory, it passes.

      My local environment, in case it's relevant:

      • Moodle master (currently 2024042200.01)
      • PHP 8.1
      • Pgbouncer
      • Postgres 14

            Unassigned Unassigned
            matthewhilton Matthew Hilton
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:

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