-
-
Notifications
You must be signed in to change notification settings - Fork 857
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
Changes from 6 commits
9bdb8fd
84ad457
171eeee
0e2b5ce
71430ed
5ec83c2
d26177f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
@@ -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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
await response.read() |
There was a problem hiding this comment.
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 callsstream_reader.feed_eof
which in turn sets the EOF flag whichat_eof
returns.