Skip to content

fix: bound keyurl caches with FIFO eviction to prevent memory leak#3

Open
luchobonatti wants to merge 1 commit intomainfrom
fix/336-bounded-keyurlcache
Open

fix: bound keyurl caches with FIFO eviction to prevent memory leak#3
luchobonatti wants to merge 1 commit intomainfrom
fix/336-bounded-keyurlcache

Conversation

@luchobonatti
Copy link
Copy Markdown
Member

Closes zama-ai#336

Summary

  • Bound keyurlCache in networkV1.ts (v1 only) and privateKeyurlCache in AbstractRelayerProvider.ts (v1 + v2) to MAX_KEYURL_CACHE_SIZE = 16 entries
  • FIFO eviction via Map insertion order — when the 17th unique relayer URL is inserted, the oldest entry is dropped
  • Zero new dependencies — uses Map.keys().next().value! which is O(1) and guaranteed by ES2015+
  • Export _clearKeyurlCache() from networkV1.ts for test isolation, mirroring the existing _clearTFHEPkeParamsCache() pattern

Changes

File Change
src/relayer-provider/v1/networkV1.ts Record → bounded Map, MAX_KEYURL_CACHE_SIZE = 16, FIFO eviction, export _clearKeyurlCache()
src/relayer-provider/AbstractRelayerProvider.ts MAX_KEYURL_CACHE_SIZE = 16, FIFO eviction block before privateKeyurlCache.set()
src/relayer-provider/v1/networkV1.test.ts Cache hit test + FIFO eviction test (17 URLs), beforeEach/afterEach isolation hooks
src/relayer-provider/v2/RelayerV2Provider_keyurl.test.ts FIFO eviction test (17 providers)

Test plan

  • tsc --noEmit passes
  • 23 tests pass across both affected suites
  • Full suite: 1376/1384 tests pass, 8 skipped (pre-existing), 0 failures

Copilot AI review requested due to automatic review settings April 9, 2026 15:24
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Bounds relayer key URL/TFHE parameter caches to prevent unbounded growth (memory leak) by introducing FIFO eviction at a fixed maximum size, plus adds tests to validate cache hits and eviction behavior.

Changes:

  • Replace the v1 keyurlCache Record with a bounded Map (max 16) using FIFO eviction and add _clearKeyurlCache() for test isolation.
  • Add FIFO eviction (max 16) to the shared privateKeyurlCache used by AbstractRelayerProvider (applies to v1 + v2).
  • Add/extend tests for cache hits and FIFO eviction in both v1 and v2 suites.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/relayer-provider/v2/RelayerV2Provider_keyurl.test.ts Adds a FIFO eviction test validating the 17th unique provider triggers eviction and a refetch causes an additional fetch.
src/relayer-provider/v1/networkV1.ts Introduces bounded Map cache with FIFO eviction and exports _clearKeyurlCache() for tests.
src/relayer-provider/v1/networkV1.test.ts Adds cache-hit and FIFO-eviction tests and clears cache/routes between tests for isolation.
src/relayer-provider/AbstractRelayerProvider.ts Adds FIFO eviction to the promise cache for TFHEPkeParams to bound memory usage across relayer URLs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +31
// Bounded FIFO cache. Mirrors MAX_KEYURL_CACHE_SIZE in AbstractRelayerProvider.ts.
const MAX_KEYURL_CACHE_SIZE = 16;
const keyurlCache = new Map<string, CachedKey>();
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAX_KEYURL_CACHE_SIZE is duplicated here and in AbstractRelayerProvider.ts (kept in sync via comments). Consider extracting it to a shared constant (single source of truth) to avoid the two caches silently diverging in size in future edits.

Copilot uses AI. Check for mistakes.
Comment on lines +53 to +54
// Bounded to MAX_KEYURL_CACHE_SIZE with FIFO eviction. Mirrors networkV1.ts.
const MAX_KEYURL_CACHE_SIZE = 16;
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAX_KEYURL_CACHE_SIZE is defined here and duplicated in v1/networkV1.ts. Consider moving this to a shared constant (or exporting/importing one value) to prevent future drift between v1 and v2 cache bounds.

Suggested change
// Bounded to MAX_KEYURL_CACHE_SIZE with FIFO eviction. Mirrors networkV1.ts.
const MAX_KEYURL_CACHE_SIZE = 16;
// Shared cache bound for relayer key-url caching. Other implementations
// should import/export this value rather than redefining it.
export const MAX_KEYURL_CACHE_SIZE = 16;

Copilot uses AI. Check for mistakes.
@luchobonatti luchobonatti force-pushed the fix/336-bounded-keyurlcache branch 3 times, most recently from 559671f to 85a0b44 Compare April 9, 2026 15:47
Replace unbounded Record/Map caches in networkV1.ts and
AbstractRelayerProvider.ts with Map<string, CachedKey> bounded to
MAX_KEYURL_CACHE_SIZE=16 entries. FIFO eviction via Map insertion order
prevents indefinite growth in long-running server processes.

Closes zama-ai#336
@luchobonatti luchobonatti force-pushed the fix/336-bounded-keyurlcache branch from 85a0b44 to c7c4423 Compare April 9, 2026 15:57
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.

Memory Leak: Unbounded keyurlCache can grow indefinitely

2 participants