Skip to content

Connection terminated unexpectedly does not propagate to the active query #1700

@gajus

Description

@gajus

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:

const connection = await pool.connect();

await connection.query('SELECT pg_sleep(10)');

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:

When a client is sitting idly in the pool it can still emit errors because it is connected to a live backend. If the backend goes down or a network partition is encountered all the idle, connected clients in your application will emit an error through the pool's error event emitter. The error listener is passed the error as the first argument and the client upon which the error occurred as the 2nd argument. The client will be automatically terminated and removed from the pool, it is only passed to the error handler in case you want to inspect it.

It is important you add an event listener to the pool to catch errors. Just like other event emitters, if a pool emits an error event and no listeners are added node will emit an uncaught error and potentially exit.

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:

if (this.activeQuery) {
      // ..
- }
- if (!this._ending) {
+ } else if (!this._ending) {

?

This fixes the issue ("Connection terminated" propagates to the query), though I am uncertain what are the side effects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions