pg-ears is leaking Postgres connections after network reconnect
Opened this issue · 1 comments
I'm using pg-ears 1.2.1 with Postgres 11.6 (Node.js 12.14)
I have found out that pg-ears is leaking Postgres connections in situations described below:
-
Connect a Node.js application via pg-ears to a remote Postgres instance via
pgEars.listen(...
-
Run
SELECT * FROM pg_stat_activity WHERE query = 'LISTEN <<your channel name here>>;';
on your Postgres instance -> it should show only one entry for your application. -
Disconnect the network connection between the machine where your Node.js app runs on and the Postgres machine (I did it by simply plugging off the network cable, but there are more sophisticated ways to achieve this)
-
Wait till pg-ears noticed the connection loss (you should see some logs if you have included logging in your code)
-
Reconnect your network
-
Wait a few seconds and then run
SELECT * FROM pg_stat_activity WHERE query = 'LISTEN <<your channel name here>>;';
again -> there are now two connections for your application
If you have an unreliable network connection, you will shortly run out of Postgres connections.
Well, as a project author and maintainer the following answer is one that I never want to provide. However, I do feel it's the most appropriate solution.
I have made a few changes that may reduce the impact of this occurring. However, it is not a solution. While I do know how it could be corrected, that would require a few API changes that would cause implementing developers to change their existing code.
Frankly, if they are going to have to change their code to accommodate the changes which would correct the behavior they would be better served using pg-listen. It has more usage and as such is more actively developed. I am not using this module myself which is a poor formula for a project maintainer providing the best product.
I have added that warning to the README as well as a link to this issue which I will leave open. I'm sorry that is the answer, but I do think it's the best thing to do. If I were going to be using a listener module myself that is what I would use at this point.