I am getting the similar issue as #1165 , while using the pg-cursor and pg. I am not using pool as I need only 2 connections. Any idea on how this can be solved. I am using [email protected] and [email protected]
The error that I am getting is (after I have explicitly called cursor.end()) :-
Uncaught Error: Connection terminated unexpectedly at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickDomainCallback (internal/process/next_tick.js:128:9)
This happens when I catch the error in case of incorrect query being sent.
My code looks something like :-
self.getClient(state.client, function (client, err) {
if (err) callback(err, null, null)
state.client = client
if (state.cursor == null) {
var cursor = client.query(new Cursor(exp.rdbms.query, exp.rdbms.values))
state.cursor = cursor
}
state.cursor.read(FETCH_SIZE, function (err, rows) {
if (err) {
// As I understand .end() is more like abort, while close
// closes the connection amicably.
state.cursor.end(function (err) {
if (err) logger.error('Cursor ending failed :: ', err)
delete state.cursor
state.client.end(function (err) {
if (err) logger.error('Connection ending failed:: ', err)
delete state.client
})
})
return callback(err)
}
if (!rows || rows.length === 0) {
state.cursor.close(function (err) {
if (err) logger.error('error closing cursor', err)
})
return callback()
}
state.rows = rows
return callback()
})
})
ON Debugging, I can further see the whole stack trace :-
Error: Connection terminated unexpectedly
at Connection.con.once (/<mymodule>/node_modules/pg/lib/client.js:179:21)
at Connection.g (events.js:291:16)
at emitNone (events.js:86:13)
at Connection.emit (events.js:185:7)
at Socket.<anonymous> (/<mymodule>/node_modules/pg/lib/connection.js:123:10)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
I'm getting this error also using Knex
2018-06-20T14:12:01.864502914Z ERROR Error: Connection terminated
2018-06-20T14:12:01.864555652Z at Connection.con.once (/app/node_modules/pg/lib/client.js:170:29)
2018-06-20T14:12:01.864561989Z at Object.onceWrapper (events.js:255:19)
2018-06-20T14:12:01.864565623Z at Connection.emit (events.js:160:13)
2018-06-20T14:12:01.864648360Z at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:76:10)
2018-06-20T14:12:01.864654860Z at Socket.emit (events.js:160:13)
2018-06-20T14:12:01.864736064Z at TCP._handle.close [as _onclose] (net.js:562:12)
2018-06-20T14:12:01.864741161Z From previous event:
2018-06-20T14:12:01.864743890Z at Client_PG._query (/app/node_modules/knex/lib/dialects/postgres/index.js:287:12)
2018-06-20T14:12:01.864827939Z at Client_PG.query (/app/node_modules/knex/lib/client.js:206:17)
2018-06-20T14:12:01.864833037Z at Runner.<anonymous> (/app/node_modules/knex/lib/runner.js:155:36)
2018-06-20T14:12:01.864835873Z From previous event:
2018-06-20T14:12:01.864838420Z at /app/node_modules/knex/lib/runner.js:61:21
2018-06-20T14:12:01.864949970Z at runCallback (timers.js:756:18)
2018-06-20T14:12:01.865175034Z at tryOnImmediate (timers.js:717:5)
2018-06-20T14:12:01.865200577Z at processImmediate [as _immediateCallback] (timers.js:697:5)
2018-06-20T14:12:01.865270155Z From previous event:
2018-06-20T14:12:01.865358401Z at Runner.run (/app/node_modules/knex/lib/runner.js:47:31)
2018-06-20T14:12:01.865363267Z at Builder.Target.then (/app/node_modules/knex/lib/interface.js:39:43)
2018-06-20T14:12:01.865365706Z at process._tickCallback (internal/process/next_tick.js:160:7)
I'm also having a connection error crashing my program. I'm not entirely sure what state my Client object is in when this occurs, but I've created a test which produces the same error by abusing the Connection object:
'use strict';
const { Client } = require('pg');
async function main() {
const client = new Client();
await client.connect();
try {
// Simulate the connection dying unexpectedly.
client.connection.end();
await client.query(`SELECT 1 AS one`);
} finally {
await client.end();
}
}
main().then(() => {
process.exit(0);
}).catch(err => {
process.exit(1);
});
Even though I'm abusing the Connection object, I would expect this program to print nothing and exit (with exit status 0 or 1). Instead, the built-in events library terminates the program because no error handler is available for the Client object:
events.js:167
throw er; // Unhandled 'error' event
^
Error: Connection terminated unexpectedly
at Connection.con.once (/mytest/node_modules/pg/lib/client.js:200:9)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket.<anonymous> (/mytest/node_modules/pg/lib/connection.js:76:10)
at Socket.emit (events.js:182:13)
at TCP._handle.close (net.js:606:12)
Emitted 'error' event at:
at connectedErrorHandler (/mytest/node_modules/pg/lib/client.js:148:10)
at Connection.con.once (/mytest/node_modules/pg/lib/client.js:216:9)
at Object.onceWrapper (events.js:273:13)
[... lines matching original stack trace ...]
at TCP._handle.close (net.js:606:12)
If I modify the test to add an error listener to the Client object, the program fails in the expected manner:
'use strict';
const { Client } = require('pg');
async function main() {
const client = new Client();
client.once('error', err => {});
await client.connect();
try {
// Simulate the connection dying unexpectedly.
client.connection.end();
await client.query(`SELECT 1 AS one`);
} finally {
await client.end();
}
}
main().then(() => {
process.exit(0);
}).catch(err => {
console.log('Error is caught in anticipated location:');
console.log(err);
process.exit(1);
});
Can anyone more familiar with the inner workings of the library comment on what normal situations cause the Client object to emit error events?
@novakka4096 The connection dying unexpectedly is one such situation. In general, you should have an 'error' event listener.
Any progress or difficulty?
Most helpful comment
I'm getting this error also using Knex