Skip to content
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

[10.x] Fixes ShouldBeUnique behavior for missing models #50211

Closed
wants to merge 1 commit into from

Conversation

naquad
Copy link
Contributor

@naquad naquad commented Feb 23, 2024

This PR is a follow-up on #49890 and tries to fix the issue by using a memo object that stores the information required to manage the unique lock (uniqueId and the name of the uniqueVia storage if present).

EDIT: A detailed explanation.

The unique job lock mechanism relies on the attributes and methods of the job class. However, these methods are not necessarily deterministic. For example, they might produce different outcomes or errors when called on the same object, especially if dependent objects have changed or no longer exist.

Additionally, if the job incorporates the Illuminate\Queue\SerializesModels trait and fails to unserialize due to a missing model, the unserialization process is interrupted by an Illuminate\Database\Eloquent\ModelNotFoundException.

Due to these factors, it may become impossible to retrieve the necessary information for managing the unique lock in the queue worker.

This scenario creates a special case where the job's failure to unserialize prevents the worker from releasing the lock, potentially causing a complete deadlock for that particular job class.

To resolve this issue, this pull request introduces a memo object Illuminate\Queue\UniqueHandler. This object gathers all essential information from the job object beforehand, which can later be used to manage the unique lock w/o relying on the command instance. This approach effectively separates the job lock information from the job object itself and guarantees that both the dispatcher and the worker are working with the same unique lock.

@naquad naquad changed the title fix laravel/framework#49890: ShouldBeUnique behavior for missing models [10.x] fix laravel/framework#49890: ShouldBeUnique behavior for missing models Feb 23, 2024
@crynobone crynobone changed the title [10.x] fix laravel/framework#49890: ShouldBeUnique behavior for missing models [10.x] Fixes ShouldBeUnique behavior for missing models Feb 23, 2024
@crynobone crynobone linked an issue Feb 23, 2024 that may be closed by this pull request
@driesvints
Copy link
Member

@naquad could you please add a much more thorough explanation for this one? That'll give a much higher chance of it being accepted.

@naquad
Copy link
Contributor Author

naquad commented Feb 23, 2024

@naquad could you please add a much more thorough explanation for this one? That'll give a much higher chance of it being accepted.

Updated, please let me know if that's good enough.

@taylorotwell
Copy link
Member

Thanks for the PR but the explanation is not clear. What are two simple jobs I can paste into my application that will recreate this issue?

@taylorotwell taylorotwell marked this pull request as draft February 23, 2024 15:32
@naquad
Copy link
Contributor Author

naquad commented Feb 23, 2024

Thanks for the PR but the explanation is not clear. What are two simple jobs I can paste into my application that will recreate this issue?

The original issue contains a PoC along with the reproduction steps: #49890 (comment) (not copy-pasting to avoid the clutter).

@driesvints
Copy link
Member

@naquad remember Taylor doesn't sees draft PR's. Mark this as ready if you want another review.

@naquad naquad marked this pull request as ready for review February 23, 2024 15:37
@taylorotwell
Copy link
Member

Your reproduction steps are too long and unclear. Why are there table create statements in them, etc?

@taylorotwell taylorotwell marked this pull request as draft February 23, 2024 15:38
@naquad
Copy link
Contributor Author

naquad commented Feb 23, 2024

@driesvints could you please help me? I've been trying to make the STR as succint and simple as possible, reducing everything down to a self-contained script exposing the issue, so I'm not sure how to simplify the three steps :(

@driesvints
Copy link
Member

@naquad best is if you create a laravel app with a separate commit with custom changes to reproduce the issue:

laravel new bug-report --github="--public"

@naquad
Copy link
Contributor Author

naquad commented Feb 23, 2024

@driesvints
Copy link
Member

Yes that looks good

@naquad
Copy link
Contributor Author

naquad commented Feb 23, 2024

@taylorotwell please review the following PoC app: https://github.com/naquad/laravel-bug-report-49890
along with the particular commit: naquad/laravel-bug-report-49890@cd68b9c

The App\Models\Record is the basic model, the App\Jobs\ProcessRecord is the unique job that exposes the behavior.
Invoke ./artisan app:poc to see the leftover unique lock.

@naquad naquad marked this pull request as ready for review February 23, 2024 16:36
@taylorotwell
Copy link
Member

I don't have the bandwidth to review this right now. If you can fix the issue in a much simpler way please submit a simpler solution that doesn't require so many code changes.

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.

Inconsistent ShouldBeUnique behavior for missing models
3 participants