Skip to content

fix(lid): bound LIDMappingStore cache to stop unbounded timer/memory growth#2640

Open
lopesclayton wants to merge 1 commit into
WhiskeySockets:masterfrom
lopesclayton:fix-lid-mapping-cache-leak
Open

fix(lid): bound LIDMappingStore cache to stop unbounded timer/memory growth#2640
lopesclayton wants to merge 1 commit into
WhiskeySockets:masterfrom
lopesclayton:fix-lid-mapping-cache-leak

Conversation

@lopesclayton

@lopesclayton lopesclayton commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Problem

LIDMappingStore.mappingCache was created as:

new LRUCache({ ttl: 3 * 24 * 60 * 60 * 1000, ttlAutopurge: true, updateAgeOnGet: true })

With no max and ttlAutopurge: true, the cache grows without bound and arms one 3-day setTimeout per entry. Under a flood of @lid mappings this accumulates tens of thousands of timers, leaking memory until OOM (heap dumps show a large TimersList of 259_200_000ms timers).

Change

new LRUCache({ max: 10000, ttl: 3 * 24 * 60 * 60 * 1000, ttlAutopurge: false, updateAgeOnGet: true })
  • max: 10000 — bounds the cache; evicted entries are simply re-fetched from the key store on the next miss.
  • ttlAutopurge: false — removes the per-entry timer. TTL is still honored, just evaluated lazily on get instead of via a background timer.

Also fixed a stale // 7 days comment (the value has always been 3 days).

Notes

  • Behavior is preserved: the cache remains a fallback layer over the DB, so eviction only causes a re-read, never data loss.
  • No public API change.

Summary by cubic

Bound the LIDMappingStore cache and removed per-entry timers to stop unbounded memory/timer growth, preventing OOM under heavy @lid mapping traffic.

  • Bug Fixes
    • Set LRUCache to max: 10000 and ttlAutopurge: false; TTL stays 3 days and is checked on get.
    • Evicted entries re-fetch from the key store; no API/behavior changes. Fixed stale "7 days" comment.

Written for commit a6e43e3. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Chores
    • Optimized internal caching configuration for improved performance and memory management.

…growth

Add `max` and disable `ttlAutopurge` so each mapping no longer holds a 3-day timer; evicted entries fall back to the DB.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The mappingCache LRU configuration in LIDMappingStore gains an explicit max: 10000 entry cap, a corrected TTL comment ("3 days"), and ttlAutopurge disabled so TTL expiry is enforced lazily on cache reads rather than via a background sweep.

Changes

LID Mapping Cache Configuration

Layer / File(s) Summary
mappingCache size bound and lazy TTL
src/Signal/lid-mapping.ts
Adds max: 10000 to bound cache size, fixes the TTL comment to say "3 days", and disables ttlAutopurge to switch from proactive background purging to lazy eviction on get.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐇 Hop, hop, the cache is capped at ten grand,
No purging at night — just lazy on demand!
Three days the TTL, the comment now right,
Old entries evict when you peek in the light.
A tidy small fix, done with a flick of the paw! 🌙

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly summarizes the main change: bounding the LIDMappingStore cache to prevent unbounded timer and memory growth, which matches the core fix described in the PR objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@whiskeysockets-bot

Copy link
Copy Markdown
Contributor

Thanks for opening this pull request and contributing to the project!

The next step is for the maintainers to review your changes. If everything looks good, it will be approved and merged into the main branch.

In the meantime, anyone in the community is encouraged to test this pull request and provide feedback.

✅ How to confirm it works

If you’ve tested this PR, please comment below with:

Tested and working ✅

This helps us speed up the review and merge process.

📦 To test this PR locally:

# NPM
npm install @whiskeysockets/baileys@lopesclayton/Baileys-1#fix-lid-mapping-cache-leak

# Yarn (v2+)
yarn add @whiskeysockets/baileys@lopesclayton/Baileys-1#fix-lid-mapping-cache-leak

# PNPM
pnpm add @whiskeysockets/baileys@lopesclayton/Baileys-1#fix-lid-mapping-cache-leak

If you encounter any issues or have feedback, feel free to comment as well.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/Signal/lid-mapping.ts (1)

7-8: 💤 Low value

Consider removing the "PATCH:" prefix from the comment.

The "PATCH:" prefix suggests a temporary workaround, but this appears to be the permanent solution to the timer accumulation issue. Consider rephrasing to a more standard explanatory comment, e.g., "Bounds the cache and disables auto-purge timers; TTL is checked lazily on get to avoid per-entry timers."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Signal/lid-mapping.ts` around lines 7 - 8, Remove the "PATCH:" prefix
from the comment block in the lid-mapping.ts file (around line 7-8) and rephrase
it as a standard explanatory comment. Instead of "// PATCH: `max` bounds the
cache and `ttlAutopurge: false` drops the per-entry...", change it to describe
this as the permanent solution rather than a temporary workaround. The comment
should explain that the cache bounds and disabled auto-purge timers with lazy
TTL checking are the intended approach to avoid per-entry timer accumulation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/Signal/lid-mapping.ts`:
- Around line 7-8: Remove the "PATCH:" prefix from the comment block in the
lid-mapping.ts file (around line 7-8) and rephrase it as a standard explanatory
comment. Instead of "// PATCH: `max` bounds the cache and `ttlAutopurge: false`
drops the per-entry...", change it to describe this as the permanent solution
rather than a temporary workaround. The comment should explain that the cache
bounds and disabled auto-purge timers with lazy TTL checking are the intended
approach to avoid per-entry timer accumulation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d7f17093-accf-4515-b6e8-6051ce9d0dfe

📥 Commits

Reviewing files that changed from the base of the PR and between 78e7e4e and a6e43e3.

📒 Files selected for processing (1)
  • src/Signal/lid-mapping.ts

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 1 file

Re-trigger cubic

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open for 14 days with no activity. Remove the stale label or comment or this will be closed in 14 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants