Unhandled 'error' event thrown when the connection password doesn't match
When I connect to an existing database with the connect username but the wrong password, I got an unhandled exception using the following code. The parameter err is set, but the exception is still thrown.
pg.connect(dbURL, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
//call `done()` to release the client back to the pool
done();
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
//output: 1
client.end();
});
});
That's odd...I'm pretty sure we have a test for that in the unit tests. What version of PG & node-postgres?
On Wed, Mar 11, 2015 at 4:48 PM, david-rouchet [email protected] wrote:
When I connect to an existing database with the connect username but the wrong password, I got an unhandled exception using the following code. The parameter err is set, but the exception is still thrown.
pg.connect(dbURL, function(err, client, done) { if(err) { return console.error('error fetching client from pool', err); } client.query('SELECT $1::int AS number', ['1'], function(err, result) { //call
done()to release the client back to the pool done();if(err) { return console.error('error running query', err); } console.log(result.rows[0].number); //output: 1 client.end();}); });
— Reply to this email directly or view it on GitHub https://github.com/brianc/node-postgres/issues/746.
pg version is 4.3.0, node-js is 0.10.31 and postgreSQL server is 9.3.5
Hi there, any clue, bug fiix or suggestion on this issue?
Hi, I am encountering the same issue when using an incorrect username. My callback to pg.connect is executed with a "password authentication failed..." error, which I deal with. However, an ECONNRESET error is subsequently emitted, hitting this line, and there is no way for me to handle it. I tested with node-postgres (not native) 4.3.0 on Windows and Mac. I am running PostgreSQL 9.4 on Windows.
I still have the same issue and this become a concern for the project I'm working on...
+1 Happens here always when the password is incorrect.
+1
events.js:72
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at errnoException (net.js:901:11)
at TCP.onread (net.js:556:19)
happens when calling pg.connect with either user, password or db incorrect. windows7, node v0.10.22, PostgreSQL 9.4.1, pg 4.3.0
So far no fix has been proposed. Does anywone see a way to workaround this issue? We still have a project going on and which is fragilized by this error.
I think the best work around would be to submit a failing test or even better some code to fix the issue in a PR. :wink:
I can't reproduce the issue locally - the error is correctly passed to the callback with both a bad host or bad username.
I also already have test cases covering this issue and they pass:
https://github.com/brianc/node-postgres/blob/master/test/integration/client/error-handling-tests.js#L136 https://github.com/brianc/node-postgres/blob/master/test/native/connection-tests.js#L5
Looks like the right test (user/password) is commented out (?) - https://github.com/brianc/node-postgres/blob/master/test/integration/client/error-handling-tests.js#L148
The one you refer to is about invalid host (means no response instead of refusing connection) - https://github.com/brianc/node-postgres/blob/master/test/integration/client/error-handling-tests.js#L136
Here is also the minimum code to reproduce the issue -
var pg = require('pg');
pg.connect('postgres://abc:def@localhost:5432/xyz', function (err, db, done) {
console.log('connect callback called');
});
which produces
connect callback called
events.js:72
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at errnoException (net.js:901:11)
at TCP.onread (net.js:556:19)
I did not test this with native bindings - possibly the issue is limited to js
I do agree, the unhandled error occurs when the server can be reached but either the username or the password is incorrect.
Another code example which reproduces this error.
In this example, 'postGres' should be 'postgres'. So if 'postGres' is used instead of 'postgres' the Unhandled exception is raised.
var pg = require('pg');
var dbURL = 'postgres://postgres:[email protected]/dbname';
if (0) { // whether this code is activated or not, the error is not catched
pg.on( 'error', function(err) {
console.log('do nothing');
});
}
try {
pg.connect( dbURL, function(err, client, done) {
if ( err ) {
return console.error( 'Connection error handled, code: ', err.code);
}
else {
if ( client ) {
console.error( 'Connection succeed on ', client.host );
}
else {
console.error( 'Connection done but client is null or undefined' );
}
}
});
}
catch( err ) {
console.log( 'DB Connect error handled');
}
+1000
The same error is thrown when we pass an invalid connection:
pg.connect("bla-bla");
Other than that, it is 100% reproducible for me, for both invalid user name and invalid password.
It's been way too long for such a critical issue to just sit here and nothing being done about it. Very frustrating.
I have a number of tests now for pg-promise commented out because of this error, which just kills jasmine framework that I'm using for testing.
Should have been labelled as a bug, at least, as it definitely is.
@vitaly-t The great thing about open source is if you're having frustration with something you can submit a patch & make the code better for everyone! I tried to reproduce the issue locally and wasn't able to and don't have time right now to dive deep into something that isn't causing me any issues and isn't reproducible on my end. Do you have the time to put together a pull request with a test and a fix for this?
The minimum code sample submitted by @kamushkin did not throw an error for me and the error was properly handled.
@brianc, it's all doable, I'm just pressed for time, same as yourself, struggling to make time to maintain my own library :)
Anyhow, the issue here goes outside node-postgres, if you Google for it:
events.js:72
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at errnoException (net.js:901:11)
at TCP.onread (net.js:556:19)
you will see how many people complain about the same error, just related to different libraries. The issue is within one of the sub-libraries that are used. I did take my time to narrow it down somehow, but without luck so far.
I don't understand how the issue doesn't manifest itself. I've tried it on 3 computers at this point, and can see it happen on each of them. Try reproducing the issue on other computers.
Also, as was pointed out earlier, the key tests for this are commented out, which is the key to showing the problem. Un-commenting those tests would be a good starting point.
I'm not able to reproduce this either. I tried the sample code and it works fine with v4.3.0 on both my Mac and Linux desktop (Ubuntu 14.04). Both were run using only the JS bindings (not the native ones).
To reproduce the issue the host:port combination should be correct, i.e. you should actually have postgres running on localhost:5432 for the code above. It does not happen when the hostname or port is incorrect.
However the following also triggers the same exception for me -
var pg = require('pg');
pg.connect('bla-bla', function (err, db, done) {
console.log('connect callback called');
});
win7, node v0.10.22, PostgreSQL 9.4.1, pg 4.3.0
Unlike mention @kamushkin, the exception happens to me when either the login name or password is NOT correct. However, I noticed that @sehrope is doing his test on Linux/Mac platform whereas @kamushkin and myself on Windows (7).
I'm developing on Windows 8.1 64-bit, is where I see the problems.
@brianc
I have found the problem :)
When you introduced connection pool, you forgot something important about EventEmitter, namely how it handles special event error, although this goes beyond the connection pool also. When your custom type doesn't provide a handler for event error, it is automatically converted into uncaughtException by EventEmitter.
In cases that were under consideration:
- Invalid connection string
- Invalid user name
- Invalid password
there was event error emitted by your library, which without any handler on the corresponding objects is turned into uncaughtException by EventEmitter. Those corresponding objects in your library are Client and Connection.
What you were supposed to do is to provide an empty trap for event error on each of those objects. If you Google for the error being thrown:
at exports._errnoException (util.js:746:11)
at TCP.onread (net.js:559:26)
you will find many other examples of people running into the same and finding the same solution in the end.
In order to fix the issue, you need to add the following code:
this.on('error', function () {
// prevent EventEmitter from turning event
// `error` into `uncaughtException`;
});
at the very beginning of:
Client.prototype.connect = function(callback) {, - in file client.jsConnection.prototype.connect = function(port, host) {, - in file connection.js
And that is it.
P.S. While those who use Client and Connection directly, they could work-around the issue by adding error handler on each of them, it is impossible for those who rely on the connection pool.
I appreciate you looking into this, but providing an empty trap for the error event on EventEmitter instances is not the right approach. This swallows potentially catastrophic errors without any warning or notice of any kind. The correct thing to do is in fact let the EventEmitter instances crash the process if they don't have an error event listener added by the consuming library.
The problem is here a bit more complicated...because node-postgres accepts methods with callbacks it attempts to temporarily subscribe to and handle internal error events when a callback is provided and might expect the error.
Something is going on in the particular instance of a bad password where 2 error events are getting emitted or something, and internally node-postgres is only handling one. My hunch is postgres returns an error message and internally an error event is emitted. Node-postgres is listening for the error and grabs it and passes it to the connection callback as an error. After the error is handled node-postgres stops listening for more errors. I'm guessing after postgres returns an error its closing the socket. Since node-postgres has already handled 1 error the next one bubbles up. If you put an error handler on the pg instance itself you might be able to supress the error there...
pg.on('error', function(e) {
console.log('something bad happened...', e);
}
pg.connect(function(err, client, done) {
});
The problem is I cannot reproduce this locally no matter what I try. I don't have access to a windows machine, and don't really test on windows myself. If someone who can actually reproduce the error will step up with a pull request to address the issue - I'm guessing at the end of the day the pull request will be 1-2 lines tops - I will happily merge & push a new version, otherwise I wont be contributing anything more here.