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

Memory leak in long-lived connection pool #1081

Open
seanlinsley opened this issue Nov 13, 2023 · 9 comments
Open

Memory leak in long-lived connection pool #1081

seanlinsley opened this issue Nov 13, 2023 · 9 comments

Comments

@seanlinsley
Copy link

seanlinsley commented Nov 13, 2023

In the image below, memory usage is stable with prune_db_connections as described in bikeshedder/deadpool#273, plus malloc_trim(0). Then disabling prune_db_connections shows a clear memory leak.

Screenshot 2023-11-11 at 7 46 29 PM

Heaptrack shows two leaks, both around BytesMut usage:

Screenshot 2023-11-12 at 4 48 13 PM Screenshot 2023-11-12 at 5 12 03 PM

Related discussions online:

Possible debugging steps:

  • add code to track every message sent/received, where the buffer size has grown since the last message (might require a fork of the bytes crate)
  • write a reproduction script that's similar to the production workload (RDS, binary COPY FROM, queries that cause unique constraint error)

Possible fixes:

  • revert ffd7245
    • this would likely fix the leak if it's actually a bug in BytesMut usage and not something like closed connections never being freed. and it'd be a good opportunity to confirm/deny the performance difference that motivated the change originally
@sfackler
Copy link
Owner

Do you have a self contained reproduction?

@seanlinsley
Copy link
Author

seanlinsley commented Nov 13, 2023

I've tried to write a reproduction script, but it doesn't leak memory. If there is a bug in BytesMut usage it might be around the exact handling of messages sent/received to a remote database, using both regular queries (with heavy use of array binds) and binary COPY FROM, and with some queries resulting in unique constraint errors.

@inzanez
Copy link

inzanez commented Jun 3, 2024

@seanlinsley is there any update on this? I am seeing similar things (queries with many parameters) increasing memory usage quite heavily.
Is it already clear where that is coming from?

@seanlinsley
Copy link
Author

We haven't been able to reproduce this outside of our production environment, even after building a load tester with similar queries. For now it's been sufficient to prune old connections as described in bikeshedder/deadpool#273

@inzanez
Copy link

inzanez commented Jun 3, 2024

Ok,...well, I am trying to reproduce this right now in a minimal example. Hopefully I will manage,...

@inzanez
Copy link

inzanez commented Jun 4, 2024

I assume you are using the deadpool / pgsql combination in a webserver, correct? If so, what is the underlying web framework?

@inzanez
Copy link

inzanez commented Jun 5, 2024

@seanlinsley can you give some details about your production environment? Are you using a web framework? Could it be that the executable is running in a container environment like Kubernetes, Openshift etc.? Does that container environment enforce memory limits?...

@inzanez
Copy link

inzanez commented Jun 10, 2024

@sfackler my tests suggest that frequently used connections have ~100MByte of memory reserved at rest. So they pretty much never free that memory. Having a connection pool of 25 clients could take up ~2.5GByte of memory which seems to be quite a lot.
I have not yet managed to locate the exact region of where that memory allocation comes from though. I am still trying but maybe you already have a clue where I might have to look?...

@inzanez
Copy link

inzanez commented Jun 10, 2024

It seems that the memory is allocated mostly in the read- and write-buffer of the

pub struct Connection<S, T> {
    stream: Framed<MaybeTlsStream<S, T>, PostgresCode
    ...
```,


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants