-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Open
zackAJ
wants to merge
11
commits into
laravel:11.x
Choose a base branch
from
zackAJ:fix-unique_jobs
base: 11.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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