launchdarkly / js-core

LaunchDarkly monorepo for JavaScript SDKs
Other
12 stars 12 forks source link

Memory Leak Inside LruCache #474

Closed eholland-banno closed 1 month ago

eholland-banno commented 1 month ago

Describe the bug It appears that the keyMap used to store indexes for retrieving values is a memory leak. When clear() runs on the cache, it clears out most of the data stored except for the keyMap which can grow infinitely as unique keys get added into the cache. It also seems like the indexes stored in the keyMap would become stale on clear() since they would no longer point to the correct index in the values array.

To reproduce If you call client.variation(key, context, defaultValue) many times passing unique keys, the keyMap will continue to grow infinitely even if clear() were to be called.

Expected behavior I would expect a call to this.keyMap.clear() in the LruCache's clear() function like so:

  clear() {
    ...
    this.keyMap.clear();
  }

Local testing of this has proven to reduce the memory as we're clearing out the keyMap periodically.

SDK version @launchdarkly/js-server-sdk-common version 1.2.3

Language version, developer tools Node 18.19.0

OS/platform macOS 14.4

kinyoklion commented 1 month ago

Thank you for the report and details @eholland-banno

We will take a look.

Filed internally as 246138

kinyoklion commented 4 weeks ago

@eholland-banno The fix has been released in server common 2.4.2 (Node server 9.4.4). Let me know if you have any challenges with version upgrades.

eholland-banno commented 4 weeks ago

@eholland-banno The fix has been released in server common 2.4.2 (Node server 9.4.4). Let me know if you have any challenges with version upgrades.

Awesome, will do. Thanks a ton for the fast response @kinyoklion!