Skip to content

fix: add TTL cleanup for pendingReplies memory leak#49

Closed
kumudasrip wants to merge 5 commits into
snackoverflowasad:mainfrom
kumudasrip:fix/memoryleak
Closed

fix: add TTL cleanup for pendingReplies memory leak#49
kumudasrip wants to merge 5 commits into
snackoverflowasad:mainfrom
kumudasrip:fix/memoryleak

Conversation

@kumudasrip

Copy link
Copy Markdown

Description of Changes

Implemented a TTL-based cleanup mechanism for the pendingReplies map to prevent unbounded memory growth in long-running deployments.

Changes made:

  • Added lastActivityAtMs tracking for each pending reply entry.

  • Implemented automatic cleanup of stale inactive entries.

  • Added configurable environment variables:

    • CHAT_BUDDY_PENDING_REPLY_TTL_HOURS
    • CHAT_BUDDY_PENDING_REPLY_CLEANUP_INTERVAL_MS
  • Added interval lifecycle management to avoid unnecessary timers.

  • Ensured cleanup timers and pending timeouts are gracefully terminated during bot shutdown.

  • Added validation and clamping for TTL and cleanup interval values.

  • Updated:

    • .env.example
    • README.md
    • CONTRIBUTING.md
    • CHANGELOG.md
    • architecture documentation
    • docker-compose configuration

This prevents inactive users from remaining indefinitely in memory and stabilizes memory usage for long-running bot instances.

Related Issue Number

Closes #45

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update
  • Refactor or other maintenance

How Was This Tested?

Manual Testing

  1. Started the bot using:

    npm run start
  2. Verified:

    • inactive entries were automatically removed after TTL expiry
    • cleanup interval executed correctly
    • active conversations were not removed
    • cleanup timers stopped when map became empty
    • shutdown cleared all intervals and timeouts properly
  3. Tested different environment variable values:

    CHAT_BUDDY_PENDING_REPLY_TTL_HOURS=1
    CHAT_BUDDY_PENDING_REPLY_CLEANUP_INTERVAL_MS=300000

Build Verification

npm run build

Additional Validation

  • Verified no regression in debounce behavior.
  • Confirmed memory usage remains stable during long-running tests with multiple simulated users.

Checklist

  • The code builds without errors (npm run build).
  • Existing tests pass (npm test).
  • Documentation has been updated to reflect these changes if needed.

@kumudasrip

Copy link
Copy Markdown
Author

Hello @snackoverflowasad , This PR fixes issue #45
Please do review and merge the PR under GSSoC 2026 whenever possible.. Thank you!

@snackoverflowasad

Copy link
Copy Markdown
Owner

Hey @kumudasrip please check for ci builds test

@kumudasrip

Copy link
Copy Markdown
Author

I have updated the changes for ci build tests failure
@snackoverflowasad @shouri123 please do review it once again whenever possible, thank you!

@shouri123

Copy link
Copy Markdown
Collaborator

I have updated the changes for ci build tests failure
@snackoverflowasad @shouri123 please do review it once again whenever possible, thank you!

Sime checks are not running

@shouri123

Copy link
Copy Markdown
Collaborator

@kumudasrip please check again the prs it not running the checks are not running

@kumudasrip kumudasrip closed this by deleting the head repository Jun 9, 2026
@kumudasrip

Copy link
Copy Markdown
Author

@kumudasrip please check again the prs it not running the checks are not running

Resolving them, I will open a new PR

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.

[BUG] Memory leak - pendingReplies Map never cleaned up, grows indefinitely

3 participants