Skip to content

If the connection to PG fails during execution, it will cause the program to crash (infinite loop) #60

@VxRain

Description

@VxRain

Env:

  • Win10
  • Node v20.14.0
  • kysely: 0.27.3
  • kysely-postgres-js: 2.0.0,
  • postgres: 3.4.4

Reproduction steps:

  1. Start the PostgreSQL service.
  2. Start the application.
  3. Kill the PostgreSQL process.
  4. The error repeats infinitely.
import { Kysely } from "kysely";
import { PostgresJSDialect } from "kysely-postgres-js";
import postgres from "postgres";

// listen for unhandledRejection and uncaughtException
process.on("unhandledRejection", (reason) => {
  console.error("unhandledRejection: ", reason);
});
process.on("uncaughtException", (err) => {
  console.error("uncaughtException: ", err);
});

const pg = postgres({
  host: "localhost",
  max: 10,
  port: 5432,
  database: "test",
  user: "test",
  password: "test",
});
const db = new Kysely({ dialect: new PostgresJSDialect({ postgres: pg }) });

async function main() {
  await pg`SELECT 1`; // check connection

  setInterval(async () => {
    const user = await db
      .selectFrom("User")
      .select("username")
      .where("id", "=", 1)
      .executeTakeFirstOrThrow();
    console.log(Date.now(), user);
  }, 1000);
}
main();

Error logs:

uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
uncaughtException:  TypeError: Cannot read properties of undefined (reading 'replace')
    at queryError (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:389:48)
    at errored (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:384:17)
    at Socket.error (file:///C:/Users/Administrator/Desktop/Workspace/kysely-postgres/node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:376:5)
    at Socket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Additionally, I found that if the connection to PostgreSQL fails when app start, the Kysely instance initializes normally, but executing a query throws an error: "Cannot read properties of undefined (reading 'replace')."

Related to #9, #10, #11, I think we should investigate further to resolve the related issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions