porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

nir-bar-zvi opened this issue · 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:

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

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

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

thanks a lot - merged to master