-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Connection terminated unexpectedly does not propagate to the active query #1700
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
If there is no “error” listener defined, and an “error” event is emitted, the error is thrown and the node process exists. This behaviour is documented in Node.js EventEmitter https://nodejs.org/api/events.html#events_error_events In the context of node-postgres, this means that even if there is an active query to associate an error with, when there is no “error” listener defined the program will exit when there is a connection error. Refer to: brianc/node-postgres#1700
It is, because it causes the query to fail. It’s just also emitted on the client, because it’s a client-level error (requires you to connect a new client if not using a pool, for example).
Connections being terminated by PostgreSQL, e.g. when shutting down or causing a client’s backend process to exit: 'use strict';
const pg = require('pg');
(async () => {
const pool = new pg.Pool({
host: '/var/run/postgresql',
});
const client = await pool.connect();
pool.on('error', error => {
console.error('error event on pool (%s)', error);
});
client.on('end', () => {
console.log('end event on client');
pool.end();
});
await pool.query('SELECT pg_terminate_backend($1)', [client.processID]);
})();
process.on('unhandledRejection', error => {
throw error;
}); |
Problem is that unless client defines "error" event handler, then the Node process is being terminated prior to the query error handler doing clean up. |
Always use a pool, always add an |
That makes sense. It would also make sense then to throw an error if client attempts to create a query without |
Referring to this code:
https://github.com/brianc/node-postgres/blob/master/lib/client.js#L179-L188
First of all, it is not clear how to replicate this error.
Under what circumstances will the
end
event be triggered without calling.end()
on the client?I have tried:
end then literally terminating the Internet connection, but that results in
ETIMEDOUT
rather than "Connection terminated unexpectedly".Point is, this is happening in production application.
I read the notes at https://node-postgres.com/api/pool:
Having read this, it is still unclear to me whats the reason this error cannot be propagated to the active query?
Perhaps I am overlooking something, but was the intention to structure the code as:
?
This fixes the issue ("Connection terminated" propagates to the query), though I am uncertain what are the side effects.
The text was updated successfully, but these errors were encountered: