correct logic in lock check to fix async mode lockup bug #8
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.
This PR fixes a bug when BACKGROUND_TASK_RUN_ASYNC=True and BACKGROUND_TASK_ASYNC_THREADS=# where # of tasks have been marked locked but are never considered unlocked due to expiration, causing all task processing to cease until the tasks are manually deleted.
locked() and unlocked() currently return conflicting sets of tasks, where certain tasks are returned by both locked() and unlocked(), due to a logic error in the query set. This leads to a state where a number of tasks equal to BACKGROUND_TASK_ASYNC_THREADS are all marked locked_by but are not running and the lock has expired, yet the count of tasks available to be run in asynchronous mode will always be non-positive which blocks further processing.
The fix is to invert the logic of locked criteria: locked_by must be present AND expires_at must be greater than locked_at. Otherwise, locked_by always takes precedent and all tasks that have been locked will remain locked indefinitely.