Skip to content

fix(cockroachdb): release all query runners before closing pool - #12891

Merged
alumni merged 3 commits into
typeorm:masterfrom
IbrahimHafez1:fix/cockroach-release-all-query-runners
Oct 7, 2026
Merged

alumni merged 3 commits into
typeorm:masterfrom
IbrahimHafez1:fix/cockroach-release-all-query-runners

Conversation

@IbrahimHafez1

Copy link
Copy Markdown
Contributor

Description of change

Fixes #12890.

Calling DataSource.destroy() with two connected CockroachDB query runners hangs even when both queries have finished. closePool() maps over connectedQueryRunners, but each release() splices itself from that array synchronously. The second runner is skipped, so pool.end() waits for a connection that shutdown never returns.

Use the same release loop already adopted by PostgresDriver in #7596. Extend that PR's existing #6958 regression test to run against CockroachDB as well. This keeps the current policy of releasing tracked runners before closing the pool; it adds no shutdown options or public API changes.

Validation

Tested against live CockroachDB v25.2.23 in Docker, using Node.js v22.17.1, pg 8.23.0 and pg-pool 3.14.0:

  • Before: the extended Promises never get resolved in specific cases #6958 test times out after 5 seconds in destroy(); teardown then reports that the pool was already ending.
  • After: the same regression passes (1 passing).
  • Additional live checks pass with 0, 1, 2, 3 and 8 connected runners, an already-released runner, and separate primary/replica pools pointing to the local server. Every tracked runner is released and shutdown completes.
  • TypeScript compilation and pnpm run package pass.
  • ESLint on both changed files: 0 errors (100 existing driver warnings). Prettier and git diff --check pass.

The full CockroachDB suite was started locally but stopped because schema setup was progressing slowly. No failures had been reported at that point; this is not a full-suite pass. The full multi-driver matrix remains for CI.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword
  • There are new or updated tests validating the change
  • Documentation has been updated to reflect this change — N/A: internal bug fix, no documented API change

@github-actions github-actions Bot added the linked-issue PR references an issue label Sep 20, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12891
    

commit: c27e761

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Shutdown spins on a released runner 🐞 Bug ☼ Reliability
Description
closePool() repeatedly releases the first tracked runner until it disappears, but release() does
nothing when that runner is already marked released. If release() runs while connect() is
pending, the connection callback can subsequently add that runner to the tracking array; destroy()
then loops without reaching pool.end().
Code

packages/typeorm/src/driver/cockroachdb/CockroachDriver.ts[R1221-1222]

+        while (this.connectedQueryRunners.length) {
+            await this.connectedQueryRunners[0].release()
Evidence
releaseConnection() sets isReleased and removes the runner only if it is already tracked. Both
connect() continuations subsequently push the runner without checking that flag. Once tracked,
further releases return immediately, so the newly added loop retains the same first element
indefinitely.

packages/typeorm/src/driver/cockroachdb/CockroachQueryRunner.ts[102-115]
packages/typeorm/src/driver/cockroachdb/CockroachQueryRunner.ts[125-136]
packages/typeorm/src/driver/cockroachdb/CockroachQueryRunner.ts[161-176]
packages/typeorm/src/driver/cockroachdb/CockroachDriver.ts[1220-1227]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A runner released while its connection is pending can later enter `connectedQueryRunners` already marked released. The new shutdown loop cannot remove it and spins indefinitely.
## Fix Focus Areas
- packages/typeorm/src/driver/cockroachdb/CockroachQueryRunner.ts[102-176]
- packages/typeorm/src/driver/cockroachdb/CockroachDriver.ts[1220-1227]
## Recommended Fix
Make connection completion honor an earlier release: return the acquired connection to its pool instead of registering an already-released runner. Ensure the shutdown loop can make progress for any tracked runner, and add a regression test for release while connection acquisition is pending.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Tip of the day
💡 Did you know, you can route each severity your way: inline, summary, both, or drop

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@IbrahimHafez1

Copy link
Copy Markdown
Contributor Author

Following up on this shutdown fix when someone has time to review. It brings CockroachDB's runner-release loop in line with PostgresDriver: releasing a runner mutates the array being iterated, so the current map() can skip a runner and leave destroy() waiting indefinitely.

The regression reproduces the hang against CockroachDB and passes with the fix. CI on the current PR head is green, including the CockroachDB jobs, and GitHub reports no merge conflicts, though the branch is behind master. Is there anything else you would like covered before review?

@Cprakhar Cprakhar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@alumni
alumni merged commit 3dddc4b into typeorm:master Oct 7, 2026
46 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linked-issue PR references an issue

Development

Successfully merging this pull request may close these issues.

CockroachDB: DataSource.destroy() hangs with multiple connected query runners

3 participants