[Bug] Retention deletes related logs when backup fails
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 (verified at f011099bcd781e70d25b684b4b91b0b134ecb258)
Cloud or Self Hosted
Self Hosted (Source)
Steps to reproduce
On current main, ClearFreePlanTenantExpiredLogs._clear_message_related_tables deletes related message rows after attempting to archive them to object storage.
Reproduction: create a related row such as a MessageChain for a message, make storage.save raise (for example, an object-store outage), then call _clear_message_related_tables for that message ID inside a transaction. The method logs the exception and returns; after the transaction commits, the related row is gone and no archive was written. A record serialization error is also skipped before deleting all selected rows.
The parent message export does not contain MessageChain rows, so those records cannot be recovered from the message backup.
✔️ Expected Behavior
If a related-row backup cannot be serialized or saved, the cleanup should fail and leave those rows available for a retry.
❌ Actual Behavior
The cleanup logs the backup error and deletes the selected related rows anyway. A temporary storage failure can therefore permanently discard records that were not archived.
The existing unit tests explicitly expect this data-loss behavior: test_storage_failure_still_deletes_records commits after an injected storage failure and asserts the MessageChain is gone; test_serialization_failure_skips_backup_but_deletes_records asserts the same for a SavedMessage, with no backup written. Those expectations need to change alongside a maintainer fix. [1]
One additional regression case matters: process_tenant deletes the parent messages before calling _clear_message_related_tables, inside the same transaction. Coverage should therefore verify that a related-row archive failure propagates and rolls back both the parent deletion and any earlier related-table deletions in that batch, not merely that the failing table retains its rows. [2]
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.