postgres icon indicating copy to clipboard operation
postgres copied to clipboard

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

Open nir-bar-zvi opened this issue 2 years ago • 2 comments

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);

nir-bar-zvi avatar Apr 30 '24 11:04 nir-bar-zvi

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

porsager avatar Apr 30 '24 12:04 porsager

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

nir-bar-zvi avatar May 01 '24 11:05 nir-bar-zvi

thanks a lot - merged to master

porsager avatar May 15 '24 11:05 porsager

Hi, @porsager -- I ran into #924 when I should've gotten "SSL connection error: Internal Server Error" so I think we need that fix, too. I had to change my pnpm dependency to github:Nearblocks/postgres#fix/crash to see the correct error message

dcitron avatar Aug 28 '24 08:08 dcitron