Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: close detached PIL image on JPEG save failure in encode_image (#13278)

Yongteng Leic91e803a383c
diff --git a/rag/utils/base64_image.py b/rag/utils/base64_image.py
index 5a328dd3ea..bc73c0433f 100644
--- a/rag/utils/base64_image.py
+++ b/rag/utils/base64_image.py
@@ -64,17 +64,17 @@ async def image2id(d: dict, storage_put_func: partial, objname: str, bucket: str
try:
img.save(buf, format="JPEG")
+ buf.seek(0)
+ return buf.getvalue()
except OSError as e:
logging.warning(f"Saving image exception: {e}")
return None
-
- buf.seek(0)
- if close_after:
- try:
- img.close()
- except Exception:
- pass
- return buf.getvalue()
+ finally:
+ if close_after:
+ try:
+ img.close()
+ except Exception:
+ pass
jpeg_binary = await thread_pool_exec(encode_image)
if jpeg_binary is None: