Skip to content

Lock with RedisStore: LockAcquiringException on second acquisition of lock within the same process (after TTL) #38541

@MichaelS86

Description

@MichaelS86

Symfony version(s) affected: 5.1.7

Description
If I want to acquire and release twice within the same process whereby the second acquisition is attempted after the TTL, there is a LockAcquiringException which is the result of a LockExpiredException.
It seems that a lock can expire after it has been released which is an obstacle to a later re-acquisition.

IMHO it should be possible to acquire and release a lock as often as wanted.

How to reproduce

<?php


namespace App\Service;

use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\RedisStore;
use Symfony\Component\Lock\Store\RetryTillSaveStore;

class LockHelper
{
    protected $lock;

    public $initialTTL;

    public function __construct($redisHost) {
        $redis = new \Redis();
        $redis->connect($redisHost);

        $store = new RedisStore($redis);
        $store = new RetryTillSaveStore($store);
        $factory = new LockFactory($store);

        $this->initialTTL = 2;

        $this->lock =$factory->createLock('maintenance', $this->initialTTL);
    }

   // this reproduces the error
    public function testBadScenario() {
        $this->lock->acquire();
        $this->lock->release();
        Sleep($this->initialTTL+1);
        $this->lock->acquire();
        $this->lock->release();
    }

    ...
}

Possible Solution
I am not familiar with that but maybe this has to do something with Key $key which should undergo some kind of a reset in $this->lock->release()?

Additional context
The output of the test is as follows:

Symfony\Component\Lock\Exception\LockAcquiringException : Failed to acquire the "maintenance" lock.
 PROJECT_DIR/vendor/symfony/lock/Lock.php:108
 PROJECT_DIR/src/Service/LockHelper.php:38
 PROJECT_DIR/tests/Service/LockHelperTest.php:31
 
 Caused by
 Symfony\Component\Lock\Exception\LockExpiredException: Failed to store the "maintenance" lock.
 
PROJECT_DIR/vendor/symfony/lock/Store/ExpiringStoreTrait.php:27
 PROJECT_DIR/vendor/symfony/lock/Store/RedisStore.php:72
PROJECT_DIR/vendor/symfony/lock/Store/RetryTillSaveStore.php:54
PROJECT_DIR/vendor/symfony/lock/Lock.php:77
PROJECT_DIR/src/Service/LockHelper.php:38
PROJECT_DIR/tests/Service/LockHelperTest.php:31

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy