Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): avoid infinite loop in _delete_records when batch deletion fails (#38118)

蒜香飞行矮堇瓜089c3f4af02b
diff --git a/api/tasks/remove_app_and_related_data_task.py b/api/tasks/remove_app_and_related_data_task.py
index d0763a7a1ad..a4fb6d57207 100644
--- a/api/tasks/remove_app_and_related_data_task.py
+++ b/api/tasks/remove_app_and_related_data_task.py
@@ -701,16 +701,29 @@ def _delete_records(query_sql: str, params: dict[str, Any], delete_func: Callabl
if not rows:
break
+ success_count = 0
for i in rows:
record_id = str(i.id)
try:
delete_func(session, record_id)
logger.info(click.style(f"Deleted {name} {record_id}", fg="green"))
+ session.commit()
+ success_count += 1
except Exception:
logger.exception("Error occurred while deleting %s %s", name, record_id)
# continue with next record even if one deletion fails
session.rollback()
- break
- session.commit()
+ continue
rs.close()
+
+ # If we couldn't delete ANY records in this batch, we must break out of the while loop
+ # to prevent an infinite loop where we keep fetching the same failing records.
+ if success_count == 0:
+ logger.warning(
+ click.style(
+ f"Failed to delete any {name} in the current batch. Stopping to prevent infinite loop.",
+ fg="yellow",
+ )
+ )
+ break