Skip to content

fix(memory): do not leak temp file when RAG upload fails#6302

Open
agharsallah wants to merge 1 commit into
google:mainfrom
agharsallah:fix/rag-memory-temp-file-leak
Open

fix(memory): do not leak temp file when RAG upload fails#6302
agharsallah wants to merge 1 commit into
google:mainfrom
agharsallah:fix/rag-memory-temp-file-leak

Conversation

@agharsallah

Copy link
Copy Markdown

Problem

VertexAiRagMemoryService.add_session_to_memory creates a
tempfile.NamedTemporaryFile(delete=False) and only removes it via
os.remove on the success path. Two failure paths leak the file
permanently:

  1. The ValueError("Rag resources must be set.") raised when no rag
    resources are configured — the temp file is already on disk by then.
  2. Any exception from rag.upload_file — the os.remove at the end is
    never reached.

Fix

  • Move the rag-resource validation before the temp file is created,
    so nothing is written to disk when the config is invalid.
  • Wrap the upload loop and os.remove in try/finally so the temp file
    is 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 — mocks
    rag.upload_file to 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant