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

Unhandled execption when postgreSQL server stops

Open david-rouchet opened this issue 12 years ago • 9 comments

An Unhandled 'error' event is generated when a client is connected to the database and the server is stopped. The documentation does not specify if there is a way to handle this case.

david-rouchet avatar Nov 17 '14 20:11 david-rouchet

Sorry for the lack of documentation on that case....this is probably as close as you can get.

If you're using a stand-alone client:

https://github.com/brianc/node-postgres/wiki/Client#event-error

If you're using the pool:

https://github.com/brianc/node-postgres/wiki/pg#error--object-error-object-client

brianc avatar Nov 17 '14 20:11 brianc

Indeed, the .on('error'...) handler allows to catch this case. However, we might think to use this handler on the object, although this is pg itself which handle it.

pg.on('error', function(err) { console.log(err); });

david-rouchet avatar Nov 17 '14 21:11 david-rouchet

I've just hit a similar issue, due to connections being terminated.

We were expecting client.on('error') to be able to handle terminated connection but the process was being killed due to the uncaught exception. If you setup a pg.on('error') that does nothing then the client handler will be called.

I would have expected that setting up client.on('error') would be sufficient and pg.on('error') should not be required.

var pg = require('pg');

if (1) { // if we don't do this we get an Unhandled 'error' event and client.on('error') isn't called
    pg.on('error', function(err) {
        console.log('do nothing');
    });
}

var client = pg.connect(function(err, client) {
    if (err) {
        console.log('ERROR: failed to connect', err);
        return;
    }

    console.log('Connected');
    // use select pg_terminate_backend(pid) to kill this connection

    client.on('error', function(err) {
        console.log('Client error', err);
    });
});

sibson avatar Mar 16 '15 22:03 sibson

Hi, I'm binding to the global error event (on pg) and still errors bubble up and crash the process. Was expecting pg.on('error', ...) to stop that from happening.

I'm using PG 4.1.1. I know it's not the latest but I don't like packages changing on me without testing. I looked for a changelog to see if this was fixed from 4.1.1 to 4.3.0 but couldn't find any.

flesler avatar Jun 02 '15 15:06 flesler

I also have same issue when mongodb server is stopped:

uncaughtException: connect ETIMEDOUT

it is not handled by client.on('error',..);

Typical use case for it: db server and node service is in different VMs and node already started but db is not yet or because of some other network issues, so it will good to handle this error not just throw uncaughtException, so we will able to apply some logic.

Thank you.

shvydok avatar Jun 13 '16 10:06 shvydok

I see this problem regularly in this context:

I have multiple scheduled jobs that query our DB independently. Their queries overlap in time. One query will have their exception caught (the one that generates the DB error shown below) while the other will throw an uncaught exception, killing node.

Here is the caught exception

{  
   "name":"error",
   "length":339,
   "severity":"ERROR",
   "code":"XX000",
   "detail":"\n -----------------------------------------------\n error: Disk Full\n code: 1016\n context: node: 0\n query: 9227519\n location: fdisk_api.cpp:350\n process: query0_56 [pid=26878]\n -----------------------------------------------\n",
   "file":"/home/awsrsqa/padb/src/sys/xen_execute.cpp",
   "line":"6198",
   "routine":"pg_throw"
}

kruser avatar Jun 13 '16 16:06 kruser

Can you submit a self-contained code sample that reproduces this issue? I'm happy to fix it in that case. Otherwise, I'm not sure what to do as I've not seen this myself.

brianc avatar Jun 21 '16 19:06 brianc

Thank you for follow-up, after some update it is not reproducible anymore for me.

Ihor.

On Tue, Jun 21, 2016 at 9:40 PM, Brian C [email protected] wrote:

Can you submit a self-contained code sample that reproduces this issue? I'm happy to fix it in that case. Otherwise, I'm not sure what to do as I've not seen this myself.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/brianc/node-postgres/issues/683#issuecomment-227548861, or mute the thread https://github.com/notifications/unsubscribe/ARdRxzmy1d9-8vFBTnN5bTm4hqpA7w3jks5qOD4lgaJpZM4C8vHc .

shvydok avatar Jun 21 '16 22:06 shvydok

Can you submit a self-contained code sample that reproduces this issue? I'm happy to fix it in that case. Otherwise, I'm not sure what to do as I've not seen this myself.

@brianc - I won't have time to build this anytime soon. So I'm fine if you close it for now. If I'm able to drill into it and create a self contained reproduction I'll submit it.

kruser avatar Jun 24 '16 15:06 kruser