Skip to content

fix: L1 crash when serialize: false with L2 cache#113

Open
thisisnkc wants to merge 2 commits intoJulien-R44:mainfrom
thisisnkc:fix/l1-serialize-false-l2-refill-crash
Open

fix: L1 crash when serialize: false with L2 cache#113
thisisnkc wants to merge 2 commits intoJulien-R44:mainfrom
thisisnkc:fix/l1-serialize-false-l2-refill-crash

Conversation

@thisisnkc
Copy link
Copy Markdown
Contributor

Problem

When using a two-tier cache with serialize: false on the L1 memory driver, reading a value that exists only in L2 causes a crash on subsequent L1 reads:

TypeError: Cannot read properties of undefined (reading 'deserialize')

Root cause: During L2-to-L1 refill, the code unconditionally called entry.serialize(), storing a serialized string into L1. Since L1 has no deserializer when serialize: false, the next read from L1 crashes in CacheEntry.fromDriver().

This affected three code paths:

  • cache.get() - L2 hit refill to L1
  • cache.get() - graced L2 value refill to L1
  • cache.getOrSet() via TwoTierHandler.#returnRemoteCacheValue()

Fix

  • Extracted toRaw() method from CacheEntry.serialize() to return the plain object without serialization
  • Added prepareForL1(entry) helper on CacheStack that returns entry.serialize() or entry.toRaw() based on the serializeL1 option
  • Replaced all three L2-to-L1 refill paths to use prepareForL1() instead of unconditional serialize()

Tests added

  • L2 hit should correctly refill L1 when serialize is falsecache.get() path: L2 hit refills L1, subsequent L1 read works and stores an object (not a string)
  • getOrSet L2 hit should correctly refill L1 when serialize is falsecache.getOrSet() path: L2 hit refills L1, subsequent call reads from L1 without error

Closes #111

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 27, 2026

🦋 Changeset detected

Latest commit: e499df6

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

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.

serialize: false on L1 memory seems to incorrectly store serialized string to L2 on refill

1 participant