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

Conversation

florimondmanca
Copy link
Member

@florimondmanca florimondmanca commented Nov 8, 2021

Closes #1226

Completes #869 (released in 0.14.0) with a missing branch: it is possible that data be b"", e.g. if the client is a health checker that uses empty TCP pings to verify a service is up. As shown in #1226, this may be the case in k8s environments for example. In that case, we should be mindful of closing the transport, i.e. closing the connection (hence the connection/memory leak reported in #1226).

To reproduce before/after, consider this server:

async def app(scope, receive, send):
    if scope["type"] == "lifespan":
        await lifespan(scope, receive, send)
        return

    assert scope["type"] == "http"
    await http(scope, receive, send)
    await send(
        {
            "type": "http.response.start",
            "status": 200,
            "headers": [[b"content-type", b"text/plain"]],
        }
    )

And this "ping as fast as you can" client:

import socket

while True:
    with socket.create_connection(("localhost", 8000)) as sock:
        pass
  1. Fire up a resource monitor, e.g. Activity Monitor on macOS.
  2. Launch the server.
  3. Lookup the Python process activity.
  4. Launch the client.

Before the change, the Python process memory would go up indefinitely, at a pace that varies from system to system. Eg I start from 16MB and get to >20MB and up in just a few seconds.

After the change, memory remains stable. Eg I see a stable 17.1 MB despite the client hitting the server with pings.

Kludex
Kludex previously approved these changes Nov 8, 2021
@florimondmanca
Copy link
Member Author

Seems like there's more subtlety to it than I thought.

@@ -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

@@ -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

Comment on lines +226 to +229
elif event_type is h11.ConnectionClosed:
if not self.transport.is_closing():
self.transport.close()
break
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

@Kludex
Copy link
Sponsor Member

Kludex commented Jan 28, 2022

Replaced by #1332

@Kludex Kludex closed this Jan 28, 2022
@tomchristie tomchristie deleted the fm/close-conn-tcp-pings branch January 28, 2022 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using UvicornWorkers in Gunicorn cause OOM on K8s
3 participants