Skip to content

ASGITransport does not stream responses. #2186

Open

Description

@havardthom

Current behaviour:

  • Running uvicorn test_endless_sse:app --reload then python test_endless_sse.py works.
  • Running pytest test_endless_sse.py hangs forever.

Expected behaviour:

  • pytest test_endless_sse.py should pass
# test_endless_sse.py
import pytest
import httpx
import asyncio

from fastapi import FastAPI
from sse_starlette.sse import EventSourceResponse

app = FastAPI()

@app.get("/endless_sse")
async def endless_sse():
    async def event_publisher():
        i = 0
        try:
            while True:
                i += 1
                yield dict(data=i)
                await asyncio.sleep(0.2)
        except asyncio.CancelledError as e:
            raise e

    return EventSourceResponse(event_publisher())

# This test should pass but it hangs forever and prints no output
@pytest.mark.asyncio
async def test_endless_sse():
    async with httpx.AsyncClient(app=app, base_url="http://test") as aclient:
        async with aclient.stream("GET", "/endless_sse") as response:
            async for line in response.aiter_lines():
                line_stripped = line.strip()
                print("line_stripped: ", line_stripped)
                if line_stripped == "data: 10":
                    break


if __name__ == "__main__":
    # Run: uvicorn test_endless_sse:app --reload

    # Check that we get output from the app running in uvicorn
    async def fetch_endless_sse():
        async with httpx.AsyncClient(base_url="http://127.0.0.1:8000") as aclient:
            async with aclient.stream("GET", "/endless_sse") as response:
                async for line in response.aiter_lines():
                    line_stripped = line.strip()
                    print("line_stripped: ", line_stripped)
                    if line_stripped == "data: 10":
                        break


    asyncio.run(fetch_endless_sse())
python 3.8.0

httpx==0.22.0
pytest==7.1.1
pytest-asyncio==0.18.3
fastapi==0.75.2
uvicorn==0.17.6
sse-starlette==0.10.3

Related: fastapi/fastapi#2006

Activity

  1. changed the title [-]Testing endless SSE endpoint with AsyncClient hangs forever[/-] [+]ASGITransport does not stream responses.[/+] on May 6, 2022
  2. lovelydinosaur commented on May 6, 2022

    @lovelydinosaur
    Member

    You've identified this correctly in your comment here... #1787 (comment)

    The issue is that the ASGITransport doesn't stream responses.
    I've retitled this issue to be more specific regarding what's needed to resolve this.

  3. aditya-n-invcr commented on Nov 1, 2022

    @aditya-n-invcr

    @tomchristie Is there any update on this one ? Or has it already been fixed ?

  4. florimondmanca commented on Nov 6, 2022

    @florimondmanca
    Contributor

    As mentioned in havardthom's comment, I had a previous PR from a long time ago here which attempted to solve something that would resolve this: #998

    It's okay to start off that PR, adjusting anything that needs to be due to changes in HTTPX since then.

  5. assigned and unassigned on Nov 6, 2022
  6. Kludex commented on Nov 6, 2022

    @Kludex
    Contributor

    If no one takes this until the end of the year, I will.

    EDIT on 28 July 2023: I lied.

  7. richardhundt commented on Jul 28, 2023

    @richardhundt

    I've run into the same issue, and unfortunately @florimondmanca's branch didn't work for me (all requests hang, not just SSE streams). I used that as inspiration though, and am monkey patching the transport in my tests. Here's a gist: https://gist.github.com/richardhundt/17dfccb5c1e253f798999fc2b2417d7e

  8. lovelydinosaur commented on Oct 11, 2023

    @lovelydinosaur
    Member

    My suggestion would be that we document this as a known constraint.

    If someone is invested in resolving it, then implementing it as a third party transport would be a good first step.

  9. added 4 commits that reference this issue on Jan 16, 2024
    dcc23a2
    460832c
    e249563
    672f459
  10. galah92 commented on Aug 28, 2024

    @galah92

    I've got the same problem - FastAPI SSE endpoint I'd like to test.
    Is there a workaround for this?

  11. added 4 commits that reference this issue on Jan 25, 2025
    454723e
    e8d1339
    a0710f0
    2f771af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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