-
Notifications
You must be signed in to change notification settings - Fork 141
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
postgres: forcibly terminate connections when dropped #237
Conversation
This commit makes it so that dropping a `deadpool_postgres::Client` forcibly terminates the PostgreSQL connection, even if there is outstanding work on the connection (e.g., a long running query). Previously, the background connection task would continue to run until the outstanding work completed. This could cause the memory/TCP socket associated with the connection to effectively leak until that outstanding work completed. In the case of a runaway query (imagine, for example, `SELECT pg_sleep(100000000)`), that leak could potentially last forever.
That's very similar to the issue that the
Regarding this PR: Dropping the connection after every use is even worse than not using a pool at all. The returned object is definitely unusable and you're just paying the cost of having all the pool logic which never recycles any object returned to it. I don't know if there is a way (with The cancel operation is described as "Attempts to cancel the in-progress query on the connection associated with this CancelToken." which makes me wonder what happens if there are multiple queued queries. There is currently no way to clear the message queue in The next steps would be:
If all that is necessary I'm tempted to add a return task for taking care of such things. That would also allow for some ahead of time recycling of objects which has been a long standing issue: |
I'm sorry, I don't follow! What you describe is definitely not the intent of this PR. And if it works that way, it's a bug in the change as submitted. The idea was to forcibly terminate the connection only in the case that you drop the |
Gotcha. Your PR description differs from what the code actually does. After being primed by your description I read the code wrong and got confused by
After reading your second message and reviewing the code for a second time it all makes a lot more sense now. 👍 I do wonder however if that feature of closing the connection when the client (by that I mean Don't get me wrong. It sounds like the proper behavior. It's just that I'm unsure if @sfackler Sorry for pinging you twice today. What do you think of adding this feature to |
Oh goodness, sorry about that! It was late last night and I said
I think tokio_postgres is unlikely to change, since the current API allows the user to choose whether they want graceful termination ( What do you think about exposing a |
It could be a feature that improves the quality of life for developers but as you just stated it is left to the user of the library. While it would be nice if the connection was closed upon dropping the client, it is somewhat obvious by the way In regards to deadpool this is not the case and the connection is spawned for you. So it's fair that the pool provides a configuration option to control this behavior. 👍 I'm torn between just adding your change and adding a configuration option for it. |
Either would be entirely okay from our perspective! For our use case, we just need some way to force-abort connections, whether it be manual or on drop. |
It took a bit of convincing but you're totally right. I really want to get Unless you're in a hurry I'd like to wait a few days for the proper |
We switched to our own fork temporarily, so this issue isn't impacting us in prod. In other words, there's no rush and we're happy to wait for the release. Thanks for the help! |
Seconding @umanwizard—thanks so much for fielding this PR and our concerns! Really appreciate all the thought you've put into deadpool's design. |
Hey @bikeshedder! Thanks for the awesome library—we've been using deadpool-postgres to great effect in @MaterializeInc for a while now. But we recently got bit by the issue described below, where dropping a
deadpool_postgres::ClientWrapper
does not actually force the connection to terminate.We think this is enough of a footgun that it's worth changing the default behavior to forcibly terminate the connection, which is what this PR does. We'd be happy to rework this to make this a configurable option of the pool/client, if preferable—@umanwizard and I just couldn't come up with a good use case for when you'd want to opt out of this behavior, so we went with the simplest possible solution to start. What do you think?
This commit makes it so that dropping a
deadpool_postgres::ClientWrapper
forcibly terminates the PostgreSQL connection, even if there is outstanding work on the connection (e.g., a long running query).Previously, the background connection task would continue to run until the outstanding work completed. This could cause the memory/TCP socket associated with the connection to effectively leak until that outstanding work completed. In the case of a runaway query (imagine, for example,
SELECT pg_sleep(100000000)
), that leak could potentially last forever.