fix(memory): do not leak temp file when RAG upload fails#6302
Open
agharsallah wants to merge 1 commit into
Open
fix(memory): do not leak temp file when RAG upload fails#6302agharsallah wants to merge 1 commit into
agharsallah wants to merge 1 commit into
Conversation
add_session_to_memory created a NamedTemporaryFile(delete=False) and only removed it on the success path. A missing rag-resources configuration or any rag.upload_file failure left the temp file on disk permanently. Validate rag resources before the temp file is created, and wrap the upload loop in try/finally so the file is always removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
VertexAiRagMemoryService.add_session_to_memorycreates atempfile.NamedTemporaryFile(delete=False)and only removes it viaos.removeon the success path. Two failure paths leak the filepermanently:
ValueError("Rag resources must be set.")raised when no ragresources are configured — the temp file is already on disk by then.
rag.upload_file— theos.removeat the end isnever reached.
Fix
so nothing is written to disk when the config is invalid.
os.removeintry/finallyso the temp fileis always deleted, even when the upload raises.
Tests
Added two cases to
test_vertex_ai_rag_memory_service.py:test_add_session_removes_temp_file_when_upload_fails— mocksrag.upload_fileto raise and asserts the temp file is gone afterward.test_add_session_does_not_create_temp_file_without_rag_resources—asserts no temp file is created when rag resources are unset.
All tests pass.