Fix non-atomic async operation creation#619
Merged
nicoloboschi merged 2 commits intomainfrom Mar 19, 2026
Merged
Conversation
Previously the method performed two separate database round-trips: 1. INSERT into async_operations with no task_payload (null) 2. submit_task → UPDATE to set task_payload A process crash or network error between steps 1 and 2 left a row with task_payload IS NULL permanently. The worker's claim query requires task_payload IS NOT NULL, so these orphaned rows could never be picked up and the queue appeared degraded indefinitely. Fix: build full_payload before the INSERT and include task_payload in the same INSERT statement, making operation creation atomic. submit_task is still called afterwards — for SyncTaskBackend it executes the task immediately (unchanged behaviour); for BrokerTaskBackend it becomes an idempotent UPDATE (payload already set) kept for symmetry.
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.
Problem
_submit_async_operationcreated database rows in two separate, non-atomic steps:INSERTintoasync_operationswithtask_payload = NULLsubmit_task→ separateUPDATEto settask_payloadA process crash or network error between steps 1 and 2 left a permanently unclaimable row. The worker's claim query requires
task_payload IS NOT NULL, so these orphaned rows would remainpendingforever. Failures of this kind have been observed during periods of high load or API restarts.Fix
Build
full_payloadbefore theINSERTand includetask_payloadin the sameINSERTstatement, making operation creation atomic in a single database round-trip.submit_taskis still called afterwards:SyncTaskBackend: executes the task immediately — unchanged behaviourBrokerTaskBackend: performs an idempotentUPDATE(payload already set by theINSERT), kept for symmetryResult
No more null-payload ghost rows can be created. Existing null-payload rows from prior deployments can be cleaned up by marking them failed (they cannot be claimed regardless).