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

fix: prevent reuse of broken connections in postgres pool #7792

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ export class AuroraDataApiPostgresQueryRunner extends PostgresQueryRunnerWrapper
*/
protected databaseConnectionPromise: Promise<any>;

/**
* Special callback provided by a driver used to release a created connection.
*/
protected releaseCallback: Function;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
Expand Down
33 changes: 22 additions & 11 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
/**
* Special callback provided by a driver used to release a created connection.
*/
protected releaseCallback: Function;
protected releaseCallback?: (err: any) => void;

// -------------------------------------------------------------------------
// Constructor
Expand Down Expand Up @@ -84,7 +84,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
this.driver.connectedQueryRunners.push(this);
this.databaseConnection = connection;

const onErrorCallback = () => this.release();
const onErrorCallback = (err: Error) => this.releasePostgresConnection(err);
this.releaseCallback = () => {
this.databaseConnection.removeListener("error", onErrorCallback);
release();
Expand All @@ -99,7 +99,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
this.driver.connectedQueryRunners.push(this);
this.databaseConnection = connection;

const onErrorCallback = () => this.release();
const onErrorCallback = (err: Error) => this.releasePostgresConnection(err);
this.releaseCallback = () => {
this.databaseConnection.removeListener("error", onErrorCallback);
release();
Expand All @@ -114,22 +114,33 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
}

/**
* Releases used database connection.
* You cannot use query runner methods once its released.
* Release a connection back to the pool, optionally specifying an Error to release with.
* Per pg-pool documentation this will prevent the pool from re-using the broken connection.
*/
release(): Promise<void> {
private async releasePostgresConnection(err?: Error) {
if (this.isReleased) {
return Promise.resolve();
return
}

this.isReleased = true;
if (this.releaseCallback)
this.releaseCallback();
if (this.releaseCallback) {
this.releaseCallback(err);
this.releaseCallback = undefined
}

const index = this.driver.connectedQueryRunners.indexOf(this);
if (index !== -1) this.driver.connectedQueryRunners.splice(index, 1);

return Promise.resolve();
if (index !== -1) {
this.driver.connectedQueryRunners.splice(index, 1);
}
}

/**
* Releases used database connection.
* You cannot use query runner methods once its released.
*/
release(): Promise<void> {
return this.releasePostgresConnection();
}

/**
Expand Down