-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix ECONNRESET error emitted after failed connect #1230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
On Windows, after a connect attempt has failed, an error event with ECONNRESET is emitted after the real connect error is propagated to the connect callback, because the connection is not in ending state (connection._ending) where ECONNRESET is ignored. This change ends the connection when connect has failed. This fixes brianc#746.
I used the following test script to see where the error came from: const pg = require('pg');
pg.on('error', err => {
throw new Error(`got error from pg: ${err}`);
});
pg.connect({
connectionString: 'postgres://postgres:wrongpassword@localhost/postgres',
log: console.log,
}, (err, client, done) => {
if (err) {
console.log(`pg.connect failed: ${err}`);
done(err);
return;
}
console.log(`pg.connect succeeded`);
done();
pg.end();
}); and the output:
The last frame (client.js:183) is called by connection error event handler (connection.js:71), and there self._ending is false at the time, so it's not ignoring the ECONNRESET error. |
Hey @magnushiie - thank you so much for putting this together! Is there any way you could include a test for this? To write a test just add a new file under this folder that has the issue number (1230) - the file should exit with a 0 status code to indicate the tests pass. This way we can be sure not to introduce regressions in the future! |
Actually, the test "raises error if cannot connect" in test/integration/client/api-tests.js already fails for me without this fix:
With the fix:
When it fails, it also fails the suite (when run via make):
|
Ah nice thanks! |
On Windows, after a connect attempt has failed, an error event with
ECONNRESET is emitted after the real connect error is propagated to the
connect callback, because the connection is not in ending state
(connection._ending) where ECONNRESET is ignored. This change ends the
connection when connect has failed.
This fixes #746.