Fix org task locking: acquire non-blocking with default expiry, preserve last results on failure - #253
Open
norkans7 wants to merge 4 commits into
Open
Fix org task locking: acquire non-blocking with default expiry, preserve last results on failure#253norkans7 wants to merge 4 commits into
norkans7 wants to merge 4 commits into
Conversation
…rve last results on failure - Acquire the org task lock non-blocking instead of a racy get + blocking lock, so a concurrent invocation skips immediately rather than blocking and re-running the task - Default the lock timeout to 1 hour so a killed worker can't leave the lock held forever - Release the lock on both success and failure paths - Stop clearing last_results on task failure so incremental tasks resume from their last successful results
…the API - Catch LockError when releasing the org task lock so a task that outlives its lock timeout doesn't fail a successful run or mask its own exception, and warn instead - Move the default lock timeout into the org_task/maybe_run_for_org signatures and document that a task running longer than its timeout may be started concurrently - Remove the unused ORG_TASK_LOCK_KEY constant and document that last_results holds the results of the last successful run - Add tests for lock expiry during a run and for the lock TTL being set
norkans7
commented
Jul 27, 2026
This file contains hidden or 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
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.
Org task locking had three defects: the lock pre-check and acquisition weren't atomic and used a blocking acquire, so a racing worker could pin a celery slot for the whole task duration and then immediately re-run the same task; the default lock had no expiry, so a hard-killed worker wedged the task forever until the key was manually deleted; and a single failed run cleared
last_results, losing the incremental state that resumable tasks depend on.The lock is now acquired non-blocking (skipping with a log line if already held), gets a 1-hour default expiry (still overridable via
lock_timeoutfor longer-running tasks), is released in a finally block, andlast_resultsfrom the last successful run survives failures. Tests cover the skip-if-running path and results preservation across a failed run.