Skip to content

fix: files/retain upload problems and orphaned retains#1091

Merged
nicoloboschi merged 1 commit intovectorize-io:mainfrom
christerence:fix/file-retain-orphaned-retains
Apr 16, 2026
Merged

fix: files/retain upload problems and orphaned retains#1091
nicoloboschi merged 1 commit intovectorize-io:mainfrom
christerence:fix/file-retain-orphaned-retains

Conversation

@christerence
Copy link
Copy Markdown
Contributor

Summary

Closes a race between the file-convert-retain worker task and the async task backend where a crash or timeout could orphan retain operations permanently.

The bug: _handle_file_convert_retain_task inserted an async_operations row without task_payload, then separately called submit_task to set the payload. If a crash or timeout occurred between those two statements, the row was left with task_payload IS NULL. The worker's claim_batch query filters on task_payload IS NOT NULL, so those orphaned rows became permanently stuck as unclaimed pending tasks — file uploads that silently never completed.

Fix: Build full_retain_payload and serialize it to JSON before the transaction, then include task_payload as a column in the atomic INSERT. The row is now born with its payload, so a crash at any point either leaves no row (transaction rolled back) or a fully-claimable row.

memory_engine.py: _handle_file_convert_retain_task now inserts task_payload in the same INSERT statement that creates the async_operations row. submit_task is still called afterwards for the SyncTaskBackend path (inline execution) and is idempotent for BrokerTaskBackend (payload already set).

Test plan

  • uv run pytest tests/test_http_api_integration.py -v -- file/retain integration tests
  • ./scripts/hooks/lint.sh -- all passed
  • CI green

Include task_payload in the async_operations INSERT atomically instead
of the previous two-step INSERT-then-UPDATE approach. When a crash or
timeout occurred between the two statements, rows were left with
task_payload IS NULL. The worker claim query filters on
task_payload IS NOT NULL, so those orphaned rows became permanently
stuck as unclaimed pending tasks.
@christerence
Copy link
Copy Markdown
Contributor Author

Bugs found on our fork for https://www.tryferris.ai/

Copy link
Copy Markdown
Collaborator

@nicoloboschi nicoloboschi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, good catch!

@nicoloboschi nicoloboschi merged commit 9e30ae2 into vectorize-io:main Apr 16, 2026
2 checks passed
nicoloboschi added a commit that referenced this pull request Apr 16, 2026
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 added a commit that referenced this pull request Apr 16, 2026
)

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.
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.

2 participants