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): close base64 file lookup sessions (#36308)

Escape0707c07686928adb
diff --git a/api/services/attachment_service.py b/api/services/attachment_service.py
index 54e664e9443..dad7163739f 100644
--- a/api/services/attachment_service.py
+++ b/api/services/attachment_service.py
@@ -22,10 +22,11 @@ class AttachmentService:
raise AssertionError("must be a sessionmaker or an Engine.")
def get_file_base64(self, file_id: str) -> str:
- upload_file = self._session_maker(expire_on_commit=False).scalar(
- select(UploadFile).where(UploadFile.id == file_id).limit(1)
- )
- if not upload_file:
- raise NotFound("File not found")
- blob = storage.load_once(upload_file.key)
+ with self._session_maker(expire_on_commit=False) as session:
+ upload_file = session.scalar(select(UploadFile).where(UploadFile.id == file_id).limit(1))
+ if not upload_file:
+ raise NotFound("File not found")
+ upload_file_key = upload_file.key
+
+ blob = storage.load_once(upload_file_key)
return base64.b64encode(blob).decode()
diff --git a/api/services/file_service.py b/api/services/file_service.py
index cd412638e05..4d3afcc9ad4 100644
--- a/api/services/file_service.py
+++ b/api/services/file_service.py
@@ -131,12 +131,13 @@ class FileService:
return file_size <= file_size_limit
def get_file_base64(self, file_id: str) -> str:
- upload_file = self._session_maker(expire_on_commit=False).scalar(
- select(UploadFile).where(UploadFile.id == file_id).limit(1)
- )
- if not upload_file:
- raise NotFound("File not found")
- blob = storage.load_once(upload_file.key)
+ with self._session_maker(expire_on_commit=False) as session:
+ upload_file = session.scalar(select(UploadFile).where(UploadFile.id == file_id).limit(1))
+ if not upload_file:
+ raise NotFound("File not found")
+ upload_file_key = upload_file.key
+
+ blob = storage.load_once(upload_file_key)
return base64.b64encode(blob).decode()
def upload_text(self, text: str, text_name: str, user_id: str, tenant_id: str) -> UploadFile: