-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Pool leaking connections #833
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
Comments
Only appears to be a bug in 2.3.0 and 2.3.1, it works as expected in 2.2.0. |
Same goes for master SHA: bce163c |
Wow. Thanks for the report! I'll get a fix out ASAP. When a connection is marked to purge, we should go ahead and close the connection when you go to return it to the pool instead of leaking it. |
Thanks. As an aside why would the connection be marked to purge in this scenario? Unless I've misunderstood the docs that should only happen when I call destroy, right? (or end in a future version when the old end behaviour is fully deprecated). Or are you saying using changeUser somehow prevent reuse of a connection? |
It is because if you, say, have a pool where all the connections are user Another reason is that when you use |
That makes a lot of sense for that use case. I wonder, would you accept a PR to make that behaviour configurable? I have an app where I have a small number of servers which each has a large number of databases each for which I have unique credentials. I'd like to be able to have 1 pool per server and be able to grab a connection in an express middleware and always immediately change the user and db on the connection before passing it onto the next middleware/route handler. |
Perhaps you should be using the PoolCluster? |
If you didn't know, the |
Yes, it can change per request depending on which group the user belongs to and which "context" they are currently viewing. A db host is multi-tenancy but our apps users can have access to multiple dbs. We'd like to keep using the mysql users to enforce restrictions on users per db because of the multi-tenanted nature of it. Previously we were managing a pool of pools whereby we kept 1 pool per host/db/user combo, but the drawback to that was the vast number of connections that would be kept open on a single db host. Add multiple instances of an app server (horizontal scaling) to the mix and suddenly the db has a huge number of open connections. If I've understood correctly the PoolCluster would have the same impact. I guess we can always write an alternative pool manager to do it :) |
Right. So I was asking because it doesn't really sound like you are using the pooling anyhow. The ejection will (mostly) just work fine with what you are trying to achieve. I assume you are only even using the pool (instead of just |
Sorry I didn't address this earlier. So, ejecting the connections after changeUser was really a quick-and-dirty solution to keep the pool's connections cleaner. What I would love to have (so PR maybe?? :D) would be such that when you return a connection that has changeUser called on it, the pool will change the user back to what the pool is configured to and call the appropriate events so state can be set up again. |
But if you say that it's setting up a new connection every time you switch I had assumed that it was using this I need to go out but I'll pick this up again in the morning and actually Thanks for the quick responses and fix for the bug, much appreciated.
|
That is correct. By "new connection" I meant the negotiation on the MySQL server and the connection session state there, rather than the raw TCP connection.
That's fine ;) Like I said, the purging of the connections that had chnageUser done on them was a quick-and-dirty solution to solve a problem with the pool. The real solution would be to change the user back when needed, of course :) I just haven't had the time to implement that, but PRs are always welcome!
No problem :) |
So I just submitted a PR for the I've also been thinking more about my use case and I'm thinking that whilst the PR would be an improvement (in so much that we're not re-establishing the TCP connection) there's still a ways to go. I was thinking on modifying the pool so during If you didn't specify credentials to the But that seems like a fair bit of change in Pool and PoolConnection. Would you take a PR for something like that, or would I be better writing a new Pool implementation as a new module? Thoughts welcome. |
Maybe. Because MySQL allows for changing users and such, it may indeed be useful. Just because the pool is simply a pool of opaque connections now, doesn't mean it has to be :) Basically what you are suggesting is that the pool should pool on the properties that are fundamental to the connection itself (host, port, SSL, etc.) but would allow you to acquire connections based on the session of that connection (user, database, charset, etc.). |
Yep, pretty much. I think it should work for more use cases, albeit a slightly more complicated implementation. |
I need to code up a minimal test case but I'm finding that the connection pool leaks connections that are released if
changeUser
has been called on it during the lifetime of the connection.When release is called on the connection it appears that it gets removed from the pool in so much that it's no longer in
pool._allConnections
orpool._freeConnections
. But the connection must live on somewhere because the tcp connection stays open. I'm using the crudenetstat -lanp | grep 3306 | wc -l
to count the connections, and the number grows by the number of connections got from the pool.If using the connection without calling
changeUser
then it returns the connection to the pool as expected.The text was updated successfully, but these errors were encountered: