warp icon indicating copy to clipboard operation
warp copied to clipboard

fix: add 50MB file size guard to prevent excessive memory usage when opening large files

Open warp-agent-staging opened this issue 6 months ago • 0 comments

Summary

Add a 50 MB file size guard to FileModel::open to prevent excessive memory usage when opening very large files in the editor.

Problem

When a very large file (hundreds of MB+) is opened in the editor, the entire content is loaded into a SumTree<BufferText> buffer via Buffer::replace_all, then cloned into Vec<StyledBufferBlock> for rendering, then layout is invalidated rebuilding all styled blocks, and diffs are computed — all without any size guard. A single large file can cascade into 15+ GB of in-use memory.

This was identified from a Sentry memory alert (issue 7259255054) with 18.6 GB resident memory. Heap profile breakdown:

  • Buffer::replace_all via populate_buffer_with_read_content: 7.6 GB (47%)
  • StyledBufferBlock cloning in handle_content_model_event: 2.8 GB (17%)
  • Layout invalidation in rebuild_layout_and_refresh_diff: 2.5 GB (15%)
  • LocalDiffStateModel::parse_diff_hunks: 2.3 GB (15%)
  • DiffModel::compute_diff: 0.4 GB (2%)

Changes

  1. FileLoadError::FileTooLarge — New error variant in warp_util::file that carries the file size and the limit.
  2. Pre-read size check in FileModel::open — Before reading the file content into a String, we stat the file via async_fs::metadata and reject files exceeding 50 MB with FileLoadError::FileTooLarge. This prevents the entire allocation chain from executing.

The existing FailedToLoad error path in the UI already handles load failures gracefully (the editor shows an empty/new-file state), so this change integrates cleanly.

Testing

  • cargo check --package warp_files --package warp_util ✅
  • cargo clippy --package warp_files --package warp_util -- -D warnings ✅
  • cargo fmt ✅

CHANGELOG-BUG-FIX: Fixed excessive memory usage (15+ GB) when opening very large files in the editor by adding a 50 MB file size limit.


Conversation: https://staging.warp.dev/conversation/27cd8261-18ad-4000-9607-03e08b76ab05 Run: https://oz.staging.warp.dev/runs/019e6d8e-6398-7d2a-92aa-bb52ef63c0f7 This PR was generated with Oz.

warp-agent-staging avatar May 28 '26 07:05 warp-agent-staging