Skip to content

onOneServer() locks are not stored on lock_connection #55610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mortenscheel opened this issue Apr 30, 2025 · 1 comment
Open

onOneServer() locks are not stored on lock_connection #55610

mortenscheel opened this issue Apr 30, 2025 · 1 comment

Comments

@mortenscheel
Copy link

Laravel Version

12.10.2

PHP Version

8.3.20

Database Driver & Version

No response

Description

Context

In config/cache.php you can define a separate connection to use for locks:
https://github.com/laravel/laravel/blob/f6e4638ee6ca1cd40aa7c56311d89ea3d91a24f8/config/cache.php#L74-L78

This makes it possible to run cache:clear without losing mutexes and atomic locks.
The lock_connection is used when calling

  • Cache::lock(...)->get()
  • Schedule::command(...)->withoutOverlapping()

It might be used in other contexts too, but those are the ones I'm aware of.

The problem

Schedule::command(...)->onOneServer() creates the lock on the regular cache connection via

public function create(Event $event, DateTimeInterface $time)
{
return $this->cache->store($this->store)->add(
$event->mutexName().$time->format('Hi'), true, 3600
);
}

Which means that cache:clear will wipe those locks and allow another server to pick up the task.

Thoughts

I'm not familiar enough with the source to send a PR, and I don't know if there's a good reason for this behavior. Naively I would assume that onOneServer() should use CacheEventMutex in stead of CacheSchedulingMutex(which is apparently only used in this context).

Steps To Reproduce

  1. Use a cache driver with a separate lock_connection
  2. Configure a scheduled job with onOneServer(), which calls sleep(60).
  3. Run schedule:run
  4. Observe where the lock is stored.
@mortenscheel
Copy link
Author

Some people are suggesting complicated workarounds for this issue: https://stackoverflow.com/questions/79183842/laravel-ononeserver-method-of-task-scheduler-break-with-php-artisan-cacheclea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant