perf(middleware): await background task cancellation on timeout#786
perf(middleware): await background task cancellation on timeout#786ishaanxgupta wants to merge 2 commits intosupermemoryai:mainfrom
Conversation
|
@claude review this PR |
|
I'll analyze this and get back to you. |
|
hey @MaheshtheDev I think the claude review is failing, please have a look |
|
@ishaanxgupta please update the package version number and rest all looking good |
Done |
Confidence Score: 5/5 - Safe to MergeSafe to merge — this patch correctly addresses a classic concurrent-modification hazard in Key Findings:
Files requiring special attention
|

Summary
Added an await asyncio.gather(*tasks_to_cancel, return_exceptions=True) call inside the except asyncio.TimeoutError: block of wait_for_background_tasks
Previously, when the wait_for_background_tasks method timed out, it iterated over unfinished tasks and called task.cancel(). However, calling cancel() only requests cancellation; it doesn't wait for the task's cleanup logic (e.g., except asyncio.CancelledError:) to actually complete. This created orphaned tasks running asynchronously in the background.
Failing to await cancelled tasks leads to "Task was destroyed but it is pending!" errors at shutdown. More importantly, it leaks memory and limits scalability because background cleanup continues executing unbounded.