Description
Describe the bug
Incorrect placement of the yield
statement in the asynchronous context manager sse_client
, causing issues with the lifecycle management of asynchronous tasks and potentially leading to improper resource cleanup.
To Reproduce
- Import the
sse_client
function from the module - Use this function in an
async with
block to establish an SSE connection - Attempting to exit the context may cause hanging or unexpected behavior
- Problems occur with proper resource cleanup (streams, HTTP connections)
Expected behavior
The sse_client
context manager should properly initialize the connection, yield communication streams, and correctly close all resources when exiting the context block, regardless of how the context exits (normally or through an exception).
Screenshots
N/A
Additional context
The issue stems from the incorrect nesting structure of asynchronous blocks. Specifically, the yield
statement is placed at the wrong nesting level in relation to the task group (anyio.create_task_group()
), which disrupts the natural flow of resource management.
The solution involves restructuring the code to first establish the HTTP and SSE connections, and only then start the asynchronous tasks in a group, with the yield
statement at the appropriate nesting level, ensuring the correct sequence of resource initialization and cleanup.