oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.29k stars 2.77k forks source link

Connection timeout when running pm2 + bun + TS + expressjs + postgres.js to connect to Postgresql database #8946

Closed minh5micron closed 1 month ago

minh5micron commented 9 months ago

What version of Bun is running?

1.0.26

What platform is your computer?

Linux 5.15.0-67-generic x86_64 x86_64

What steps can reproduce the bug?

We have Postgresql database size around 200Mb. Our web server code is written in TS + Expressjs, Postgresjs to connect to the local database and the app is run by Bun. We also use pm2 to start/restart and monitor the app itself. However it's strange that after around somewhere from 10 to 20 hours there is connection time out issue with the database.

2024-02-16T16:29:14: 22 |       errno: x,
2024-02-16T16:29:14: 23 |       address: options.path || host
2024-02-16T16:29:14: 24 |     }, options.path ? {} : { port: port }
2024-02-16T16:29:14: 25 |   )
2024-02-16T16:29:14: 26 |   Error.captureStackTrace(error, connection)
2024-02-16T16:29:14:              ^
2024-02-16T16:29:14: error: write CONNECT_TIMEOUT localhost:5432
2024-02-16T16:29:14:  code: "CONNECT_TIMEOUT"
2024-02-16T16:29:14: 
2024-02-16T16:29:14:       at connection (/home/****/node_modules/postgres/src/errors.js:26:9)

The database is still active without errors. We restart the app by "pm2 restart" and the connection timeout issue is disappeared. Therefore we suspect that somehow Bun + Postgresjs is not stable enough though we have no ways to debug it. It would be nice if anyone can explain for us or show us some ways to debug :)

It is worth mentioning that we use the combo pm2 + Node + express + postgresjs before and never encountered into this issue before. The reason why we are trying to switch to Bun is mostly because of its support of Typescript out-of-the-box.

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

alhamda commented 8 months ago

i experienced the same thing, did you fix it ?

minh5micron commented 8 months ago

Unfortunately, we had to switch back to Node + Typescript. We can only suspect that Postgresjs might be the main reason of this instability.

github-actions[bot] commented 3 months ago

This issue is stale and may be closed due to inactivity. If you're still running into this, please leave a comment.

bnussman commented 2 months ago

I am unable to connect to my PostgreSQL database when using a ca cert

import postgres from "postgres";
import { DB_CA, DB_URL } from "./constants";
import type { ConnectionOptions } from 'node:tls';

const options: ConnectionOptions = {
  ca: DB_CA,
};

const queryClient = postgres(DB_URL, options);

The same code works on 1.1.4, but not on latest

Jarred-Sumner commented 2 months ago

@bnussman are you getting a connection timeout? what's the error you're seeing? is there a chance you could let us know what type of certificate is in use? or if it's using a managed postgres instance, what provider? it would help us if you could provide some way for us to reproduce the authentication mechanism

bnussman commented 2 months ago

@Jarred-Sumner Yeah, a timeout using a managed Postgres (v13.2) database on Linode.

Error: write CONNECT_TIMEOUT lin-1010-21-pgsql-primary.servers.linodedb.net:5432
    at connectTimedOut (/home/banks/Development/beep/apinext/node_modules/postgres/src/connection.js:257:20)
    at done (/home/banks/Development/beep/apinext/node_modules/postgres/src/connection.js:1033:8)

I also get this error when trying with a different client

Error: Connection terminated unexpectedly
     at <anonymous> (/home/banks/Development/beep/node_modules/pg/lib/client.js:132:77)
     at onceWrapper (node:events:37:21)
     at emit (node:events:201:48)
     at <anonymous> (/home/banks/Development/beep/node_modules/pg/lib/connection.js:63:12)
     at emit (node:events:201:48)
     at emitCloseNT (node:stream:1433:25)
     at processTicksAndRejections (:12:39)

Working on gathering more detail. Will update shortly

nektro commented 1 month ago

this may have been fixed by https://github.com/oven-sh/bun/pull/11322

if this is still affecting you in the latest version of Bun (you may need to run bun upgrade) please leave a comment and let us know. for now we're going to mark this as closed.