porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.03k stars 255 forks source link

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

Closed nir-bar-zvi closed 1 month ago

nir-bar-zvi commented 2 months ago

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: https://github.com/porsager/postgres/blob/cc688c642fc98c4338523d3e281e03bf0c3417b8/cjs/src/connection.js#L387-L396


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 commented 2 months ago

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 commented 2 months ago

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 commented 1 month ago

thanks a lot - merged to master