-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix memory leak in JSON encoder #6620
Conversation
- Created OpenHandsJSONEncoder class to properly handle custom types - Reuse single encoder instance for common case - Added memory leak test to verify fix - Improved memory usage by avoiding repeated encoder creation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! OpenHands did a great job here helping us hunting down memory leaks 😢
Here's a concise overview of the changes and their effectiveness: Changes Made:
Status Assessment: Remaining Issues: None apparent - the solution seems complete and robust, following Python best practices for process management. Overall: The changes successfully resolve the reported memory leak issue with a well-structured, defensive implementation. |
ugh it is fixing the wrong thing? |
This reverts commit 3ec5f32.
return obj.model_dump() | ||
if isinstance(obj, CmdOutputMetadata): | ||
return obj.model_dump() | ||
return json.JSONEncoder().default(obj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was it actually a leak that never got garbage collected over time, or was it just excessive memory usage from having to construct json.JSONEncoder()
over and over again for every object being serialized?
Co-authored-by: openhands <[email protected]> Co-authored-by: Xingyao Wang <[email protected]>
Co-authored-by: openhands <[email protected]> Co-authored-by: Xingyao Wang <[email protected]>
Description
Fixed a memory leak in the JSON encoder by implementing a proper
OpenHandsJSONEncoder
class and reusing encoder instances.OpenHands identified a memory leak, confirmed that it failed with tests, and then fixed it: https://www.all-hands.dev/share?share_id=23e481ad21c3bebf3d9a7b97da951ac086f26a7919ed89db1ea560ad96a52c2f
Changes
OpenHandsJSONEncoder
class to properly handle custom typesTesting
Impact
This change improves memory usage by avoiding the creation of unnecessary encoder instances, which should help prevent memory leaks in long-running processes.
To run this PR locally, use the following command: