drizzle-team / drizzle-team/drizzle-orm
[BUG]: Unhandled `error` event on `pg.Client` crashes process
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [X] 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.
Contributor guide
Assessment
This issue has not been assessed yet.