Skip to content

Comments

fix: resolve OOM crashes from memory leaks in renderer process#12975

Draft
DeJeune wants to merge 1 commit intomainfrom
DeJeune/review-pr-12965
Draft

fix: resolve OOM crashes from memory leaks in renderer process#12975
DeJeune wants to merge 1 commit intomainfrom
DeJeune/review-pr-12965

Conversation

@DeJeune
Copy link
Collaborator

@DeJeune DeJeune commented Feb 20, 2026

What this PR does

Fixes #12925: Resolves three memory leak sources causing out-of-memory crashes during active agent/tool usage in the renderer process.

Before: Global maps and Redux caches accumulated entries indefinitely, causing heap growth until OOM termination.

After: Stream completion and topic switching now properly clean up resources, stabilizing heap memory.

Why we need it and why it was done in this way

Root causes identified:

  1. globalActiveToolCalls (static Map): Entries added on tool-input-start but only removed on tool-result/error. Aborted streams left orphaned entries forever.
  2. abortMap: Entries added per message but only removed on user abort. Normal stream completion never cleaned up entries.
  3. Topic message caches: Redux messageIdsByTopic and entities grew unbounded as users viewed topics.

Fixes applied:

  1. Added ToolCallChunkHandler.clearAll() static method, called in AiSdkToChunkAdapter.readFullStream() finally block.
  2. Added cleanupAbortController(id) utility, called in both fetchAndProcessAgentResponseImpl and fetchAndProcessAssistantResponseImpl finally blocks.
  3. Added evictTopicCache reducer and eviction logic in loadTopicMessagesThunk to keep only 5 most-recently-accessed topics in memory.

Why this approach:

  • Minimal changes: Cleanups added to existing finally blocks (safe, always executes)
  • No API changes: New functions are internal; no breaking changes
  • Conservative cache limit: MAX_CACHED_TOPICS=5 balances memory vs user convenience (can adjust later)

Breaking changes

None. All changes are internal cleanup; no public APIs modified.

Checklist

  • Lint, typecheck, format all pass
  • All 3151 tests pass
  • Changes follow existing patterns
  • Code is simple and maintainable

Clean up three memory leak sources during active agent/tool usage:
1. Clear globalActiveToolCalls static map on stream end/abort
2. Remove abortMap entries after normal stream completion
3. Evict inactive topic caches to free message/block entities

Fixes #12925
@DeJeune DeJeune requested a review from 0xfullex as a code owner February 20, 2026 10:46
@DeJeune DeJeune marked this pull request as draft February 20, 2026 10:50
@DeJeune
Copy link
Collaborator Author

DeJeune commented Feb 20, 2026

Note

This comment was translated by Claude.

A simple fix that needs to be tested.


Original Content

a simple fix and need to be tested

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]: Multiple OOM Crashes in Renderer Process Due to Memory Leaks

1 participant