-
Notifications
You must be signed in to change notification settings - Fork 4
If the connection to PG fails during execution, it will cause the program to crash (infinite loop) #60
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
Comments
I don't think this is an issue with Postgres.js. I also tested it using Postgres.js. After killing the PG process, Postgres.js correctly throws an error and attempts to reconnect. Once the reconnection is successful, the program outputs results correctly, which is the expected behavior. |
this only happens when i try to run my nodejs server while postgresql db server is off or inaccessible (e.g. due to docker initialization) in docker postgres if you have init sql scripts it will restart once. if your nodejs server tries to connect while it's initializing / restarting, you'll get this error above. |
Hi, I didn't encounter this issue when using only Postgres.js. Does this problem occur only when using the Docker image? Could you provide a minimal reproducible code or repository? |
import { Kysely } from "kysely";
import { PostgresJSDialect } from "kysely-postgres-js";
import postgres from "postgres";
import type { KyselyDatabase } from "./index.js";
const POSTGRES_HOST = "localhost";
const POSTGRES_PORT = 5432;
const POSTGRES_DB = "postgres";
const POSTGRES_USER = "postgres";
const POSTGRES_PASSWORD = "postgres";
export const pgc = postgres({
database: POSTGRES_DB,
host: POSTGRES_HOST,
port: POSTGRES_PORT,
user: POSTGRES_USER,
password: POSTGRES_PASSWORD,
max: 10,
});
export const pgd = new PostgresJSDialect({ postgres: pgc });
export const pgdb = new Kysely<KyselyDatabase>({ dialect: pgd });
try {
const users = await pgc`SELECT * FROM users;`;
console.log({ users });
} catch (e) {
if (e instanceof Error) {
console.error("POSTGRES.JS ERROR CAUGHT");
console.error(e);
}
}
try {
const users = await pgdb.selectFrom("users").selectAll().execute();
console.log({ users });
} catch (e) {
if (e instanceof Error) {
console.error("KYSELY ERROR CAUGHT");
console.error(e);
}
} user@device:~/Documents/server$ tsc
user@device:~/Documents/server$ node ./dist/postgres.mjs
POSTGRES.JS ERROR CAUGHT
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16)
at cachedError (file:///home/user/Documents/server/node_modules/postgres/src/query.js:170:23)
at new Query (file:///home/user/Documents/server/node_modules/postgres/src/query.js:36:24)
at sql (file:///home/user/Documents/server/node_modules/postgres/src/index.js:112:11)
at file:///home/user/Documents/server/dist/postgres.mjs:20:29 {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
file:///home/user/Documents/server/node_modules/postgres/src/connection.js:389
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
^
TypeError: Cannot read properties of undefined (reading 'replace')
at queryError (file:///home/user/Documents/server/node_modules/postgres/src/connection.js:389:48)
at errored (file:///home/user/Documents/server/node_modules/postgres/src/connection.js:384:17)
at Socket.error (file:///home/user/Documents/server/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)
Node.js v20.13.1 user@device:~/Documents/server$ npx tsx ./src/postgres.mts
POSTGRES.JS ERROR CAUGHT
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16)
at cachedError (file:///home/user/Documents/server/node_modules/postgres/src/query.js:170:23)
at new Query (file:///home/user/Documents/server/node_modules/postgres/src/query.js:36:24)
at sql (file:///home/user/Documents/server/node_modules/postgres/src/index.js:112:11)
at <anonymous> (/home/user/Documents/server/src/postgres.mts:27:23) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
file:///home/user/Documents/server/node_modules/postgres/src/connection.js:389
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
^
TypeError: Cannot read properties of undefined (reading 'replace')
at queryError (file:///home/user/Documents/server/node_modules/postgres/src/connection.js:389:48)
at errored (file:///home/user/Documents/server/node_modules/postgres/src/connection.js:384:17)
at Socket.error (file:///home/user/Documents/server/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)
Node.js v20.13.1 dang it never reached |
nope, it also occurs even in my localhost (no docker). example provided above. same results with tsc & tsx compiled typescript code. you're right that it throws proper econrefused when using postgres.js, but when using the kysely interface it doesn't throw a catchable error at all. |
Found this issue upstream, they're using mssql but similar problem where app crashes when db unreachable. |
@joshxyzhimself Yes, we are encountering the same issue. Currently, we can use process.on("uncaughtException", (err) => {
console.error("uncaughtException: ", err);
}); to catch the error. But in this case, it isn't very helpful because it keeps throwing errors in an infinite loop, blocking the main process and making the service completely unusable. I think this might need a thorough investigation @igalklebanov |
Hey 👋 This happens in For now, use this library @ v1.x.x (this is before Alternatively, extend the driver, and override the Alternatively, don't execute with Kysely, but compile and pass the compiled queries straight to Alternatively, patch |
Not the same.
Unlike The issue was Here, |
For those who have this problem still unresolved - in my case I forgot to destroy db instance after each call, so requests were creating a new clients each time. Once max client threshold was reached it failed with this ugly error:
|
Env:
Reproduction steps:
Error logs:
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.
The text was updated successfully, but these errors were encountered: