Skip to content

perf(middleware): await background task cancellation on timeout#786

Open
ishaanxgupta wants to merge 1 commit intosupermemoryai:mainfrom
ishaanxgupta:ishaan/middleware
Open

perf(middleware): await background task cancellation on timeout#786
ishaanxgupta wants to merge 1 commit intosupermemoryai:mainfrom
ishaanxgupta:ishaan/middleware

Conversation

@ishaanxgupta
Copy link

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.

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