Closed
Description
Laravel Version
11.26.0
PHP Version
8.2.24 (irrelevant)
Database Driver & Version
SQLite 3.37.2 (irrelevant)
Description
When scheduling events in App\Console\Kernel::schedule()
, and having an event not overlap itself for M
minutes, if the event takes too long and is overlapped but completes first, it releases the mutex that is owned by the newer event, allowing the newer event to be overlapped immediately regardless of M
.
Steps To Reproduce
- Schedule a command (Event-X)
- Have the command not overlap itself for
M
minutes; it holds a mutex forM
minutes. - Command takes longer than
M
minutes to complete, mutex expires. - Command is re-run (Event-Y), overlapping Event-X (this is fine), and Event-Y now has the mutex.
- Event-X completes, and
CacheEventMutex::forget()
force-releases Event-Y's mutex, which may still be running (this is not fine). - Event-Z can now immediately overlap Event-Y, regardless of
M
Solution:
CacheEventMutex::create()
must provide an$owner
to the lock, or retrieve the owner after creation. This prevents a random identifier from being used during lock creation and then immediately lost. Where this value lives can be discussed. It only needs to exist in the current runtime.CacheEventMutex::forget()
must not force-release if an owner is already known fromcreate()
. This allowsScheduleClearCacheCommand
to still force-release by not giving one.ScheduleFinishCommand
should accept an owner identifier to ensure that the owners are the same when attempting mutex release for background jobs. The owner, as determined in the same runtime, should be given byCommandBuilder::buildBackgroundCommand()
.- Failure to release upon job completion may be silent; the event is done, and overlap was allowed, no exception is necessary when another process holds the mutex.