Skip to content

fix: keep a reference to the throttled cleanup background task (avoid GC before completion)#6226

Open
kratos0718 wants to merge 1 commit into
ogx-ai:mainfrom
kratos0718:fix/vector-store-cleanup-task-reference
Open

fix: keep a reference to the throttled cleanup background task (avoid GC before completion)#6226
kratos0718 wants to merge 1 commit into
ogx-ai:mainfrom
kratos0718:fix/vector-store-cleanup-task-reference

Conversation

@kratos0718

Copy link
Copy Markdown

What

openai_create_vector_store_file_batch schedules the throttled expired-batch cleanup with a bare asyncio.create_task(...) whose result is discarded:

asyncio.create_task(self._cleanup_expired_file_batches())

The event loop keeps only a weak reference to a task, so a fire-and-forget task can be garbage-collected before it finishes — silently dropping the cleanup mid-run. This is the pattern behind Ruff's RUF006 and the asyncio docs warning.

Notably, the file-batch task a few lines above is already handled correctly (self._file_batch_tasks[batch_id] = task) — this cleanup task just slipped through.

Fix

Keep a strong reference via a dedicated self._background_tasks set with a self-cleaning done-callback (the standard pattern), and cancel those tasks in shutdown() alongside the existing file-batch tasks — matching the lifecycle handling already present in this mixin.

Notes

  • Behavior-preserving: the cleanup runs exactly as before; it just can't be GC'd mid-flight.
  • Minimal diff, no new dependencies.

asyncio.create_task(self._cleanup_expired_file_batches()) discarded its result,
so the event loop's weak reference allowed the cleanup task to be garbage-
collected before completion, silently dropping the cleanup mid-run. Store it in
a dedicated _background_tasks set with a self-cleaning done-callback, and cancel
those tasks on shutdown() alongside the existing file-batch tasks.
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