Skip to content

Instantly share code, notes, and snippets.

@liavkoren
Last active May 4, 2022 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liavkoren/3a7b2773e3ede681d2786bfd83bc14a7 to your computer and use it in GitHub Desktop.
Save liavkoren/3a7b2773e3ede681d2786bfd83bc14a7 to your computer and use it in GitHub Desktop.
Scripts that will reliably reproduce an AnyIO.EndOfStream error when using ContextMiddleware
# Note, I'm not running the docker process directly, instead I'm building the container and bashing in to start
# `$ python fast_server.py` and `python fast_client.py`.
FROM python:3.9.7
ARG REQUIREMENTS_FILE=requirements-dev.txt
WORKDIR /code
ADD requirements.txt requirements-*.txt /code/
RUN pip install -r $REQUIREMENTS_FILE
ADD . /code/
VOLUME /code
# CMD ["ddtrace-run", "--profiling", "python", "-m", "fast_app"]
import time
from fastapi import FastAPI, Request
from starlette.middleware.cors import CORSMiddleware
from starlette_context.middleware import ContextMiddleware
from starlette_context import plugins
app = FastAPI()
# Commenting out line 11 resolves the error:
app.add_middleware(ContextMiddleware, plugins=[plugins.CorrelationIdPlugin()])
@app.middleware("http")
async def middleware(request: Request, call_next):
return await call_next(request)
@app.get("/")
def read_root():
time.sleep(4)
return {"Hello": "World"}
import requests
count = 0
while True:
try:
r = requests.get("http://0.0.0.0:8100/", timeout=2)
except requests.exceptions.ReadTimeout:
count += 1
print(f"{count}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"fast_app:app",
host="0.0.0.0",
port=8100,
reload=False,
debug=False,
)
-r requirements.txt
black==20.8b1
coverage
pydevd-pycharm~=213.5744.248
pdbpp
fastapi~=0.75.2
pytest==6.2.5
pytest-aiohttp==0.3.0
aiohttp==3.8.1
aiomcache>=0.5.2, < 0.7.0
aiocache[memcached]==0.11.1
aioresponses~=0.7.2
hvac==0.11.2
pydantic==1.9.0
ddtrace==0.59.1
fnc~=0.5.3
mysqlclient==2.0.3
pycryptodome==3.14.1
python-json-logger==2.0.2
requests==2.27.1
okta==2.3.1
okta-jwt-verifier==0.2.3
hvac==0.11.2
sqlalchemy==1.4.26
starlette-context==0.3.3
uvicorn==0.17.3
tenacity~=8.0.1
datadog-api-client[async]==1.10.0
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 41, in call_next
message = await recv_stream.receive()
File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 101, in receive
raise EndOfStream
anyio.EndOfStream
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 364, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 261, in __call__
await super().__call__(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 63, in __call__
response = await self.dispatch_func(request, call_next)
File "/code/fast_app.py", line 21, in middleware
return await call_next(request)
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 45, in call_next
raise RuntimeError("No response returned.")
RuntimeError: No response returned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment