Node-postgres: Unhandled execption when postgreSQL server stops

Created on 17 Nov 2014  Â·  9Comments  Â·  Source: brianc/node-postgres

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.

Most helpful comment

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);
    });
});

All 9 comments

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

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);
});

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);
    });
});

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.

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.

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"
}

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.

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
.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gregallenvt picture gregallenvt  Â·  3Comments

wrod7 picture wrod7  Â·  4Comments

dipakdas99 picture dipakdas99  Â·  3Comments

gpanainte picture gpanainte  Â·  3Comments

tonylukasavage picture tonylukasavage  Â·  4Comments