Skip to content

Updating last_login_at leads to deadlocks #207

@MatanYadaev

Description

@MatanYadaev

Due to a high concurrency on my company's API, Sanctum leads to deadlocks.
This line of code, which updating last_login_at is responsible for these deadlocks.

tap($accessToken->forceFill(['last_used_at' => now()]))->save()

My company doesn't need this last_login_at column, and I can see that Sanctum doesn't use it anywhere.
It seems like an optional column. I guess it exists just for those who'd like to display it to the users or to make some logic above it.

I think Sanctum should provide the ability to choose whether this column is "working" or not. It should be configurable in my opinion.

What do you think guys?


As a workaround, I've used an observer with a return false to cancel this update query.

<?php

namespace App\Observers;

use Laravel\Sanctum\PersonalAccessToken;

class PersonalAccessTokenObserver
{
    public function updating(PersonalAccessToken $accessToken)
    {
        $dirtyAttributes = array_keys($accessToken->getDirty());

        if (count($dirtyAttributes) === 1 && $dirtyAttributes[0] === 'last_used_at') {
            return false;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions