node-postgres: Question: How to properly handle unexpected disconnection / autoreconnect ?
After have updated from 5.1 to 6.2.4, I see sometimes (nearly once a day) the following error which crashes our production server :
Error: Connection terminated unexpectedly
2017-06-15T10:03:33.683+02:00 at Object.onceWrapper (events.js:293:19)
2017-06-15T10:03:33.683+02:00Connection terminated unexpectedly
2017-06-15T10:03:33.684+02:00 at Socket.<anonymous> (/home/bas/app_21386476-a451-424c-ad67-870442bbdbe7/node_modules/pg/lib/connection.js:138:10)
2017-06-15T10:03:33.684+02:00 at emitNone (events.js:86:13)
2017-06-15T10:03:33.684+02:00 at emitNone (events.js:91:20)
2017-06-15T10:03:33.684+02:00 at Connection.emit (events.js:188:7)
2017-06-15T10:03:33.684+02:00 at Socket.emit (events.js:188:7)
2017-06-15T10:03:33.685+02:00 at process._tickCallback (internal/process/next_tick.js:104:9)
2017-06-15T10:03:33.685+02:00undefined
2017-06-15T10:03:33.685+02:00 at _combinedTickCallback (internal/process/next_tick.js:80:11)
2017-06-15T10:03:33.685+02:00 at endReadableNT (_stream_readable.js:975:12)
I’ve seen https://github.com/brianc/node-postgres/pull/1316
AFAIK, the error was not emitted before this patch, as of now, we have an error emitted on the pool.
I’m a bit confused here about what we should do when this error is happening ? Does it mean that we have to reconnect the entire pool ? But how ? Or does it means that the client emitting the error is definitively dead, so we rather drop the operation initiated by the client, and tell the user that his query is dropped. But once again how since the client.query callback seems not called with this error (perhaps somehow related to https://github.com/brianc/node-postgres/issues/1322) ?
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 3
- Comments: 22 (3 by maintainers)
Commits related to this issue
- Add onError config for pool To handle idle pool errors see https://github.com/brianc/node-postgres/issues/1324 — committed to Minishlink/knex by Minishlink 5 years ago
- Use pool query since this should catch idle errors https://github.com/brianc/node-postgres/issues/1324#issuecomment-623311914 Helps with https://github.com/ajayyy/SponsorBlockServer/issues/487 — committed to ajayyy/SponsorBlockServer by ajayyy 2 years ago
- fix: unhandled connection errors pg-pool requires attaching an error handler to each item checked out off the pool, which will handle connection-level errors. Without it, the application straight-up ... — committed to Quentin-M/loopback-connector-postgresql by Quentin-M 7 months ago
- fix: unhandled connection errors pg-pool requires attaching an error handler to each item checked out off the pool, which will handle connection-level errors. Without it, the application straight-up ... — committed to Quentin-M/loopback-connector-postgresql by Quentin-M 7 months ago
- fix: unhandled connection errors pg-pool requires attaching an error handler to each item checked out off the pool, which will handle connection-level errors. Without it, the application straight-up ... — committed to Quentin-M/loopback-connector-postgresql by Quentin-M 7 months ago
- pg-pool requires attaching an error handler to each item checked out off the pool, which will handle connection-level errors. Without it, the application straight-up crashes whenever the connection is... — committed to Quentin-M/loopback-connector-postgresql by Quentin-M 7 months ago
- pg-pool requires attaching an error handler to each item checked out off the pool, which will handle connection-level errors. Without it, the application straight-up crashes whenever the connection is... — committed to Quentin-M/loopback-connector-postgresql by Quentin-M 7 months ago
- fix: add missing connection error handler in pool, resolving process crash pg-pool requires attaching an error handler to each item checked out off the pool, which will handle connection-level errors... — committed to Quentin-M/loopback-connector-postgresql by Quentin-M 7 months ago
- fix: add missing connection error handler in pool, resolving process crash pg-pool requires attaching an error handler to each item checked out off the pool, which will handle connection-level errors... — committed to loopbackio/loopback-connector-postgresql by Quentin-M 7 months ago
Hey @abenhamdine - I’m glad to see the patch I put into
pg@6.2.4
is working!Before when connections unexpectedly dropped from the backend or a network partition happened node-postgres would silently ignore the closed connection and not always purge the disconnected connections from the pool so your pool could fill up with stale connections under heavy load. Under light load connections are purged on a configurable timeout, so you like wouldn’t see the old ‘stale connection’ issue unless you had sustained heavy load for many days in production or were doing db failovers or the like.
What’s happening now is one (or many) client(s) in your pool sits idle and connected to the backend database. A network partition happens or database failure, fail-over, forced disconnection from the backend, etc and the idle client(s) all notice and emit an error event. The pool listens for errors on idle clients. When an idle client has an error the pool destroys the client and removes it from the pool so the next time you request a client from the pool it will automatically create and connect a new client.
It’s unlikely the error is being emitted from the pool during an active query (if so, this is a bug) - during query execution a client will delegate error handling to its active query so the query can call it’s callback with the error (or reject it’s promise, etc).
You’ll want to put a
pool.on('error', (err) => console.error(e.stack))
listener or something on your pool and handle (or at least log) errors here. You probably don’t need to restart your application every time an idle client disconnects in the pool because node-postgres will try to do the right thing & reconnect you next time you need a client but you should at least be aware of it. And since node crashes the process when you don’t listen for anerror
event, by default your process is crashing.tl; dr -
Hope this helps. I’ve tagged this as a doc candidate so I’ll refine the docs here & make an entry in the docs about error handling of pooled clients.
@aranair Yes, you currently need to attach an error listener manually while the client is checked out. It’s for any connection-level errors (including getting disconnected in various ways) that happen during that time, which could be before a query, during it, after it…
@vyerneni17 attempting to belatedly provide an answer for @brianc:
You don’t need to do anything in the listener aside logging it to your destination of choice.
You still want to add a listener:
Hope that helps!
Hi @brianc, I faced this issue in my application. Thank you for your explanation I was able to put in the console.error and know why and when it is happening.
My question is how should I handle it in the pool.on (‘error’, () => { }) ? I mean to say, how would I reconnect automatically when the connection terminates ? At this point, I’m required to restart the node server manually when the connection is terminated.
Please pardon if it is a silly question, just wanted to know what would be the best way to do this ? Thank you!
I’ve spent too much time on this, on typeorm side, but for eveyone who is struggling with this:
If you read the documentation carefully(events section on https://node-postgres.com/api/client), then you’ll see that connection errors are emitted on client, not thrown on client.query().Thus, all the SQL errors are thrown, or returned on the callback, but for connection errors you must attach client.on(‘error’), and if you don’t the error will be uncaught and by default the process exits.
If you look at pool.query() you see that they properly handle on(‘error’) AND query callback errors, so if you use client returned from pool.connect you must do the same.
What is weird though, as noted also by @dobesv , if you do attach on(‘error’) then the client.query() calls will start throwing exceptions. This is inconsistent or a bug, not sure.
I’ve been running into this issue today, and what I’m seeing doesn’t seem to match what is supposed to be happening.
I create my pool like this:
Then I use it like this:
When I kill the backend server, the error printed is like this:
Notice how:
Got error from client instance
), not the pool, AND it is thrown as an exception from the query.08000
which I can use to realize the nature of the error.If I don’t add an error handler onto the client I checkout from the pool, I get an error like this when I restart the database server, and the process is terminated:
This seems counter to some of the information above that said that if a query were in progress, the error emitter would not be called.
@CoreyCole You probably just want a pool.
There’s some documentation on how to use the pool at https://node-postgres.com/features/pooling.
@brianc @pcothenet I’m running my node process with nodemon, I’m not sure if this changes things, but when this error occurs it stops the process.
(I’ve already added the event listener)
I’m encountering problems on this front as well. I’m trying to intercept momentary interruptions (e.g., due to a db restart) which emit this error:
terminating connection due to unexpected postmaster exit
.Initially I tried connecting a handler for pool errors on the
pool
instance directly, but found it was never triggered when restarting my database:I then tried waiting to attach the error handler until I get the
connect
event emitted:Much better luck there. I got the errors logged out. Unfortunately somewhere along the way there is still an error thrown from the connection itself:
@brianc or others: Can you advise on a thorough, proper implementation which will avoid unhandled errors in situations like these? Have searched the issues, but haven’t had much luck for a comprehensive solution with the new pools.