node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

restarting postgres throws an error

Open chani3 opened this issue 13 years ago • 6 comments

if I restart postgres, pg, throws an error like so:

events.js:72 throw er; // Unhandled 'error' event ^ error: terminating connection due to administrator command at Connection.parseE (/home/ec2-user/source/releases/20131108025818/node_modules/pg/lib/connection.js:558:11) (sorry, the rest of the BT was lost)

it should call my callback with the error instead, so that I don't have to resort to ugly hacks to make sure it's caught.

chani3 avatar Nov 13 '13 22:11 chani3

just looked at master, and there's no throwing anywhere near connection.js:558. maybe this was fixed already? (but in what version?)

chani3 avatar Nov 13 '13 22:11 chani3

nope, one of the servers is using 2.8.2 (latest release) and still crashes. got a full backtrace now:

events.js:72 throw er; // Unhandled 'error' event ^ error: terminating connection due to administrator command at Connection.parseE (/home/ec2-user/source/releases/20131112200610/node_modules/pg/lib/connection.js:558:11) at Connection.parseMessage (/home/ec2-user/source/releases/20131112200610/node_modules/pg/lib/connection.js:387:17) at null. (/home/ec2-user/source/releases/20131112200610/node_modules/pg/lib/connection.js:92:20) at Socket.EventEmitter.emit (events.js:95:17) at Socket. (stream_readable.js:720:14) at Socket.EventEmitter.emit (events.js:92:17) at emitReadable (_stream_readable.js:392:10) at emitReadable (_stream_readable.js:388:5) at readableAddChunk (_stream_readable.js:150:9) at Socket.Readable.push (_stream_readable.js:113:10) error: Forever detected script exited with code: 8

chani3 avatar Nov 13 '13 23:11 chani3

Are you handling the 'error' event? I was unable to reproduce your issue with an error handler, but I got the same result as you when I removed the handler. However, I did receive a fatal error when pg tries to end the connection, since the database has gone away and can not accept the command. I have submitted pull request #468 to handle this error.

Here is the error output:

Error: write ECONNABORTED
    at errnoException (net.js:901:11)
    at Socket._write (net.js:643:26)
    at doWrite (_stream_writable.js:219:10)
    at writeOrBuffer (_stream_writable.js:209:5)
    at Socket.Writable.write (_stream_writable.js:180:11)
    at Socket.write (net.js:613:40)
    at Connection._send (c:\Users\michaelp\Google Drive\Projects\mongres\node_modules\pg\lib\connection.js:169:26)
    at Connection.end (c:\Users\michaelp\Google Drive\Projects\mongres\node_modules\pg\lib\connection.js:292:8)
    at Client.end (c:\Users\michaelp\Google Drive\Projects\mongres\node_modules\pg\lib\client.js:299:19)
    at Object.genericPool.Pool.destroy (c:\Users\michaelp\Google Drive\Projects\mongres\node_modules\pg\lib\pool.js:40:16)

benighted avatar Nov 15 '13 17:11 benighted

@chani3 to expand on what @benighted wrote...if you have an open connection to postgres and the backend server crashes or for any other reason sends an error message to one or many connected clients those clients will emit that error. If you are using the pool this means all idle, connected clients in the pool will also emit that error. If you want to handle idle pooled clients attach an error listener to pg.on('error', function(err) { /* handle idle client error */ })

If you have manually created/managed client instances you'll need to be prepared for them to emit an error at any time just as you would have to prepare for any other stream to do similarly so.

Do you have a snippet of the code so I can elaborate a bit for your particular circumstance?

brianc avatar Nov 18 '13 04:11 brianc

oh, I had no idea I was supposed to do pg.on('error',... I thought the callbacks would be enough.

my code was pretty simple:

postgres.connect(connectionString, function runPooledQuery(err, client, done) {
   if (err) {
      done(); //probably unnecessary but doesn't hurt
      callback(err);
   } else {
       client.query(sql, args, function gotResult(err, result) {
           done();
          callback(err, result);
    });
   }
});

chani3 avatar Nov 18 '13 20:11 chani3

Yeah sorry - there's no easy way to handle errors in things that are idling and connected in the background. I think I've actually experienced something similar with the node http agent throwing weird ECONNTIMEOUT stuff when doing long running scraping jobs but it's really, really hard to pin that kinda thing down. Glad in this case we were able to figure it out for you!

brianc avatar Nov 19 '13 06:11 brianc