Skip to content

Fix: Unique lock not being released after transaction rollback in ShouldBeUnique jobs with afterCommit() #55420

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

Merged
merged 4 commits into from
Apr 18, 2025

Conversation

toshitsuna-otsuka
Copy link
Contributor

@toshitsuna-otsuka toshitsuna-otsuka commented Apr 15, 2025

This pull request addresses an issue where ShouldBeUnique jobs scheduled with afterCommit() would fail to execute after a transaction rollback.

The problem occurred because when a ShouldBeUnique job is dispatched, a unique lock is created. Under normal circumstances, this lock is released after the job has been executed successfully. However, if the transaction within which the job was dispatched rolled back, the unique lock would remain in place. This would prevent any subsequent dispatch of the same ShouldBeUnique job from being processed, as the unique lock would still exist.

This pull request introduces a fix to ensure that when a transaction rollback occurs, any associated unique locks created by ShouldBeUnique jobs dispatched with afterCommit() are properly released. This ensures that the jobs can be dispatched and executed correctly even after a transaction rollback.

e.g.

  • some job
class SampleJob implements ShouldQueue, ShouldBeUnique
{
    ...
}
  • job dispatch
\DB::transaction(function() {
    SampleJob::dispatch()->afterCommit();

    // Exception occurred
    // At this time, the transaction is rolled back, but the unique lock for the job remains.
    // As a result, the job will not be executed again until the unique lock times out.
    // This PR fixes that issue.
});

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@toshitsuna-otsuka toshitsuna-otsuka marked this pull request as ready for review April 16, 2025 02:23
@taylorotwell taylorotwell merged commit 1f184bb into laravel:12.x Apr 18, 2025
58 checks passed
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

Successfully merging this pull request may close these issues.

2 participants