-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(postgres-driver): handle connection termination errors gracefully #10277
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,7 +132,7 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre | |
| ...config | ||
| }); | ||
| this.pool.on('error', (err) => { | ||
| console.log(`Unexpected error on idle client: ${err.stack || err}`); // TODO | ||
| this.databasePoolError(err); | ||
| }); | ||
| this.config = <Partial<Config>>{ | ||
| ...this.getInitialConfiguration(dataSource), | ||
|
|
@@ -298,6 +298,13 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre | |
|
|
||
| const conn = await this.pool.connect(); | ||
|
|
||
| // Attach error handler to the client to prevent unhandled error events | ||
| // from crashing the process when connections are terminated unexpectedly. | ||
| // See: https://github.com/brianc/node-postgres/issues/2112 | ||
| conn.on('error', (err) => { | ||
| this.databasePoolError(err); | ||
| }); | ||
|
|
||
| try { | ||
| await this.prepareConnection(conn); | ||
|
|
||
|
|
@@ -342,6 +349,13 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre | |
|
|
||
| const conn = await this.pool.connect(); | ||
|
|
||
| // Attach error handler to the client to prevent unhandled error events | ||
| // from crashing the process when connections are terminated unexpectedly. | ||
| // See: https://github.com/brianc/node-postgres/issues/2112 | ||
| conn.on('error', (err) => { | ||
| this.databasePoolError(err); | ||
| }); | ||
|
Comment on lines
+355
to
+357
|
||
|
|
||
| try { | ||
| await this.prepareConnection(conn); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -99,7 +99,7 @@ export class QuestDriver<Config extends QuestDriverConfiguration = QuestDriverCo | |||||
| ...config | ||||||
| }); | ||||||
| this.pool.on('error', (err) => { | ||||||
| console.log(`Unexpected error on idle client: ${err.stack || err}`); | ||||||
| this.databasePoolError(err); | ||||||
| }); | ||||||
| this.config = <Partial<Config>>{ | ||||||
| ...this.getInitialConfiguration(), | ||||||
|
|
@@ -144,6 +144,13 @@ export class QuestDriver<Config extends QuestDriverConfiguration = QuestDriverCo | |||||
| private async queryResponse(query: string, values: unknown[]) { | ||||||
| const conn = await this.pool.connect(); | ||||||
|
|
||||||
| // Attach error handler to the client to prevent unhandled error events | ||||||
| // from crashing the process when connections are terminated unexpectedly. | ||||||
| // See: https://github.com/brianc/node-postgres/issues/2112 | ||||||
| conn.on('error', (err) => { | ||||||
|
||||||
| conn.on('error', (err) => { | |
| conn.once('error', (err) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error listener is being attached each time a connection is acquired from the pool, but it's never removed. When connections are reused from the pool, this will result in multiple identical error listeners accumulating on the same connection object, causing a memory leak. Consider using
conn.once('error', ...)instead ofconn.on('error', ...)to ensure the listener is automatically removed after the first error, or explicitly remove the listener in the finally block or release callback.