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

Ensure HTTP connections are closed in case of data-less TCP pings #1244

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions uvicorn/_handlers/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def retrieve_exception(task: asyncio.Task) -> None:
# yet: all data that the client might have already sent since the connection has
# been established is in the `_buffer`.
data = reader._buffer # type: ignore
if data:
protocol.data_received(data)
protocol.data_received(data)
Copy link
Member

@euri10 euri10 Nov 20, 2021

Choose a reason for hiding this comment

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

Suggested change
protocol.data_received(data)
if isinstance(protocol, H11Protocol):
if data:
protocol.data_received(data)
elif isinstance(protocol, HttpToolsProtocol):
protocol.data_received(data)

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we should be moving that if data: check to H11Protocol instead, then.

Copy link
Member

Choose a reason for hiding this comment

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

the thing is that this b"" case is already handled by h11 in it's receive_data we use in our protocol's data_received

see https://gitlab.com/kalilinux/packages/python-h11/-/blob/kali/master/h11/_connection.py#L303-348

so I dont know


# Let the transport run in the background. When closed, this future will complete
# and we'll exit here.
Expand Down
5 changes: 5 additions & 0 deletions uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ def handle_events(self):
self.cycle.more_body = False
self.cycle.message_event.set()

elif event_type is h11.ConnectionClosed:
if not self.transport.is_closing():
self.transport.close()
break
Comment on lines +226 to +229
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
elif event_type is h11.ConnectionClosed:
if not self.transport.is_closing():
self.transport.close()
break


def handle_upgrade(self, event):
upgrade_value = None
for name, value in self.headers:
Expand Down
3 changes: 3 additions & 0 deletions uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def data_received(self, data):
except httptools.HttpParserUpgrade:
self.handle_upgrade()

if data == b"" and not self.transport.is_closing():
self.transport.close()

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if data == b"":
self.on_response_complete()

Copy link
Member

Choose a reason for hiding this comment

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

seems like this pass tests and fix the issue if I'm correct

def handle_upgrade(self):
upgrade_value = None
for name, value in self.headers:
Expand Down