From 3147ecec433cb04685e915e6a4a3ffaf5c6ee23b Mon Sep 17 00:00:00 2001 From: phiresky Date: Mon, 22 May 2023 16:08:13 +0200 Subject: [PATCH 1/4] fix stack traces of query() to include the async context (#1762) --- packages/pg-pool/index.js | 5 ++ packages/pg/lib/client.js | 5 ++ .../integration/client/async-stack-trace.js | 47 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 packages/pg/test/integration/client/async-stack-trace.js diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 910aee6d2..3c0bf0665 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -39,6 +39,11 @@ function promisify(Promise, callback) { const result = new Promise(function (resolve, reject) { res = resolve rej = reject + }).catch(err => { + // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the + // application that created the query + Error.captureStackTrace(err); + throw err; }) return { callback: cb, result: result } } diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 88f2f5f36..a2b7c2686 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -520,6 +520,11 @@ class Client extends EventEmitter { if (!query.callback) { result = new this._Promise((resolve, reject) => { query.callback = (err, res) => (err ? reject(err) : resolve(res)) + }).catch(err => { + // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the + // application that created the query + Error.captureStackTrace(err); + throw err; }) } } diff --git a/packages/pg/test/integration/client/async-stack-trace.js b/packages/pg/test/integration/client/async-stack-trace.js new file mode 100644 index 000000000..507b89810 --- /dev/null +++ b/packages/pg/test/integration/client/async-stack-trace.js @@ -0,0 +1,47 @@ +'use strict' +var helper = require('../test-helper') +var pg = helper.pg + +process.on('unhandledRejection', function (e) { + console.error(e, e.stack) + process.exit(1) +}) + +const suite = new helper.Suite() + +suite.testAsync('promise API async stack trace in pool', async function outerFunction() { + async function innerFunction() { + const pool = new pg.Pool() + await pool.query('SELECT test from nonexistent'); + } + try { + await innerFunction(); + throw Error("should have errored"); + } catch (e) { + const stack = e.stack; + if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) { + throw Error("async stack trace does not contain wanted values: " + stack); + } + } +}) + +suite.testAsync('promise API async stack trace in client', async function outerFunction() { + async function innerFunction() { + const client = new pg.Client() + await client.connect(); + try { + await client.query('SELECT test from nonexistent'); + } finally { + client.end(); + } + } + try { + await innerFunction(); + throw Error("should have errored"); + } catch (e) { + const stack = e.stack; + if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) { + throw Error("async stack trace does not contain wanted values: " + stack); + } + } +}) \ No newline at end of file From 88e546f96d28ce2e9b3739bf7e4ae27ff29453da Mon Sep 17 00:00:00 2001 From: phiresky Date: Tue, 23 May 2023 12:07:26 +0200 Subject: [PATCH 2/4] rename tests so they are actually run --- .../client/{async-stack-trace.js => async-stack-trace-tests.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/pg/test/integration/client/{async-stack-trace.js => async-stack-trace-tests.js} (100%) diff --git a/packages/pg/test/integration/client/async-stack-trace.js b/packages/pg/test/integration/client/async-stack-trace-tests.js similarity index 100% rename from packages/pg/test/integration/client/async-stack-trace.js rename to packages/pg/test/integration/client/async-stack-trace-tests.js From a3b52954db4d09720378de088081ea18468ebab2 Mon Sep 17 00:00:00 2001 From: phiresky Date: Tue, 23 May 2023 12:10:30 +0200 Subject: [PATCH 3/4] conditionally only run async stack trace tests on node 16+ --- .../client/async-stack-trace-tests.js | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/packages/pg/test/integration/client/async-stack-trace-tests.js b/packages/pg/test/integration/client/async-stack-trace-tests.js index 507b89810..d69f7b717 100644 --- a/packages/pg/test/integration/client/async-stack-trace-tests.js +++ b/packages/pg/test/integration/client/async-stack-trace-tests.js @@ -9,39 +9,43 @@ process.on('unhandledRejection', function (e) { const suite = new helper.Suite() -suite.testAsync('promise API async stack trace in pool', async function outerFunction() { - async function innerFunction() { - const pool = new pg.Pool() - await pool.query('SELECT test from nonexistent'); - } - try { - await innerFunction(); - throw Error("should have errored"); - } catch (e) { - const stack = e.stack; - if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) { - throw Error("async stack trace does not contain wanted values: " + stack); +// these tests will only work for if --async-stack-traces is on, which is the default starting in node 16. +const NODE_MAJOR_VERSION = +process.versions.node.split('.')[0]; +if (NODE_MAJOR_VERSION >= 16) { + suite.testAsync('promise API async stack trace in pool', async function outerFunction() { + async function innerFunction() { + const pool = new pg.Pool() + await pool.query('SELECT test from nonexistent'); } - } -}) - -suite.testAsync('promise API async stack trace in client', async function outerFunction() { - async function innerFunction() { - const client = new pg.Client() - await client.connect(); try { - await client.query('SELECT test from nonexistent'); - } finally { - client.end(); + await innerFunction(); + throw Error("should have errored"); + } catch (e) { + const stack = e.stack; + if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) { + throw Error("async stack trace does not contain wanted values: " + stack); + } } - } - try { - await innerFunction(); - throw Error("should have errored"); - } catch (e) { - const stack = e.stack; - if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) { - throw Error("async stack trace does not contain wanted values: " + stack); + }) + + suite.testAsync('promise API async stack trace in client', async function outerFunction() { + async function innerFunction() { + const client = new pg.Client() + await client.connect(); + try { + await client.query('SELECT test from nonexistent'); + } finally { + client.end(); + } + } + try { + await innerFunction(); + throw Error("should have errored"); + } catch (e) { + const stack = e.stack; + if(!e.stack.includes("innerFunction") || !e.stack.includes("outerFunction")) { + throw Error("async stack trace does not contain wanted values: " + stack); + } } - } -}) \ No newline at end of file + }) +} \ No newline at end of file From 6059e3938d1a39d64f68aedeca31a5602c870e1c Mon Sep 17 00:00:00 2001 From: phiresky Date: Wed, 24 May 2023 13:16:12 +0200 Subject: [PATCH 4/4] add stack trace to pg-native --- packages/pg/lib/native/client.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/pg/lib/native/client.js b/packages/pg/lib/native/client.js index 6a8eb5363..1daaf1090 100644 --- a/packages/pg/lib/native/client.js +++ b/packages/pg/lib/native/client.js @@ -172,6 +172,9 @@ Client.prototype.query = function (config, values, callback) { result = new this._Promise((resolve, reject) => { resolveOut = resolve rejectOut = reject + }).catch(err => { + Error.captureStackTrace(err); + throw err; }) query.callback = (err, res) => (err ? rejectOut(err) : resolveOut(res)) }