Skip to content

fix: only disconnect the stores that were created in disconnectAll() - #132

Open
nbourdin wants to merge 1 commit into
Julien-R44:mainfrom
nbourdin:fix/disconnect-all-unused-stores
Open

nbourdin wants to merge 1 commit into
Julien-R44:mainfrom
nbourdin:fix/disconnect-all-unused-stores

Conversation

@nbourdin

Copy link
Copy Markdown

Problem

disconnectAll() calls this.use(cache) on every configured store:

async disconnectAll(): Promise<void> {
await Promise.all(Object.keys(this.#stores).map(cache => this.use(cache).disconnect()))
}

use() creates a store's provider the first time it is called (#createProvider() → new CacheStack() → L1/L2 driver factories and new Bus(), which subscribes to the bus channel). So disconnectAll() creates every store the application never used, with its drivers and bus, only to disconnect it right away. This contradicts the method's doc comment: "Disconnect all cache connections created by the manager".

Different from #122: that one creates a stray client for a store that is used, this one creates the whole store when it is not.

@adonisjs/cache calls disconnectAll() when the application shuts down. In an AdonisJS app that declares a Redis store next to a memory default, every Ace command goes through this on exit, including commands that never touch the Redis store.

Reproduction

import { BentoCache, bentostore } from 'bentocache'
import { memoryDriver } from 'bentocache/drivers/memory'
import { redisDriver, redisBusDriver } from 'bentocache/drivers/redis'

const connection = { host: '127.0.0.1', port: 1 } // nothing listens here

const bento = new BentoCache({
  default: 'memory',
  stores: {
    memory: bentostore().useL1Layer(memoryDriver({})),
    // Never used
    redis: bentostore()
      .useL1Layer(memoryDriver({}))
      .useL2Layer(redisDriver({ connection }))
      .useBus(redisBusDriver({ connection })),
  },
})

await bento.use('memory').set({ key: 'foo', value: 'bar' })
await bento.disconnectAll()

Expected: nothing happens for the redis store, since it was never created.

Observed (Node 22):

bentocache 1.6.1, @boringnode/bus 0.9.2, ioredis 5.11.1 main (4fcb763), @boringnode/bus 0.10.0, ioredis 5.11.1 or 6.0.0
Unused redis store created yes yes
TCP connections to a reachable Redis (counted with a TCP proxy) 1 0
Redis unreachable [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:1 printed 21 times. After about 10 s, disconnectAll() rejects with MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). A second MaxRetriesPerRequestError is unhandled and the process exits with code 1. disconnectAll() resolves
Logged at error level, whether Redis is reachable or not none failed to subscribe to bus channel (channel: bentocache.notifications:redis)

On main the connection and the rejection are gone thanks to @boringnode/bus 0.10, which disconnects a connection that is not ready yet instead of calling quit(), and to #129, which catches the subscription failure. The root cause is still there: the unused store is created on every disconnectAll(), and its bus subscription fails and is logged as an error. In an AdonisJS app that error goes to the app logger on every command exit.

Change

disconnectAll() now disconnects the providers in #driversCache instead of calling use() on every configured store name:

   async disconnectAll(): Promise<void> {
-    await Promise.all(Object.keys(this.#stores).map(cache => this.use(cache).disconnect()))
+    await Promise.all([...this.#driversCache.values()].map(cache => cache.disconnect()))
   }

Why this keeps the current behaviour for every store that was used:

  • use() is the only place that creates a provider, and it stores every provider it creates in #driversCache. The default-store shortcuts (get(), set(), namespace(), and so on) and clearAll() all go through use().
  • A store that is not in #driversCache has no driver, client or bus to close.
  • Each provider is disconnected with the same Cache.disconnect() as before (L1, L2 and bus), and the providers stay in #driversCache, as they did before.

A patch changeset is included.

Test

disconnectAll does not create stores that were never used, in the Bento Cache group of tests/bento_cache.spec.ts, wraps the L1 factory of an unused memory store and counts its calls:

  • without the fix: fails with expected 1 to equal +0
  • with the fix: passes

The existing tests that call disconnectAll() after using Redis stores keep covering the disconnection of created stores.

Checked locally on Node 22:

  • pnpm typecheck, pnpm lint and pnpm format:check pass.
  • Test suite, with Redis on 6379, Valkey on 6380 and a 3-node Redis cluster on 7000-7002: 394 tests pass with the fix. Without it, only the new test fails.
  • Not run, because those services were not available locally: tests/drivers/{knex,kysely}/{mysql,postgres}.spec.ts, tests/drivers/orchid/postgres.spec.ts and tests/drivers/dynamodb.spec.ts. This change does not touch them.

Not changed

clearAll() still iterates over every configured store. Clearing has to reach persistent L2 data even when the store was not used in this process, so creating the store there is expected.

disconnectAll() called use() on every configured store, so it created
every store the application never used, along with its drivers and bus,
only to disconnect it right away. Disconnect the providers already
cached in #driversCache instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BXNob8sjkiaor7KMhLkMmg
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b677e51

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
bentocache Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T20:36:28.019795Z b677e51 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants