Transient storage failures orphan deleted segment attachments
Self Checks
- [x] I have read the Contributing Guide and Language Policy.
- [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.
Dify version
main (3b51e33dcd)
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
- Run a self-hosted Dify instance with a multimodal knowledge dataset.
- Add a document and an image attachment to a segment, then wait for indexing to complete.
- Delete the segment that owns the attachment and make the configured storage backend temporarily fail its delete operation (for example, inject a transient backend error).
- Let the delete_segment_from_index_task run, then restore storage and inspect the attachment object and its database rows.
On current main, api/tests/unit_tests/tasks/test_delete_segment_from_index_task.py::test_delete_segment_keeps_database_cleanup_when_attachment_storage_delete_fails injects this failure: the task logs it but removes the binding and UploadFile row before returning.
✔️ Expected Behavior
After a transient object-storage failure, Dify should keep enough durable attachment state to retry cleanup. Once storage recovers, the object should be removed without requiring manual bucket cleanup.
❌ Actual Behavior
The task commits deletion of SegmentAttachmentBinding and UploadFile before deleting the storage object. If storage.delete fails, it only logs the exception and returns successfully. The object remains in storage, but its key and attachment references are gone, so there is no later retry path.
Related: #41399. This occurs when storage.delete raises during the existing cleanup.
Retrying the task alone would not recover this case: attachment cleanup starts by querying SegmentAttachmentBinding, and that query returns nothing after the first run commits those deletions. Also, both the per-key storage exception handler and the outer task handler swallow exceptions, so simply re-raising from the inner handler would still not schedule a retry. [1]
The existing test explicitly asserts that both database rows disappear after storage.delete() fails, so it currently preserves the behavior you’re reporting rather than recovery after a transient failure. [2]
For the maintainer fix, the key requirement is durable cleanup state that survives the failure, plus an explicit retry mechanism, not just another storage-delete attempt in the same invocation. A useful regression test would cover failure, storage recovery, and a later attempt successfully deleting the object, while retaining the existing protection for attachments still bound to another segment. The current task already performs that shared-attachment check. [1]
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
Thanks, agreed. The fix is now in PR #44434: it keeps the attachment binding and UploadFile row in the transaction until storage deletion succeeds, rolls back and schedules a bounded Celery retry on storage errors, and treats a missing object as success. The regression test injects a failure, verifies the database references remain, then retries successfully after storage recovers.