yii\redis\Mutex mutex expires very quickly

yii\redis\Mutex expiration is not working like I think, or, more likely, I don’t understand things like I think. :slight_smile:

I have an issue where my mutexes expire when I don’t expect them to. Honestly, I just don’t have a good handle on the expiration at all. #help

Okay, here is my configuration to start things off:


// http://stuff.cebe.cc/yii2docs/yii-redis-mutex.html
$config['components']['mutex'] = [
    'class' => 'yii\redis\Mutex',
    // auto-expire after 1 day just in case; oddly, this defaults to
    // 30 seconds!? if you leave this at 30s, then you'll have a lot
    // of process overlap, e.g., in yii2-scheduler.
    'expire' => 60 * 30, // 30 min
    // bug: for whatever reason, if this is not set the mutex
    // doesn't seem to do anything?
    'autoRelease' => true,
    'redis' => [
        'hostname' => $_SERVER['REDIS_HOSTNAME'],
        'port' => $_SERVER['REDIS_PORT'],
        'database' => $_SERVER['REDIS_DB_MUTEX'],

What I’m finding is my lock expires very quickly. Like, in a minute or so.

Here is an example of getting a mutex to lock some work:

    $mutex = new \yii\redis\Mutex();
    if ($mutex->acquire($key, Yii::$app->params['default.mutexLockTimeout'])) {
        return $mutex;
    } else {
        throw new \Exception('Unable to acquire lock: ' . $key);

Seems standard. Yet, a lot of times when I try to release the mutex I get a warning that it doesn’t exist. Here is an example of that:

    if (!$mutex->release($key)) {
        $msg = sprintf('Invalid key to release: ' . $key);
        //throw new \yii\web\ServerErrorHttpException($msg);
        \Yii::error($msg, __METHOD__);

Again, this doesn’t take 30 minutes per my mutex component config, it happens in a minute.

What am I doing wrong/misunderstanding?

My redis is just stock redis on Ubuntu. Nothing special and no customized configuration. I wouldn’t think it should matter but FYI.

How fast your request ends?