From c91e803a383cc3a18e4d8f201d3c2ba4ad7b498d Mon Sep 17 00:00:00 2001 From: Yongteng Lei Date: Sat, 28 Feb 2026 14:43:35 +0800 Subject: [PATCH] Fix: close detached PIL image on JPEG save failure in encode_image (#13278) ### What problem does this PR solve? Properly close detached PIL image on JPEG save failure in encode_image. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- rag/utils/base64_image.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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: