Skip to content
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

[BUG]: Unhandled error event on pg.Client crashes process #3908

Open
1 task done
sybereal opened this issue Jan 6, 2025 · 1 comment · May be fixed by #3954
Open
1 task done

[BUG]: Unhandled error event on pg.Client crashes process #3908

sybereal opened this issue Jan 6, 2025 · 1 comment · May be fixed by #3954
Labels
bug Something isn't working driver/pg

Comments

@sybereal
Copy link

sybereal commented Jan 6, 2025

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.35.2

What version of drizzle-kit are you using?

0.26.2

Other packages

No response

Describe the Bug

If a pg.Client receives an ErrorMessage from the PostgreSQL server, it will eventually emit an error event.

One could argue that installing such a handler would the task of whoever provides the client to Drizzle. However, that only works with individual clients. When passing a pg.Pool to Drizzle, the caller no longer has control of the individual client instances and therefore cannot easily do so, at least not without hacks like overriding the method. Unfortunately, when acquiring a connection from a pg.Pool, Drizzle does not install a listener for the error event itself. Due to Node's special handling of the error event, this crashes the entire process when it does occur.

In this case, what happened was that someone on the server invoked pg_terminate_backend() for debugging purposes. It should thus be possible to reproduce this error by, e.g., sleeping (on the client) inside a transaction and then using pg_terminate_backend() to kill it from the server side.

Edit to add: Since I didn't clarify above, we are using node-postgres, i.e., the pg and pg-pool packages.

@sybereal sybereal added the bug Something isn't working label Jan 6, 2025
@sybereal
Copy link
Author

sybereal commented Jan 9, 2025

To add some information: node-postgres documentation mentions that while there are outstanding promises for connect()/query()/end() operations, errors are caught and transformed into promise rejections.

I believe the issue occurred as follows: A transaction() callback was suspended awaiting an unrelated promise, i.e., not in a query, and that's when the error was raised and why it wasn't caught automatically. I updated the issue body to clarify that for the repro instructions.

@sybereal sybereal linked a pull request Jan 15, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working driver/pg
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants