diff --git a/packages/openai-sdk-python/src/supermemory_openai/middleware.py b/packages/openai-sdk-python/src/supermemory_openai/middleware.py index 4f6dc8ec0..12100065c 100644 --- a/packages/openai-sdk-python/src/supermemory_openai/middleware.py +++ b/packages/openai-sdk-python/src/supermemory_openai/middleware.py @@ -554,9 +554,12 @@ async def wait_for_background_tasks(self, timeout: Optional[float] = 10.0) -> No f"Background tasks did not complete within {timeout}s timeout" ) # Cancel remaining tasks - for task in self._background_tasks: - if not task.done(): - task.cancel() + tasks_to_cancel = [task for task in self._background_tasks if not task.done()] + for task in tasks_to_cancel: + task.cancel() + + if tasks_to_cancel: + await asyncio.gather(*tasks_to_cancel, return_exceptions=True) raise def cancel_background_tasks(self) -> None: