-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[11.x] Fix unique job lock is not released on model not found exception, lock gets stuck. #54000
Conversation
…hidden context wrapper, only when ModelNotFound exception is thrown.
I'll handle the 5 failing checks |
done |
I'm curious - is the lock released if you use this: https://laravel.com/docs/11.x/queues#ignoring-missing-models |
…e releasing of the lock to be before all returns
nope it's not, I've updated the test btw to run correctly both locally and in CI. |
@taylorotwell any comments on this ? |
Thanks! |
SAHA 🇩🇿 |
Description
When using
SerializesModels
on aUnique job
and the model is deleted before the job is being processed,ModelNotFoundException
will be thrown and the job will fail and that's okay and well documented.However the unique lock is not released and gets stuck until expired, this happens a lot with delayed jobs.
Related issues: #50211, #49890, possibly #37729
Steps to reproduce
1- Create or open a laravel project, for ease of inspecting use
database
cache driver.2- Create
TestJob.php
in your app/Jobs :3- Add a test route to your
web.php
:4- Run the web and queue servers via
composer run dev
and hit the/test
route5- Refresh the page and inspect your database's
cache_locks
andfailed_jobs
tablesCause
TLDR
No serialized job command instance = no unique lock to release
In depth
callQueuedHanlder.php
'scall()
is responsible for handling the queued job, when we try to unserialize the payload of the jobModelNotFoundException
is thrown because the model was deleted.In this case we don't have a job command instance which is currently required to release the lock.
after
$this->handleMedelNotFound(...)
is done the job is marked asfailed
and thefailed()
method will be called:Solution
The idea
Wrap the job with a hidden context in order to have access to the lock key and the cache driver used, here's a simple overview of the flow:
Now when handling
ModelNotFoundException
via thehandleModelNotFound()
method, we can forceRelease the lock by using the providedcache_driver
andlock_key
from the hidden contextImplementation
See code diff.If this gets closed for any reason use my test to research and implement another possible solution, add this to
tests/Integration/Queue/UniqueTestJob.php