-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Connection terminated unexpectedly #1611
Comments
I guess you're not waiting for db to connect and making requests to early. |
Using native mode fixed problem for client query
|
@JohnsiJohn oh, that's interesting! Thank you for that discover, I will try to use it for sequelize |
Although I'm not using pools, for the benefit of anyone else here from Google I had this problem when the connection sat idle for a while and was disconnected. The problem is that the exception that is thrown as a result of this unexpected disconnection cannot be caught and causes Node to terminate. I fixed the problem by adding an error handler so that the exception was never thrown in the first place:
I then just make sure the |
I've just had a hell of a time debugging this from the NestJS TypeORM package, and came to the same conclusion, pg-native skirts around this problem entirely |
@Upperfoot and how did you solve this setup in NestJS TypeORM? |
Hey @tocosastalo I just installed the pg-native npm library which solved it, it's automatically used if detected, and to be honest it's comparatively faster than the normal pg npm library anyway |
@Upperfoot Thanks for the quick reply. So I added
Didn't you encounter this error? |
No, I didn't get that error, looks like the connection attempt was blocked, can you check connectivity via https://www.postgresql.org/docs/9.3/app-pg-isready.html or something similar to narrow the issue down a bit more? |
@Upperfoot |
node pg node event emitter docs state:
So if you don't want your node process to exit - register an error handler. eg: |
Try using |
I've been having this error when using
This is a dumb Fastify project I made to reproduce the error: https://github.com/PierBover/fly-postgres-node-test/blob/main/index.js When using |
@PierBover |
@charmander but the docs say:
https://node-postgres.com/features/pooling If this is wrong, why is it in the docs? I will try what you suggest and report back. BTW I also tried using the undocumented Edit: Oh I think I get it now. I can still use the pool to query but I don't need to use Edit 2: You were 100% right @charmander . Now I just need to figure out how to have a persistent client with reconnect for listen/notify. |
For listen/notify connections I use this pattern: function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function *backoff() {
let sleep = initialDelay;
do {
yield sleep;
sleep = Math.min(maxDelay, randomBetween(initialDelay, sleep * 2));
} while (true)
}
async function reconnect() {
if (client) {
cleanupSomehow(client);
}
for (const nextDelay of backoff()) {
try {
client = await connectSomehow();
} catch {
await new Promise(res => setTimeout(res, nextDelay));
}
}
client.on('error', reconnect);
client.connection.stream.setTimeout(heartbeatDealy);
client.connection.stream.on('timeout', async function () {
const timeoutId = setTimeout(reconnect, heartbeatTimeout);
try {
await client.query('SELECT 1');
} catch (err) {
reconnect();
} finally {
clearTimeout(timeoutId);
}
});
setupListenNotify(client);
} This executes a heartbeat query, after a set amount of inactivity on the connection. If the query times out or fails for whatever reason, I create and setup a new connection. You could use the |
This fixes various issues with node-postgres, specifically that the connection pooling implementation does not work well: brianc/node-postgres#1611 brianc/node-postgres#2112
This fixes various issues with node-postgres, specifically that the connection pooling implementation does not work well: brianc/node-postgres#1611 brianc/node-postgres#2112 --------- Co-authored-by: Ivan Chub <ichub@users.noreply.github.com>
This fixes various issues with node-postgres, specifically that the connection pooling implementation does not work well: brianc/node-postgres#1611 brianc/node-postgres#2112 --------- Co-authored-by: Ivan Chub <ichub@users.noreply.github.com>
Im trying to connecto to remote database.
I can connect using psql client.
But i get "Connection terminated unexpectedly" error while trying to run this (with same connection string as in psql clinet):
ive been trying to connect within sequelize ORM, but got same error.
The text was updated successfully, but these errors were encountered: