Skip to content

fix(worker): make submit_task idempotent when payload already set - #1097

Merged
nicoloboschi merged 1 commit into
mainfrom
fix/submit-task-idempotent-skip
Apr 16, 2026
Merged

fix(worker): make submit_task idempotent when payload already set#1097
nicoloboschi merged 1 commit into
mainfrom
fix/submit-task-idempotent-skip

Conversation

@nicoloboschi

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1091. That PR made _submit_async_operation insert task_payload atomically in the same row that creates the async_operations record, closing the crash-window orphan bug. The follow-up call to _task_backend.submit_task is still needed so SyncTaskBackend can execute the task inline (tests + embedded mode), but for BrokerTaskBackend the call was redundantly UPDATE-ing task_payload and bumping 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 continue to work via the legacy fallback path.
  • Updated the now-stale "idempotent UPDATE" comments at both call sites in 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_at not 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. Mocks submit_task to raise (simulating a crash between the INSERT commit and the follow-up call) and asserts the row is still born claimable (status=pending, task_payload populated).
  • ./scripts/hooks/lint.sh passes
  • CI green

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.
@nicoloboschi
nicoloboschi merged commit 088dfec into main Apr 16, 2026
156 of 159 checks passed
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