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 6 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/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ async def read(

return data

def is_connection_dropped(self) -> bool:
return self.stream_reader.at_eof()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should clarify this seemingly simplistic implementation. The StreamReaderProtocol.connection_lost callback calls stream_reader.feed_eof which in turn sets the EOF flag which at_eof returns.



class Writer(BaseWriter):
def __init__(self, stream_writer: asyncio.StreamWriter, timeout: TimeoutConfig):
Expand Down
4 changes: 4 additions & 0 deletions httpx/dispatch/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ async def _send_event(self, event: H11Event, timeout: TimeoutConfig = None) -> N
Send a single `h11` event to the network, waiting for the data to
drain before returning.
"""
if self.reader.is_connection_dropped():
raise NotConnected
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the connection status here also prevents us from sending the request event which, as @tomchristie mentioned, makes things ambiguous for non-idempotent requests.

bytes_to_send = self.h11_state.send(event)
await self.writer.write(bytes_to_send, timeout)

Expand Down Expand Up @@ -157,6 +159,8 @@ async def _receive_event(self, timeout: TimeoutConfig = None) -> H11Event:
"""
Read a single `h11` event, reading more data from the network if needed.
"""
if self.reader.is_connection_dropped():
raise NotConnected
while True:
event = self.h11_state.next_event()
if event is h11.NEED_DATA:
Expand Down
3 changes: 3 additions & 0 deletions httpx/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ async def read(
) -> bytes:
raise NotImplementedError() # pragma: no cover

def is_connection_dropped(self) -> bool:
raise NotImplementedError() # pragma: no cover


class BaseWriter:
"""
Expand Down
32 changes: 32 additions & 0 deletions tests/dispatch/test_connection_pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,35 @@ 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


@pytest.mark.asyncio
async def test_connection_closed_by_server_before_receiving_data(server):
"""
If the server closes the connection after sending the response but before reading the data
a disconnect error is raised.
"""
async with httpx.ConnectionPool() as http:
response = await http.request("GET", "http://127.0.0.1:8000/")
await server.shutdown() # shutdown the server to close the connection before receiving the data

with pytest.raises(httpx.exceptions.NotConnected):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is NotConnected the right exception here? Maybe we should wrap it into a more descriptive Exception and error message?

await response.read()
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