Skip to content

Fix unbounded memory growth in DocCache#2

Merged
mattpodwysocki merged 1 commit intomainfrom
fix-unbounded-cache
Mar 6, 2026
Merged

Fix unbounded memory growth in DocCache#2
mattpodwysocki merged 1 commit intomainfrom
fix-unbounded-cache

Conversation

@mattpodwysocki
Copy link
Contributor

@mattpodwysocki mattpodwysocki commented Mar 6, 2026

Summary

The DocCache class used an unconstrained Map with no limits on the number of entries, size per entry, or total memory used. A client providing many unique URLs (e.g. with arbitrary query parameters) could grow the cache without bound, eventually causing memory exhaustion.

  • LRU eviction: cap at 512 entries; oldest entry evicted when full
  • Per-entry size cap: entries larger than 2 MB are silently skipped and not cached
  • Total size cap: total cached content capped at 50 MB; oldest entries evicted to make room
  • URL key normalization: query parameters and hash fragments are stripped before using a URL as a cache key, so https://docs.mapbox.com/page?bust=1 and https://docs.mapbox.com/page?bust=2 map to the same cache entry

Test plan

  • 22 new unit tests in test/utils/docCache.test.ts covering all invariants
  • All existing tests still pass
  • CI green (lint, format, spellcheck, build, tests)

🤖 Generated with Claude Code

@mattpodwysocki mattpodwysocki requested review from a team as code owners March 6, 2026 20:36
jussi-sa
jussi-sa previously approved these changes Mar 6, 2026
@mattpodwysocki mattpodwysocki dismissed jussi-sa’s stale review March 6, 2026 20:52

The merge-base changed after approval.

@mattpodwysocki mattpodwysocki requested a review from jussi-sa March 6, 2026 20:54
jussi-sa
jussi-sa previously approved these changes Mar 6, 2026
@jussi-sa jussi-sa dismissed their stale review March 6, 2026 20:58

stale review

ctufts
ctufts previously approved these changes Mar 6, 2026
@mattpodwysocki mattpodwysocki dismissed ctufts’s stale review March 6, 2026 20:58

The merge-base changed after approval.

jussi-sa
jussi-sa previously approved these changes Mar 6, 2026
@mattpodwysocki mattpodwysocki dismissed jussi-sa’s stale review March 6, 2026 21:01

The merge-base changed after approval.

jussi-sa
jussi-sa previously approved these changes Mar 6, 2026
@mattpodwysocki mattpodwysocki dismissed jussi-sa’s stale review March 6, 2026 21:03

The merge-base changed after approval.

ctufts
ctufts previously approved these changes Mar 6, 2026
jussi-sa
jussi-sa previously approved these changes Mar 6, 2026
- Add 512-entry LRU eviction cap
- Add 2 MB per-entry size limit (oversized responses silently skipped)
- Add 50 MB total cache size cap with oldest-first eviction
- Normalize cache keys: strip query params and hash fragments to prevent
  cache-busting strings from inflating entry count
- Add 22 unit tests covering all new cache invariants

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mattpodwysocki mattpodwysocki dismissed stale reviews from jussi-sa and ctufts via fc70b68 March 6, 2026 21:06
@mattpodwysocki mattpodwysocki merged commit 1cf0ee4 into main Mar 6, 2026
1 check passed
@mattpodwysocki mattpodwysocki deleted the fix-unbounded-cache branch March 6, 2026 21:09
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.

3 participants