Skip to content
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

TypeError: Cannot redefine property: parameters when connections is severed unexpectedly #854

Closed
nir-bar-zvi opened this issue Apr 30, 2024 · 3 comments

Comments

@nir-bar-zvi
Copy link

node version: 18.18.2
package version: ^3.4.3


if postgres connection dies while queries are running i get an unrelated error:

Exception has occurred: TypeError: Cannot redefine property: parameters
  at Function.defineProperties (<anonymous>)
    at queryError (/home/nir/pg_test/node_modules/postgres/cjs/src/connection.js:388:12)
    at error (/home/nir/pg_test/node_modules/postgres/cjs/src/connection.js:378:7)
    at Socket.closed (/home/nir/pg_test/node_modules/postgres/cjs/src/connection.js:438:44)
    at Socket.emit (node:events:517:28)
    at Socket.emit (node:domain:489:12)
    at TCP.<anonymous> (node:net:350:12)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17) 

jumping out of here:

function queryError(query, err) {
Object.defineProperties(err, {
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
query: { value: query.string, enumerable: options.debug },
parameters: { value: query.parameters, enumerable: options.debug },
args: { value: query.args, enumerable: options.debug },
types: { value: query.statement && query.statement.types, enumerable: options.debug }
})
query.reject(err)
}


for the purpose of reproducing this issue i have code that runs multiple queries that wait and i kill the container while its working:

import postgres from "postgres";

const waitsec = 5;

async function pgSleep(num: number) {
  const query = sql`select pg_sleep(${waitsec - 1})`;
  await query;
  console.log(`FINISHED QUERY ${num}`);
}

export const sql = postgres({
  username: "postgres",
  password: "postgres",
  database: "postgres",
  host: "localhost",
  port: 5432,
  ssl: false,
  max: 5,
  connect_timeout: 10,
  onnotice: () => {
    // ignore notices
  },
});

async function main() {
  while (true) {
    const ps = (
      [...Array(15)].map(async (_item, index) => {
        console.log("run query", index);
        console.time(`run query-${index}`);
        await pgSleep(index);
        console.timeEnd(`run query-${index}`);
      })
    );
    const rs = await Promise.allSettled(ps);
  }
}

main().catch(console.error);
@porsager
Copy link
Owner

Very nice bug report.

Mind giving this branch a try? https://github.com/porsager/postgres/tree/error-reassign-fix

You can try simply be npm install porsager/postgres#error-reassign-fix

@nir-bar-zvi
Copy link
Author

Very nice bug report.

i aim to please

Mind giving this branch a try? https://github.com/porsager/postgres/tree/error-reassign-fix

You can try simply be npm install porsager/postgres#error-reassign-fix

tested locally, looks go - ECONNREFUSED is correctly reflected out

@porsager
Copy link
Owner

thanks a lot - merged to master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants