Skip to content

Conversation

@ericallam
Copy link
Member

@ericallam ericallam commented Jan 26, 2026

  • Fix memory leak in RunAttemptSystem queue cache - was keying by runId instead of queue identifier
  • Replace @unkey/cache MemoryStore with new LRUMemoryStore for O(1) operations and better memory bounds

Problem

Cache Key Bug

The queue cache in #resolveTaskRunExecutionQueue was keyed by runId, creating one cache entry per run instead of per queue. With 1-2 hour TTLs and 5000 entry soft cap, these accumulated causing memory growth.

MemoryStore Performance

The @unkey/cache MemoryStore uses O(n) synchronous iteration for eviction, blocking the event loop at high throughput.

Solution

Cache Key Fix

Changed cache key from params.runId to queue identifier:

const cacheKey = params.lockedQueueId ?? `${params.runtimeEnvironmentId}:${params.queueName}`;

LRU Cache

Created LRUMemoryStore adapter using lru-cache package:

  • O(1) get/set/delete operations
  • Strict memory bounds (hard max vs soft cap)
  • No event loop blocking

Test Results:

Metric Before Fix After Fix
Queue cache entries (per 1000 runs) ~1000 1
Old space growth 32.27 MB 5.45 MB
Heap growth 7.21 MB (3.9%) 4.81 MB (2.6%)

Open with Devin

@changeset-bot
Copy link

changeset-bot bot commented Jan 26, 2026

⚠️ No Changeset found

Latest commit: f1be5b3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 26, 2026

Walkthrough

Introduces a new LRU-backed in-memory cache implementation (LRUMemoryStore, createLRUMemoryStore) and exports it from internal-packages/cache, adds the lru-cache dependency and Vitest config, and adds comprehensive tests for the LRUMemoryStore. Replaces many in-process MemoryStore usages across the codebase with createLRUMemoryStore(...) calls (webapp services, run-engine, queues, rate-limiting, idempotency, etc.). Also updates one private method signature in runAttemptSystem to change how queue cache keys are derived and used.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description is largely incomplete. It lacks the required checklist section, testing section details, and screenshots section, missing most of the template structure despite containing substantial technical content. Add the missing checklist (with contributor guide confirmation), a dedicated Testing section explaining validation steps, and follow the provided template structure more closely.
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main changes: fixing a queue cache memory leak and replacing MemoryStore with LRU cache, both primary objectives of this PR.

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


📜 Recent review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2871f4a and f1be5b3.

📒 Files selected for processing (1)
  • internal-packages/cache/package.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal-packages/cache/package.json
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (21)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (8, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (5, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (7, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (6, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (6, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (4, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (3, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (2, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (1, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (8, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (5, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (7, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (1, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (2, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (3, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (4, 8)
  • GitHub Check: units / packages / 🧪 Unit Tests: Packages (1, 1)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: Analyze (javascript-typescript)

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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.

@vibe-kanban-cloud
Copy link

Review Complete

Your review story is ready!

View Story

Comment !reviewfast on this PR to re-generate the story.

Copy link

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional flags.

Open in Devin Review

@ericallam ericallam marked this pull request as ready for review January 26, 2026 16:32
@ericallam ericallam force-pushed the fix/cache-memory-store-leak branch from 8bd2b77 to 2871f4a Compare January 26, 2026 16:59
@ericallam ericallam merged commit eeab6bd into main Jan 26, 2026
31 checks passed
@ericallam ericallam deleted the fix/cache-memory-store-leak branch January 26, 2026 22:23
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.

3 participants