Skip to content
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

ASGITransport does not stream responses. #2186

Open
1 task done
havardthom opened this issue Apr 21, 2022 · 6 comments · May be fixed by #3059
Open
1 task done

ASGITransport does not stream responses. #2186

havardthom opened this issue Apr 21, 2022 · 6 comments · May be fixed by #3059

Comments

@havardthom
Copy link

havardthom commented Apr 21, 2022

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: tiangolo/fastapi#2006

@tomchristie tomchristie changed the title Testing endless SSE endpoint with AsyncClient hangs forever ASGITransport does not stream responses. May 6, 2022
@tomchristie
Copy link
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.

@aditya-n-invcr
Copy link

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

@florimondmanca
Copy link
Member

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.

@Kludex Kludex assigned Kludex and unassigned Kludex Nov 6, 2022
@Kludex
Copy link
Sponsor Member

Kludex commented Nov 6, 2022

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

EDIT on 28 July 2023: I lied.

@richardhundt
Copy link

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

@tomchristie
Copy link
Member

tomchristie commented Oct 11, 2023

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.

jhominal added a commit to jhominal/httpx that referenced this issue Jan 16, 2024
jhominal added a commit to jhominal/httpx that referenced this issue Jan 17, 2024
jhominal added a commit to jhominal/httpx that referenced this issue Jan 17, 2024
jhominal added a commit to jhominal/httpx that referenced this issue Jan 17, 2024
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

Successfully merging a pull request may close this issue.

6 participants