Skip to content

fix(cache): release query runners created by clear and getFromCache - #12797

Merged
alumni merged 3 commits into
typeorm:masterfrom
kyungseopk1m:fix/cache-release-query-runner
Aug 26, 2026
Merged

alumni merged 3 commits into
typeorm:masterfrom
kyungseopk1m:fix/cache-release-query-runner

Conversation

@kyungseopk1m

Copy link
Copy Markdown
Contributor

Description of change

DbQueryResultCache.clear and getFromCache build a query runner through getQueryRunner when the caller passes none, and never release it. The interface types the argument as optional and CacheClearCommand calls clear with no argument, so calling dataSource.queryResultCache.clear from application code (an admin endpoint, a scheduled job, a test teardown) leaks one pooled connection per call. After poolSize calls the pool is exhausted and every later query waits forever.

The fix releases the self-created runner in a finally block, the same way remove and storeInCache already do, and keeps the original return value. A runner passed in by the caller is left untouched. getFromCache has the same shape and is covered by the same change.

Verified with poolSize 2 on Postgres 16: without the fix the third argument-less clear times out once the pool is drained; with the fix the three clears complete and a following query still runs. Added a test that spies on createQueryRunner and asserts the self-created runner is released by clear and getFromCache, that a runner passed in by the caller is not released, and that the self-created runner is still released when the underlying operation throws.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword: N/A (found while reading the cache code, no existing issue)
  • There are new or updated tests validating the change (test/functional/cache/query-runner-release.test.ts)
  • Documentation has been updated to reflect this change: N/A (internal behavior, no API surface change)

DbQueryResultCache.clear and getFromCache create a query runner through
getQueryRunner when the caller passes none, but never release it. Calling
dataSource.queryResultCache.clear repeatedly leaks a pooled connection each
time (its interface argument is optional, and CacheClearCommand invokes it
with no argument), and after poolSize calls every later query waits forever.

Release the self-created runner in a finally block, matching the existing
remove and storeInCache methods, and keep the original return value.
@github-actions github-actions Bot added the needs-triage PR needs issue link triage label Aug 21, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 21, 2026 •

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

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

commit: 40d5bb1

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

qodo-free-for-open-source-projects Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

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

Grey Divider


Remediation recommended

1. as any in runner stub ✓ Resolved 📘 Rule violation ⚙ Maintainability
Description
The new functional test stubs createQueryRunner using any casts/any[], bypassing type safety
and violating the guideline to avoid type-bypass noise. This can hide real type regressions and
makes the test harder to maintain.
Code

test/functional/cache/query-runner-release.test.ts[R36-37]

+        dataSource.createQueryRunner = (...args: any[]): QueryRunner => {
+            const queryRunner: QueryRunner = (originalCreate as any)(...args)
Evidence
PR Compliance ID 4 forbids introducing any-casts/type-bypass noise. The new test overrides
createQueryRunner with (...args: any[]) and calls the original via `(originalCreate as
any)(...args), and also uses (dataSource as any)` during restore.

Rule 4: Remove AI-generated noise
test/functional/cache/query-runner-release.test.ts[36-37]
test/functional/cache/query-runner-release.test.ts[52-54]

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

## Issue description
The new test introduces `any` casts/`any[]` to monkeypatch `DataSource.createQueryRunner`, which violates the compliance requirement to avoid `any`-casts that bypass types.
## Issue Context
This is in a functional test, but it still reduces type-safety and can mask incorrect stubbing.
## Fix Focus Areas
- test/functional/cache/query-runner-release.test.ts[35-54]

ⓘ 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 start a comment with 'qodo' or '@qodo' to chat about any finding

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Type the createQueryRunner stub with ReplicationMode and undo it with
Reflect.deleteProperty rather than casting through any, and run Prettier
over the file.
Spanner rejects the built-in cache table name, so DataSource.synchronize
fails in beforeEach when the cache is enabled. The other cache suites
already exclude spanner through disabledDrivers; do the same here.

@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.

@gioboa gioboa 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.

Thanks @kyungseopk1m 🙏

@Cprakhar
Cprakhar requested review from alumni and smith-xyz August 22, 2026 12:17
@alumni
alumni merged commit 3883d76 into typeorm:master Aug 26, 2026
46 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-triage PR needs issue link triage

Development

Successfully merging this pull request may close these issues.

4 participants