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

Detect EOF signaling remote server closed connection #143

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions httpx/dispatch/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ async def _receive_event(self, timeout: TimeoutConfig = None) -> H11Event:
)
except OSError: # pragma: nocover
data = b""
if self.h11_state.their_state == h11.SEND_RESPONSE and data == b"":
yeraydiazdiaz marked this conversation as resolved.
Show resolved Hide resolved
# the server closed a keep-alive connection
raise NotConnected
self.h11_state.receive_data(data)
else:
break
Expand Down
18 changes: 18 additions & 0 deletions tests/dispatch/test_connection_pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,21 @@ async def test_premature_response_close(server):
await response.close()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 0


@pytest.mark.asyncio
async def test_keepalive_connection_closed_by_server_is_reestablished(server):
"""
Upon keep-alive connection closed by remote a new connection should be reestablished.
"""
async with httpx.ConnectionPool() as http:
response = await http.request("GET", "http://127.0.0.1:8000/")
await response.read()

await server.shutdown() # shutdown the server to close the keep-alive connection
await server.startup()

response = await http.request("GET", "http://127.0.0.1:8000/")
await response.read()
assert len(http.active_connections) == 0
assert len(http.keepalive_connections) == 1
2 changes: 1 addition & 1 deletion tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
CertTypes,
Client,
Dispatcher,
multipart,
Request,
Response,
TimeoutTypes,
VerifyTypes,
multipart,
)


Expand Down