Fix 1221 stateless memory leak #1239
Closed
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.
Fix memory leak in stateless StreamableHTTP sessions by creating request-scoped task groups instead of using the global task group, ensuring proper cleanup after each request.
Motivation and Context
Fixes #1221 - The simple-streamablehttp-stateless server had a memory leak where concurrent requests caused continuous memory growth without stabilization. Each stateless request was
spawning a long-lived task in the global task group without proper cleanup, causing task and resource accumulation over time.
How Has This Been Tested?
Breaking Changes
None. This is an internal implementation change that maintains the same API and behavior.
Types of changes
Checklist
Additional context
The fix creates a separate task group for each stateless request using
async with anyio.create_task_group()instead of spawning tasks in the global session manager's task group. Thisensures that when the request completes, the task group is properly cleaned up along with any associated tasks and resources.
The memory stabilization observed in testing indicates that the leak has been resolved, with the remaining memory growth likely due to normal Python interpreter overhead and connection pooling.