question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Client error: Connection terminated unexpectedly when server stops in the middle of running queries

See original GitHub issue

Hello,

I’m using a connection pool. My program gets a client from the pool and releases it after the query.

   const client = await this.getClient();
   await client.query(statement);
   client.release();

I’m listening to all pool and client events. When my server stops the clients start to emit the following error event:

    Connection terminated unexpectedly
       at Connection.<anonymous> (/project/node_modules/pg/lib/client.js:272:71)
       at Object.onceWrapper (events.js:420:28)
       at Connection.emit (events.js:314:20)
       at Socket.<anonymous> (/project/node_modules/pg/lib/connection.js:102:10)
       at Socket.emit (events.js:326:22)
       at endReadableNT (_stream_readable.js:1226:12)
       at processTicksAndRejections (internal/process/task_queues.js:80:21)

The problem is, eventually I get an Unhandled ‘error’ event from clients. I already tried to use client.release(true) to destroy all the clients that emit an error event, tried to use pool.end() after a client sends an error event but nothing seems to work.

The only thing that works for me is calling client.release(true) after client.query(statement) every time even when there is no error, but there is no point in using a pool if I’m always destroying connections.

I’m using node-postgres 8.2.1.

Thanks in advance

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
charmandercommented, Jul 20, 2020

You still need the listener on the pool as well. Wherever you’re creating the pool,

pool.on('error', error => {
    console.error('idle client error:', error);
});

Also, off needs to be used with the original function passed to on, not just one with the same effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Client error: Connection terminated unexpectedly when server ...
Hello,. I'm using a connection pool. My program gets a client from the pool and releases it after the query. const client =...
Read more >
node-postgres, Connection terminated unexpectedly
I have tried this, problem is asynchronous property of JavaScript. Connection is being ended before execution of query.
Read more >
Understanding Heroku Postgres Log Statements and ...
This error can happen as a result of one of several intermittent network connectivity issues. If you are seeing this only intermittently, ...
Read more >
Troubleshoot connecting to your instance - AWS Documentation
Check your instance to make sure it is running and has passed its status checks. ... Error: Server unexpectedly closed network connection.
Read more >
Common Errors and Solutions | CockroachDB Docs
This message indicates a client is trying to connect to a node that is either not running or is not listening on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found