Skip to content

Commit 3dc37cc

Browse files
rchandranmanidhmlau
authored andcommitted
fix: handle unexpected connection termination error in error listener
Signed-off-by: Ramachandran Mani <[email protected]>
1 parent e820316 commit 3dc37cc

File tree

4 files changed

+507
-7
lines changed

4 files changed

+507
-7
lines changed

lib/postgresql.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,19 @@ function PostgreSQL(postgresql, settings) {
103103
function attachErrorHandler(settings, pg) {
104104
if (settings.onError) {
105105
if (settings.onError === 'ignore') {
106-
pg.on('error', function(err) {
107-
debug(err);
108-
});
106+
// Check if the error handler is already attached
107+
// to avoid possible memory leak
108+
if (!pg.listenerCount('error')) {
109+
pg.on('error', function(err) {
110+
debug(err);
111+
});
112+
}
109113
} else {
110-
pg.on('error', settings.onError);
114+
// Check if the error handler is already attached
115+
// to avoid possible memory leak
116+
if (!pg.listenerCount('error')) {
117+
pg.on('error', settings.onError);
118+
}
111119
}
112120
}
113121
}
@@ -165,8 +173,6 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
165173
// Release the connection back to the pool.
166174
if (releaseCb) {
167175
releaseCb(err);
168-
// Remove error event listener to avoid possible memory leak
169-
connection.removeAllListeners('error');
170176
}
171177
let result = null;
172178
if (data) {

0 commit comments

Comments
 (0)