-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: Add resource cleanup mechanism in CodeAgent #1056
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
Open
kingdomad
wants to merge
6
commits into
huggingface:main
Choose a base branch
from
kingdomad:fix/codeagent-docker-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c7d7cf8
fix: Add resource cleanup mechanism in CodeAgent to prevent Docker co…
kingdomad 92e5770
simplify CodeAgent cleanup logic
kingdomad 09db403
Remove redundant conditional check
kingdomad 5042e84
Revert "Remove redundant conditional check"
kingdomad 2b04606
Add test for CodeAgent Docker resource cleanup
kingdomad ba41fe6
use try-except for executor deletion in cleanup method
kingdomad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
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.
I think the first condition is always True:
self.python_executor
is defined at instantiation.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.
You're right, I'll remove the first condition.
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.
I found that the first condition is necessary because there are some cases where the program exits before
self.python_executor
is fully instantiated, and in those cases,CodeAgent
doesn't have thepython_executor
attribute.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.
I'm not sure of understanding your argument: if the program exits during instantiation, then the instance is not created, so
self
does not exist, so you cannot delete a non-existing instance. 🤔Am I missing something?
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.
No. If
CodeAgent
encounters an error during instantiation, the instance will still exist in an incomplete form and will still execute the logic in the__del__
method andcleanup
method. For example, if an error occurs when instantiating the docker executor, theCodeAgent
won't have apython_executor
attribute, but it will still executeCodeAgent.cleanup
. Without theif hasattr(self, "python_executor")
check, it would raise another error. I've already tested this.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.
Even if, in 99% of cases, the
__del__
method will not be executed and only in 1% of cases it will, we should still take this 1% into account and add the conditionif hasattr(self, "python_executor")
to enhance the robustness of the program.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.
In an interactive console, several factors can affect the execution of the
__del__
method: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.
I see your point, @kingdomad, and I appreciate the discussion.
__del__
is reliably called. See my suggestion below.__del__
may not be called, ensuring proper resource cleanup in those scenarios. How do you think we could best handle this?For the case where
__del__
is called, I would suggest following the "Easier to ask for forgiveness than permission" approach, by wrapping the cleanup in atry
block:Let me know what you think!
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.
You make a valid point, but I think you're making this more complicated than it needs to be. I've consulted both Claude 3.7 and GPT-4o, and they both agree with my approach to modifying the code! Which approach do you prefer? I'm happy to implement your suggestion if that's what you'd like!
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.
@albertvillanova I've reconsidered, and your approach makes sense. I'll implement the change using the try-except pattern as you suggested.