Skip to content

sse.py - fixing yield placement issue #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gregers666 opened this issue May 6, 2025 · 2 comments
Open

sse.py - fixing yield placement issue #642

gregers666 opened this issue May 6, 2025 · 2 comments

Comments

@gregers666
Copy link

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

  1. Import the sse_client function from the module
  2. Use this function in an async with block to establish an SSE connection
  3. Attempting to exit the context may cause hanging or unexpected behavior
  4. 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.

sse_py.txt

@kavinkumar807
Copy link

kavinkumar807 commented May 7, 2025

@gregers666 added your fix as part of my existing PR, let me know the changes are valid

@kavinkumar807
Copy link

I've been following this PR for this issue for past one month. No response from maintainers.
cc: @ihrpr @dsp-ant

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

No branches or pull requests

2 participants