Skip to content

Fix: Connection terminated unexpectedly error not caught in on error listener #781

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@ function PostgreSQL(postgresql, settings) {
function attachErrorHandler(settings, pg) {
if (settings.onError) {
if (settings.onError === 'ignore') {
pg.on('error', function(err) {
debug(err);
});
// Check if the error handler is already attached
// to avoid possible memory leak
if (!pg.listenerCount('error')) {
pg.on('error', function(err) {
debug(err);
});
}
} else {
pg.on('error', settings.onError);
// Check if the error handler is already attached
// to avoid possible memory leak
if (!pg.listenerCount('error')) {
pg.on('error', settings.onError);
}
}
}
}
Expand Down Expand Up @@ -165,8 +173,6 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
// Release the connection back to the pool.
if (releaseCb) {
releaseCb(err);
// Remove error event listener to avoid possible memory leak
connection.removeAllListeners('error');
}
let result = null;
if (data) {
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"pretest": "node pretest.js",
"test": "mocha test/*.test.js",
"posttest": "npm run lint",
"lint": "eslint ."
"lint": "eslint .",
"lint-fix": "eslint . --fix"
},
"files": [
"intl",
Expand Down Expand Up @@ -47,6 +48,7 @@
"loopback-datasource-juggler": "^5.1.5",
"mocha": "^11.0.0",
"rc": "^1.0.0",
"rewire": "^8.0.0",
"should": "^13.2.3",
"sinon": "^21.0.0"
}
Expand Down
42 changes: 42 additions & 0 deletions test/postgresql.attachErrorHandler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright IBM Corp. 2013,2025. All Rights Reserved.
// Node module: loopback-connector-postgresql
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0

'use strict';
const sinon = require('sinon');
const assert = require('assert');
const rewire = require('rewire');
const postgresqlModule = rewire('../lib/postgresql');
const attachErrorHandler = postgresqlModule.__get__('attachErrorHandler');

describe('attachErrorHandler', function() {
let pg;
beforeEach(function() {
pg = {
on: sinon.spy(),
listenerCount: sinon.stub().returns(0),
};
});

it('should attach custom handler if onError is a function', function() {
const handler = sinon.spy();
const settings = {onError: handler};
attachErrorHandler(settings, pg);
assert(pg.on.calledOnce, 'pg.on should be called once');
assert(pg.on.firstCall.args[1] === handler, 'should attach the custom handler');
});

it('should not attach handler if already attached', function() {
pg.listenerCount.returns(1);
const settings = {onError: 'ignore'};
attachErrorHandler(settings, pg);
assert(pg.on.notCalled, 'pg.on should not be called if already attached');
});

it('should do nothing if onError is not set', function() {
const settings = {};
attachErrorHandler(settings, pg);
assert(pg.on.notCalled, 'pg.on should not be called if onError is not set');
});
});