lingo.dev icon indicating copy to clipboard operation
lingo.dev copied to clipboard

fix: memory leak in translation execution (unbounded Map growth)

Open NiranjanKumar001 opened this issue 1 year ago • 4 comments

What

Memory leak in packages/cli/src/cli/cmd/run/execute.ts where the perFileIoLimiters Map grows indefinitely and is never cleared, causing unbounded memory accumulation across multiple translation runs.

Current behavior:

  • Map uses lazy allocation: creates limiters on-demand when first accessed (lines 58-68)
  • Map entries are never removed or cleared after task completion
  • In watch mode or long-running processes, memory grows indefinitely
  • Each unique bucket pattern adds a new Map entry that persists in memory

Expected behavior:

  • Map should have fixed size based on number of unique bucket patterns
  • Memory should be released after task completion
  • No memory accumulation across multiple runs

Location: packages/cli/src/cli/cmd/run/execute.ts, lines 58-68

Current code:

const perFileIoLimiters = new Map<string, LimitFunction>();
const getFileIoLimiter = (bucketPathPattern: string): LimitFunction => {
  const lockKey = bucketPathPattern;
  if (!perFileIoLimiters.has(lockKey)) {
    perFileIoLimiters.set(lockKey, pLimit(1)); // ❌ Unbounded growth
  }
  return perFileIoLimiters.get(lockKey)!;
};


please assign this to me 

NiranjanKumar001 avatar Nov 06 '25 13:11 NiranjanKumar001

please assign this issue to me

Priyanshubhartistm avatar Nov 06 '25 13:11 Priyanshubhartistm

@NiranjanKumar001 @Priyanshubhartistm

Awesome find! How do you plan to fix this, any thoughts so far? One approach: replace Map with WeakMap to allow automatic garbage collection. What do you think?

@NiranjanKumar001 - are you still interested in working on this? If so, we'll assign it to you. Otherwise, @Priyanshubhartistm can take it.

maxprilutskiy avatar Nov 10 '25 06:11 maxprilutskiy

@maxprilutskiy sure I would like to fix, thanks for your time

NiranjanKumar001 avatar Nov 10 '25 06:11 NiranjanKumar001

ok after reading docs and some research i got more info ,here is why weakmap will fail ? 1.WeakMap requires object keys, strings aren't objects. 2. then i thought of using Wrapper but here is why Wrapper Doesn't Solve the Memory Leak-> ~WeakMap would garbage collect the Symbol → pLimit entries only if the symbol has no other references But we're storing the symbols in patternSymbols Map (regular Map) Regular Map holds strong references - they never get garbage collected automatically So the WeakMap entries never get cleaned up because the symbols are always referenced!

now, how i am looking to fix it: i will use pre-allocation approach

Why This will Solves the Memory Leak?

Before (On-Demand):

Run 1: Map has 100 entries Run 2: Map has 200 entries (previous 100 never cleared!) Run 3: Map has 300 entries Memory leak! After (Pre-allocation):

Run 1: Map created with 100 entries → used → scope ends, garbage collected Run 2: NEW Map created with 100 entries → used → scope ends, garbage collected Run 3: NEW Map created with 100 entries → used → scope ends, garbage collected No leak! Each run is independent!

The Map will be created inside a task function scope. When the task completes, the entire Map is automatically garbage collected because there are no references to it anymore. @maxprilutskiy

NiranjanKumar001 avatar Nov 10 '25 09:11 NiranjanKumar001