fix: keep a reference to the throttled cleanup background task (avoid GC before completion)#6226
Open
kratos0718 wants to merge 1 commit into
Open
Conversation
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.
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.
What
openai_create_vector_store_file_batchschedules the throttled expired-batch cleanup with a bareasyncio.create_task(...)whose result is discarded: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
RUF006and 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_tasksset with a self-cleaning done-callback (the standard pattern), and cancel those tasks inshutdown()alongside the existing file-batch tasks — matching the lifecycle handling already present in this mixin.Notes