This repository was archived by the owner on May 15, 2026. It is now read-only.
fix: Investigate and fix memory leak in file editor#60
Merged
Conversation
This commit adds a comprehensive test to demonstrate and measure memory growth during file editing operations. The test: 1. Sets a memory limit of 256MB 2. Creates large file content to make memory issues more apparent 3. Performs multiple edits while monitoring memory usage 4. Tracks memory growth rate and fails if consistent growth is detected 5. Provides detailed memory statistics throughout the test run The test successfully demonstrates a memory leak where memory grows by approximately 19MB per 100 edits.
This commit implements several improvements to reduce memory usage in the file editor: 1. Added FileHistoryManager class to manage file edit history: - Uses disk-based storage instead of in-memory storage - Maintains a fixed window of history entries per file - Implements a memory cache with size limits - Automatically cleans up temporary files 2. Modified OHEditor to use the new history manager: - Removed in-memory history dictionary - Updated all history operations to use disk storage - Added proper cleanup on exit 3. Updated memory leak test: - Reduced test size to be more manageable - Added history size monitoring - Test now passes with stable memory usage The changes ensure that memory usage remains constant regardless of the number of edits performed.
- Add line-by-line reading for large files - Use streaming operations for file modifications - Add psutil to test dependencies - Update workflow to install test deps
- Keep only memory-efficient file reading in editor.py - Optimize history storage to avoid saving entire history at once
3a2cc2c to
7aa1e23
Compare
- Add size limit check (10MB max) - Add binary file detection - Add MIME type checking for text files - Add FileValidationError exception - Fix error message handling in base ToolError class - Add comprehensive tests for file validation
- Reduce file size to 9.5MB to stay under validation limit - Add garbage collection before memory measurement - Add better error handling and debug output - Add file size and line count verification - Improve memory usage reporting
1 task
enyst
reviewed
Jan 31, 2025
Member
|
The code LGTM. Interesting! |
neubig
reviewed
Jan 31, 2025
neubig
left a comment
Member
There was a problem hiding this comment.
Looks great to me! I basically approve but just had a few small comments.
| from collections import defaultdict | ||
| from pathlib import Path | ||
| from typing import Literal, get_args | ||
| from typing import Literal, Optional, get_args |
Member
There was a problem hiding this comment.
| None probably works instead of Optional.
|
|
||
| # Check file size (10MB limit) | ||
| file_size = os.path.getsize(path) | ||
| max_size = 10 * 1024 * 1024 # 10MB in bytes |
Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
- Replace Optional with | None - Make file size limit configurable
neubig
approved these changes
Jan 31, 2025
Contributor
Author
|
Thanks @neubig ! Just got OpenHands revert an unintended change when it was addressing your comment :D Will merge now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a memory leak issue in the file editor functionality by implementing a disk-based history manager.
Issue Description
When performing multiple file edit operations, memory usage grows significantly and consistently. A test was added that demonstrated:
Solution
Implemented several improvements to reduce memory usage:
Added FileHistoryManager class to manage file edit history:
Modified OHEditor to use the new history manager:
Updated memory leak test:
Test Results
The memory leak test now passes successfully, showing:
Additional Notes