fix(worker): make submit_task idempotent when payload already set - #1097
Merged
Conversation
Follow-up to #1091. That PR made _submit_async_operation insert task_payload atomically in the same row that the async_operations row is created, closing a crash-window that left orphaned NULL-payload rows. The follow-up call to _task_backend.submit_task is still needed so SyncTaskBackend can execute the task inline in tests and embedded mode, but for BrokerTaskBackend the call redundantly UPDATEd task_payload and bumped updated_at on a row that was already claimable — and could even touch a row that a worker had already claimed and transitioned to processing/completed. Make the UPDATE a no-op when task_payload is already set by adding `AND task_payload IS NULL` to the WHERE clause. Existing callers that still rely on a two-step INSERT-then-submit pattern (legacy/ fallback) continue to work, but the common path stops writing to a row it has nothing new to say about. Also add two regression tests: - test_worker.py::test_submit_task_preserves_existing_payload locks in the idempotent semantics at the backend level. - test_async_batch_retain.py:: test_submit_async_operation_leaves_claimable_row_when_submit_task_fails simulates a crash between the INSERT transaction commit and submit_task by mocking submit_task to raise, and asserts the row is still born claimable (status=pending, task_payload populated). This is the invariant the original bug violated.
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.
Summary
Follow-up to #1091. That PR made
_submit_async_operationinserttask_payloadatomically in the same row that creates the async_operations record, closing the crash-window orphan bug. The follow-up call to_task_backend.submit_taskis still needed soSyncTaskBackendcan execute the task inline (tests + embedded mode), but forBrokerTaskBackendthe call was redundantly UPDATE-ingtask_payloadand bumpingupdated_aton a row that was already claimable — and could even touch a row that a worker had already claimed and transitioned to processing/completed.task_payloadis already set by addingAND task_payload IS NULLto the WHERE clause.memory_engine.py.Test plan
tests/test_worker.py::TestBrokerTaskBackend::test_submit_task_preserves_existing_payload— locks in the idempotent semantics at the backend level (payload not overwritten,updated_atnot bumped).tests/test_async_batch_retain.py::test_submit_async_operation_leaves_claimable_row_when_submit_task_fails— regression test for the original fix: files/retain upload problems and orphaned retains #1091 bug. Mockssubmit_taskto raise (simulating a crash between the INSERT commit and the follow-up call) and asserts the row is still born claimable (status=pending,task_payloadpopulated)../scripts/hooks/lint.shpasses