Skip to content

Commit

Permalink
fix: closing pool incorrectly works on Postgres (#7596)
Browse files Browse the repository at this point in the history
* fix: Promises never get resolved in specific cases (#6958)

* Limit the number of query runners to remove when releasing one of them
* Add while loop to release all items on the connected query runners list

Closes #6958

* test: Create test for github issue #6958
  • Loading branch information
PauloRSF committed May 10, 2021
1 parent 1b8b7f1 commit 1310c97
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,10 @@ export class PostgresDriver implements Driver {
* Closes connection pool.
*/
protected async closePool(pool: any): Promise<void> {
await Promise.all(this.connectedQueryRunners.map(queryRunner => queryRunner.release()));
while (this.connectedQueryRunners.length) {
await this.connectedQueryRunners[0].release();
}

return new Promise<void>((ok, fail) => {
pool.end((err: any) => err ? fail(err) : ok());
});
Expand Down
2 changes: 1 addition & 1 deletion src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
this.releaseCallback();

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

return Promise.resolve();
}
Expand Down
28 changes: 28 additions & 0 deletions test/github-issues/6958/issue-6958.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "reflect-metadata";
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Connection } from "../../../src/connection/Connection";
import { PostgresDriver } from "../../../src/driver/postgres/PostgresDriver";
import { expect } from "chai";

describe("github issues > #6958 Promises never get resolved in specific cases", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
enabledDrivers: ["postgres"],
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should release all used query runners upon disconnection", () => Promise.all(connections.map(async connection => {
const runner1 = connection.createQueryRunner();
await runner1.query("SELECT 1 as foo;"); // dummy query to ensure that a database connection is established
const runner2 = connection.createQueryRunner();
await runner2.query("SELECT 2 as foo;");

await connection.close();

expect(runner1.isReleased).to.be.true;
expect(runner2.isReleased).to.be.true;
expect((connection.driver as PostgresDriver).connectedQueryRunners.length).to.equal(0);
})));
});

0 comments on commit 1310c97

Please sign in to comment.