Skip to content

Fix org task locking: acquire non-blocking with default expiry, preserve last results on failure - #253

Merged
norkans7 merged 4 commits into
mainfrom
org-task-locking
Jul 28, 2026
Merged

Fix org task locking: acquire non-blocking with default expiry, preserve last results on failure#253
norkans7 merged 4 commits into
mainfrom
org-task-locking

Conversation

@norkans7

Copy link
Copy Markdown
Contributor

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_timeout for longer-running tasks), is released in a finally block, and last_results from the last successful run survives failures. Tests cover the skip-if-running path and results preservation across a failed run.

norkans7 added 2 commits July 27, 2026 13:34
…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
Comment thread dash/orgs/tasks.py Outdated
@norkans7
norkans7 merged commit 12b9534 into main Jul 28, 2026
16 of 28 checks passed
@norkans7
norkans7 deleted the org-task-locking branch July 28, 2026 12:37
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.

1 participant