Intermittent httpcore.RemoteProtocolError: Server disconnected without sending a response. #2056
Replies: 7 comments 15 replies
-
I'm having the same issue with about 1 in 10 requests.. |
Beta Was this translation helpful? Give feedback.
-
@jogu - Could you start by updating For example looking at this line in the traceback...
I can see that doesn't match up with the latest codebase... Line 228 is here... Whereas the |
Beta Was this translation helpful? Give feedback.
-
@tomchristie I've made a fairly minimal reproduction: import httpx
urls = """\
https://archive.org/download/cu31924003160540/page/n0_x0_y0_w1283_h2099_s2.jpg
https://archive.org/download/cu31924003160540/page/n1_x532_y904_w220_h222_s1.jpg
https://archive.org/download/cu31924003160540/page/n2_x404_y1804_w326_h92_s1.jpg
https://archive.org/download/cu31924003160540/page/n3_x130_y270_w1024_h368_s2.jpg
https://archive.org/download/cu31924003160540/page/n7_x54_y214_w1184_h1342_s2.jpg
https://archive.org/download/cu31924003160540/page/n39_x174_y450_w902_h904_s2.jpg
https://archive.org/download/cu31924003160540/page/n50_x172_y452_w958_h968_s2.jpg
https://archive.org/download/cu31924003160540/page/n55_x210_y424_w450_h214_s1.jpg
https://archive.org/download/cu31924003160540/page/n55_x572_y628_w526_h238_s2.jpg
https://archive.org/download/cu31924003160540/page/n56_x220_y488_w872_h856_s2.jpg
https://archive.org/download/cu31924003160540/page/n58_x232_y730_w844_h344_s2.jpg
https://archive.org/download/cu31924003160540/page/n59_x168_y740_w966_h390_s2.jpg
https://archive.org/download/cu31924003160540/page/n60_x176_y868_w994_h458_s2.jpg
https://archive.org/download/cu31924003160540/page/n66_x236_y682_w802_h252_s2.jpg
https://archive.org/download/cu31924003160540/page/n80_x368_y672_w618_h528_s2.jpg
https://archive.org/download/cu31924003160540/page/n88_x152_y292_w1020_h1298_s2.jpg
https://archive.org/download/cu31924003160540/page/n92_x304_y116_w708_h1702_s2.jpg
https://archive.org/download/cu31924003160540/page/n98_x870_y412_w284_h958_s1.jpg
https://archive.org/download/cu31924003160540/page/n103_x260_y570_w776_h814_s2.jpg
https://archive.org/download/cu31924003160540/page/n119_x210_y592_w884_h536_s2.jpg
https://archive.org/download/cu31924003160540/page/n120_x562_y966_w586_h604_s2.jpg
https://archive.org/download/cu31924003160540/page/n147_x140_y342_w894_h1266_s2.jpg
https://archive.org/download/cu31924003160540/page/n152_x504_y498_w646_h586_s2.jpg
""".split("\n")
async def get_image(url, client):
print('fetching', url)
response = await client.get(url, follow_redirects=True)
print(repr(response))
async def gather_with_concurrency(n, *tasks):
semaphore = asyncio.Semaphore(n)
async def sem_task(task):
async with semaphore:
return await task
return await asyncio.gather(*(sem_task(task) for task in tasks))
async def main():
client = httpx.AsyncClient(timeout=20.0)
await gather_with_concurrency(2, *[get_image(url, client) for url in urls])
if __name__ == "__main__":
import asyncio
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same issue (I think it's the same). It happens when talking to a Hypercorn ASGI server over HTTP/2. It doesn't happen always but very regularly. A few requests is all that's needed to reproduce it. This is the traceback: Traceback (most recent call last):
File ".../lib/python3.9/site-packages/httpx/_transports/default.py", line 66, in map_httpcore_exceptions
yield
File ".../lib/python3.9/site-packages/httpx/_transports/default.py", line 366, in handle_async_request
resp = await self._pool.handle_async_request(req)
File ".../lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 268, in handle_async_request
raise exc
File ".../lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 251, in handle_async_request
response = await connection.handle_async_request(request)
File ".../lib/python3.9/site-packages/httpcore/_async/connection.py", line 103, in handle_async_request
return await self._connection.handle_async_request(request)
File ".../lib/python3.9/site-packages/httpcore/_async/http2.py", line 185, in handle_async_request
raise exc
File ".../lib/python3.9/site-packages/httpcore/_async/http2.py", line 148, in handle_async_request
status, headers = await self._receive_response(
File ".../lib/python3.9/site-packages/httpcore/_async/http2.py", line 292, in _receive_response
event = await self._receive_stream_event(request, stream_id)
File ".../lib/python3.9/site-packages/httpcore/_async/http2.py", line 333, in _receive_stream_event
await self._receive_events(request, stream_id)
File ".../lib/python3.9/site-packages/httpcore/_async/http2.py", line 361, in _receive_events
events = await self._read_incoming_data(request)
File ".../lib/python3.9/site-packages/httpcore/_async/http2.py", line 452, in _read_incoming_data
raise exc
File ".../lib/python3.9/site-packages/httpcore/_async/http2.py", line 440, in _read_incoming_data
raise RemoteProtocolError("Server disconnected")
httpcore.RemoteProtocolError: Server disconnected
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
...
File ".../our-code.py", line 191, in make_stream_request
async with client.stream(
File "/home/jonathan/.pyenv/versions/3.9.16/lib/python3.9/contextlib.py", line 181, in __aenter__
return await self.gen.__anext__()
File ".../lib/python3.9/site-packages/httpx/_client.py", line 1573, in stream
response = await self.send(
File ".../lib/python3.9/site-packages/httpx/_client.py", line 1617, in send
response = await self._send_handling_auth(
File ".../lib/python3.9/site-packages/httpx/_client.py", line 1645, in _send_handling_auth
response = await self._send_handling_redirects(
File ".../lib/python3.9/site-packages/httpx/_client.py", line 1682, in _send_handling_redirects
response = await self._send_single_request(request)
File ".../lib/python3.9/site-packages/httpx/_client.py", line 1719, in _send_single_request
response = await transport.handle_async_request(request)
File ".../lib/python3.9/site-packages/httpx/_transports/default.py", line 366, in handle_async_request
resp = await self._pool.handle_async_request(req)
File "/home/jonathan/.pyenv/versions/3.9.16/lib/python3.9/contextlib.py", line 137, in __exit__
self.gen.throw(typ, value, traceback)
File ".../lib/python3.9/site-packages/httpx/_transports/default.py", line 83, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.RemoteProtocolError: Server disconnected The client is created like this: async with httpx.AsyncClient(
verify=ssl_context,
timeout=10.0,
trust_env=False,
proxies=None,
http2=True,
limits=httpx.Limits(
max_connections=100,
max_keepalive_connections=20,
keepalive_expiry=None,
),
) as client: ... edit: I think, but really not sure at all, that it happens after a timeout, possibly faster with |
Beta Was this translation helpful? Give feedback.
-
Hi guys, I had the same issue, but, I found some things related about this:
I was use tenacity for retry and sometimes fixed the problem, but it come back likewise 🙃 |
Beta Was this translation helpful? Give feedback.
-
This issue in http2 still exits. We saw this issue after running a load of 20 request per second for 10 minutes and we are kind of reproduce it where we are seeing this issue after certain number of request are completed. ERROR: Exception in ASGI application
Traceback (most recent call last):
File ".../lib/python3.12/site-packages/httpx/_transports/default.py", line 101, in map_httpcore_exceptions
yield
File ".../lib/python3.12/site-packages/httpx/_transports/default.py", line 127, in __iter__
for part in self._httpcore_stream:
^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpcore/_sync/connection_pool.py", line 407, in __iter__
raise exc from None
File ".../lib/python3.12/site-packages/httpcore/_sync/connection_pool.py", line 403, in __iter__
for part in self._stream:
^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpcore/_sync/http2.py", line 585, in __iter__
raise exc
File ".../lib/python3.12/site-packages/httpcore/_sync/http2.py", line 575, in __iter__
for chunk in self._connection._receive_response_body(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpcore/_sync/http2.py", line 316, in _receive_response_body
event = self._receive_stream_event(request, stream_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpcore/_sync/http2.py", line 336, in _receive_stream_event
self._receive_events(request, stream_id)
File ".../lib/python3.12/site-packages/httpcore/_sync/http2.py", line 355, in _receive_events
raise RemoteProtocolError(self._connection_terminated)
httpcore.RemoteProtocolError: <ConnectionTerminated error_code:0, last_stream_id:19999, additional_data:None>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ".../lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File ".../lib/python3.12/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File ".../lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in __call__
raise exc
File ".../lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in __call__
await self.app(scope, receive, _send)
File ".../lib/python3.12/site-packages/starlette/middleware/base.py", line 176, in __call__
with recv_stream, send_stream, collapse_excgroups():
^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py", line 158, in __exit__
self.gen.throw(value)
File ".../lib/python3.12/site-packages/starlette/_utils.py", line 82, in collapse_excgroups
raise exc
File ".../lib/python3.12/site-packages/starlette/middleware/base.py", line 178, in __call__
response = await self.dispatch_func(request, call_next)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../main.py", line 64, in profile_request
response = await call_next(request)
^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/starlette/middleware/base.py", line 156, in call_next
raise app_exc
File ".../lib/python3.12/site-packages/starlette/middleware/base.py", line 141, in coro
await self.app(scope, receive_or_disconnect, send_no_error)
File ".../lib/python3.12/site-packages/opentelemetry/instrumentation/asgi/__init__.py", line 743, in __call__
await self.app(scope, otel_receive, otel_send)
File ".../lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File ".../lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File ".../lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File ".../lib/python3.12/site-packages/starlette/routing.py", line 714, in __call__
await self.middleware_stack(scope, receive, send)
File ".../lib/python3.12/site-packages/starlette/routing.py", line 734, in app
await route.handle(scope, receive, send)
File ".../lib/python3.12/site-packages/starlette/routing.py", line 288, in handle
await self.app(scope, receive, send)
File ".../lib/python3.12/site-packages/starlette/routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File ".../lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File ".../lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File ".../lib/python3.12/site-packages/starlette/routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/fastapi/routing.py", line 301, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/fastapi/routing.py", line 214, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/starlette/concurrency.py", line 37, in run_in_threadpool
return await anyio.to_thread.run_sync(func)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/anyio/to_thread.py", line 56, in run_sync
return await get_async_backend().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2470, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 967, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File ".../routes/authorize.py", line 33, in authorize
response: AuthzResponse = client.authorize(request=authz_request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../client/client.py", line 194, in authorize
response = self.http_sync_client.post(url=self.authorize_url, payload=payload, headers=headers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../transport/sync_http_session_manager.py", line 172, in post
response = self._session.post(url, content=b_payload, headers=headers, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpx/_client.py", line 1144, in post
return self.request(
^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpx/_client.py", line 825, in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpx/_client.py", line 928, in send
raise exc
File ".../lib/python3.12/site-packages/httpx/_client.py", line 922, in send
response.read()
File ".../lib/python3.12/site-packages/httpx/_models.py", line 881, in read
self._content = b"".join(self.iter_bytes())
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpx/_models.py", line 897, in iter_bytes
for raw_bytes in self.iter_raw():
^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpx/_models.py", line 951, in iter_raw
for raw_stream_bytes in self.stream:
^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpx/_client.py", line 153, in __iter__
for chunk in self._stream:
^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/httpx/_transports/default.py", line 126, in __iter__
with map_httpcore_exceptions():
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py", line 158, in __exit__
self.gen.throw(value)
File ".../lib/python3.12/site-packages/httpx/_transports/default.py", line 118, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.RemoteProtocolError: <ConnectionTerminated error_code:0, last_stream_id:19999, additional_data:None> |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing the same issue. Worth noting that keep-alive is not mandatory in TCP implementations and used to be frowned upon because it wastes bandwidth. When implemented, the keep-alive interval would be a couple of hours (although I did see 15 minutes being used as well), so it's not likely something that occurs within seconds or even a couple of minutes, as I seem to be observing in my case for these errors. Also, servers may disconnect in a couple of ways. For a graceful disconnect, they will send a FIN packet, so the client will find the socket readable, but with zero data, which means the connection was gracefully closed. Some less-friendly implementations may use RST packets,, which typically come through as errors. I don't have experience with Python's HTTP libraries, but at the socket level this is handled by detecting socket readability via file descriptor sets and a subsequent zero-length read, so if Python exposes this behavior in some way, it could be used to detect closed connections before sending more data. RST packets work similarly, except that they maybe handled via readability and Lastly, worth noting that HTTP/3 works over QUIC/UDP, so none of the above applies. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
I've been trying to switch from requests to httpx for all kinds of reasons, but I'm getting a weird and kind of reproducible problem.
About 1 in 5000 or so requests I'll get:
httpcore.RemoteProtocolError: Server disconnected without sending a response.
I'm running the latest httpx on the latest python:3.10-alpine3.15 docker container.
"By kind of reproducible" I mean "generally happens upto 2 or 3 times inside a 25 minute job in our CI".
This happens with both the normal and async client.
The client is talking to apache (as a reverse proxy for a java spring app) on a Debian container, and I've tried the apaches across 3 major versions of Debian (stretch, buster, bullseye) and the problem happens on all 3.
I've had some success adding a custom transport that retries in this case, though I'd prefer to try and fix the root cause if possible as I don't think this should happen.
I can't figure out how to diagnose this further. I tried HTTPX_LOG_LEVEL=trace, but as noted in other discussions that's not really doing much currently.
The backtrace looks like this:
Beta Was this translation helpful? Give feedback.
All reactions