Skip to content

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

Closed

Description

@IbrahimHafez1

Issue description

DbQueryResultCache.remove() releases its internally created query runner only when every deletion succeeds. A rejected DELETE bypasses release and can exhaust the connection pool. This is a remaining failure-path gap after earlier cache cleanup fixes.

Expected Behavior

Return an owned connection after removal fails, preserve the deletion error, and keep caller-supplied runners under caller ownership. Any other deletions already started on the runner must settle before the connection is returned to the pool.

Actual Behavior

await Promise.all(...) rejects before the release statement. A straightforward finally alone also needs care: Promise.all() rejects as soon as one operation fails, while other operations can still be using that runner.

Steps to reproduce

Use a disposable PostgreSQL database: this script deliberately removes its test cache table to make the deletion fail.

import { DataSource } from 'typeorm'

const source = new DataSource({
  type: 'postgres', url: process.env.POSTGRES_URL,
  cache: true, synchronize: true,
  extra: { max: 1, connectionTimeoutMillis: 1000 },
})
await source.initialize()
try {
  await source.query('DROP TABLE "query-result-cache"')
  await source.queryResultCache!.remove(['first', 'second'])
    .catch(error => console.log(error.message)) // Missing-table error.
  await source.query('SELECT 1') // Currently times out acquiring a connection.
} finally {
  await source.destroy()
}

This is not limited to a missing table: a separate reproduction holds a conflicting table lock and causes a statement timeout. After releasing that lock, PostgreSQL is healthy but the cache removal connection remains checked out.

My Environment

Current master c64a1f052fc39f6688b6b73b83d065d7147ba8bb; Node.js 22.17.1; PostgreSQL 15.6. The affected cache implementation is shared across relational drivers.

Additional Context / proposed fix

Protect construction/execution of the deletion operations with cleanup. On rejection, settle the already-started operations, attempt release of an internally created runner, and rethrow the original deletion error even if cleanup also fails. After successful deletion, release an internally created runner normally and surface any cleanup error. Caller-supplied runners remain caller-owned. Keep the existing per-identifier SQL and parallel scheduling on success. This adds no queries, parameter-limit changes, or new dependencies; the extra settlement pass is restricted to failures.

Related history: #3370/#4866 reported earlier cache connection problems; merged #8969 added release on successful removal. Merged #12797 protects clear()/getFromCache(), and #12545 protects storeInCache(). None covers this remaining remove() error path.

Regression tests cover empty/single/multiple identifiers, successful and rejected operations, supplied-runner ownership, a deferred second deletion after the first fails, cleanup errors, and real PostgreSQL failure followed by a successful query. The owned-error and pending-deletion assertions fail on unchanged master. With the fix, all 19 focused cache tests pass, including the existing query-builder cache suite. The broader unit and functional-cache run has 164 passing and 4 pending tests.

Are you willing to resolve this issue by submitting a Pull Request?

Yes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions