Skip to content

sse.py - fixing yield placement issue #642

Open
@gregers666

Description

@gregers666

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions