Skip to content

fix: release database cache runners after failed removals - #12938

Merged
alumni merged 3 commits into
typeorm:masterfrom
IbrahimHafez1:codex/cache-remove-error-release
Oct 21, 2026
Merged

alumni merged 3 commits into
typeorm:masterfrom
IbrahimHafez1:codex/cache-remove-error-release

Conversation

@IbrahimHafez1

@IbrahimHafez1 IbrahimHafez1 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Description of change

Fixes #12935.

DbQueryResultCache.remove() skips releasing its owned query runner when a deletion rejects. If a deletion fails, first settle any other deletions already using the same runner, attempt cleanup, then rethrow the original error. A cleanup failure cannot replace that deletion error; if all deletions succeeded, a cleanup failure is still surfaced. Caller-supplied runners are never released.

Successful removal keeps the same per-identifier SQL and parallel scheduling. There are no additional queries, batching/parameter-limit changes, or new dependencies. The extra settlement pass only runs after a failure. Waiting for already-started deletions avoids returning a still-busy connection to the pool.

This closes the remaining removal error path after #8969's successful-removal cleanup. #12797/#12545 protect other database-cache methods.

Validation (Node.js 22.17.1, PostgreSQL 15.6):

  • On unchanged master, new tests fail for the unreleased runner and early settlement while a second deletion is pending.
  • Removal tests cover empty/single/multiple identifiers, both runner owners, deferred deletion cleanup, and synchronous/asynchronous cleanup errors after successful or failed deletions.
  • Real PostgreSQL failure/reuse tests and the existing query-builder cache suite pass. The test's single-connection settings override local pool defaults without discarding other connection options.
  • Full unit and functional-cache run: 164 passing, 4 pending. After the final test-fixture corrections, the source removal-unit/functional-cache run has 25 passing, 4 pending; the custom-schema removal run has 15 passing. The custom-schema cases fail before qualifying the dropped table. An intentionally failing second connection also exits without forced Mocha shutdown, verifying earlier initialized connections are closed by teardown.
  • Complete TypeScript compilation, focused ESLint (no errors), formatting and diff checks pass. Other database servers were not run locally; CI supplies the broader driver matrix.

Developed with AI assistance; source, prior fixes, regression behavior, and final diff reviewed.

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: restores resource cleanup without a new API/configuration

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

pkg-pr-new Bot commented Oct 5, 2026 •

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

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

commit: 674edcf

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

qodo-free-for-open-source-projects Bot commented Oct 5, 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. Cache tests fail with a custom schema ✓ Resolved
Description
The test drops "query-result-cache" without qualifying it with the configured schema, although
DbQueryResultCache creates its table using that schema. When a PostgreSQL test connection
specifies a schema outside the search path, the drop fails before either removal assertion runs.
Code

packages/typeorm/test/functional/cache/db-cache-remove.test.ts[45]

+                    await dataSource.query('DROP TABLE "query-result-cache"')
Evidence
The test retains connection configuration, including schema; the cache constructs its table name
from that schema, but the new DROP statement names only the unqualified table.

packages/typeorm/test/functional/cache/db-cache-remove.test.ts[14-20]
packages/typeorm/test/functional/cache/db-cache-remove.test.ts[41-45]
packages/typeorm/test/utils/test-utils.ts[244-259]
packages/typeorm/src/cache/DbQueryResultCache.ts[30-48]
packages/typeorm/src/driver/postgres/PostgresDriver.ts[1075-1082]

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 regression test drops an unqualified cache table, so PostgreSQL connections configured with a non-default schema can fail before testing removal.
## Fix Focus Areas
- packages/typeorm/test/functional/cache/db-cache-remove.test.ts[45-45]
## Recommended Fix
Build and escape the cache table name using the test connection's configured schema before dropping it.

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


2. A failed test setup can leave pools open ✓ Resolved
Description
The before hook assigns dataSources only after every concurrent initialization succeeds, while
the after hook can close only the assigned array. If one of multiple configured PostgreSQL
connections fails to initialize after another succeeds, the successful connection remains open and
the test process can stay alive.
Code

packages/typeorm/test/functional/cache/db-cache-remove.test.ts[15]

+        dataSources = await Promise.all(
Evidence
The setup helper can return multiple PostgreSQL configurations. Promise.all rejects without
assigning its result when any initialization fails, and the cleanup helper does nothing when passed
an unassigned array.

packages/typeorm/test/functional/cache/db-cache-remove.test.ts[12-39]
packages/typeorm/test/utils/test-utils.ts[236-249]
packages/typeorm/test/utils/test-utils.ts[489-502]

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

## Issue description
Concurrent test initialization can leave successful connections open when another initialization fails before `dataSources` is assigned.
## Fix Focus Areas
- packages/typeorm/test/functional/cache/db-cache-remove.test.ts[14-32]
- packages/typeorm/test/functional/cache/db-cache-remove.test.ts[39-39]
## Recommended Fix
Track created data sources before awaiting initialization. On setup failure, destroy any that initialized successfully, then rethrow the setup error.

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


3. Callers lose the original deletion error ✓ Resolved
Description
remove() rethrows a deletion error after settling pending queries, but an error from the awaited
release() in finally replaces it. When both deletion and runner cleanup fail, callers receive
the cleanup error instead of the database error that caused removal to fail.
Code

packages/typeorm/src/cache/DbQueryResultCache.ts[R360-362]

+        } finally {
+            if (!queryRunner) {
+                await _queryRunner.release()
Evidence
The new catch rethrows the deletion failure, while the new finally awaits release afterward; a
rejection from that await supersedes the pending throw. PostgreSQL release invokes a pool callback,
so cleanup is an operation that can itself fail.

packages/typeorm/src/cache/DbQueryResultCache.ts[355-363]
packages/typeorm/src/driver/postgres/PostgresQueryRunner.ts[140-165]

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 failing runner release can override the original deletion error rethrown by `remove()`.
## Fix Focus Areas
- packages/typeorm/src/cache/DbQueryResultCache.ts[355-363]
- packages/typeorm/test/unit/cache/db-query-result-cache.test.ts[36-53]
## Recommended Fix
Preserve the deletion error if release also fails, while still surfacing a release failure when deletion succeeded. Add a test in which both operations reject.

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


View review recommended (1)
4. Cache tests can run with the wrong pool ✓ Resolved
Description
The functional test supplies poolSize and extra through driverSpecific, but
setupTestingConnections() lets corresponding values from the local configuration overwrite them.
If that configuration defines either value, the test no longer runs with its intended
single-connection pool or one-second connection timeout, making its reuse check
environment-dependent.
Code

packages/typeorm/test/functional/cache/db-cache-remove.test.ts[R17-20]

+            driverSpecific: {
+                poolSize: 1,
+                extra: { max: 1, connectionTimeoutMillis: 1000 },
+            },
Evidence
The test requests its pool settings through driverSpecific, but the helper merges that object
before the configuration-derived options. The PostgreSQL driver then uses the resulting poolSize
and extra values to construct its pool.

packages/typeorm/test/functional/cache/db-cache-remove.test.ts[13-21]
packages/typeorm/test/utils/test-utils.ts[244-264]
packages/typeorm/src/driver/postgres/PostgresDriver.ts[1734-1753]

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

## Issue description
Local PostgreSQL configuration can override the pool settings required by the removal regression test.
## Fix Focus Areas
- packages/typeorm/test/functional/cache/db-cache-remove.test.ts[13-21]
- packages/typeorm/test/utils/test-utils.ts[254-264]
## Recommended Fix
Ensure the test-specific pool size and timeout take precedence over local configuration, without discarding other PostgreSQL connection settings.

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


Grey Divider

  • 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

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

LGTM

@Cprakhar
Cprakhar requested review from alumni and gioboa October 5, 2026 17:48

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

💯

@alumni
alumni merged commit f372541 into typeorm:master Oct 21, 2026
49 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Oct 21, 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.

Database cache remove() leaks its query runner after a deletion fails

4 participants