-
Notifications
You must be signed in to change notification settings - Fork 2.1k
added isolated code executor #3225
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @AlexFierro9, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Response from ADK Triaging Agent Hello @AlexFierro9, thank you for creating this PR! To help us review your contribution, could you please address the following points from our contribution guidelines:
This information will help reviewers to review your PR more efficiently. Thanks! |
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.
Code Review
This pull request introduces an IsolatedCodeExecutor
that runs code in a separate process using subprocess
, which is a good improvement for isolation. My review includes several suggestions to enhance security and robustness, such as adding a timeout to prevent denial-of-service attacks and clarifying the security guarantees in the docstring. I've also pointed out some code cleanup opportunities like removing unused imports and dead code.
Unused imports can be confusing Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Response from ADK Triaging Agent Hello @AlexFierro9, thank you for creating this PR and for linking the issue! To help us review your contribution, could you please also add a This information will help reviewers to review your PR more efficiently. Thanks! |
Implemented unit-tests in line with those from unsafe local code executor |
@boyangsvl please assign a reviewer as time permits |
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.
Code Review
The pull request introduces a new IsolatedCodeExecutor
that enhances code execution security by running code in a separate process. This approach provides memory isolation, preventing the executed code from directly interfering with the main application. The changes include the implementation of the executor and corresponding unit tests to ensure its functionality and robustness. The executor is designed to be stateless and not optimize data files, with appropriate checks in place to prevent these features from being enabled.
process_result = subprocess.run( | ||
[sys.executable, "-c", code], | ||
capture_output=True, | ||
text=True | ||
) |
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.
Consider adding a timeout to the subprocess.run
call to prevent indefinite hanging in case the executed code enters an infinite loop or takes too long to execute. This will improve the robustness of the executor.
Also, it might be useful to capture and log the return code of the subprocess for debugging purposes.
process_result = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True, # Enables decoding of stdout and stderr as text
timeout=30 # Add a timeout to prevent indefinite hanging
)
if process_result.returncode != 0:
print(f"Code execution failed with return code: {process_result.returncode}")
return CodeExecutionResult( | ||
stdout=process_result.stdout, | ||
stderr=process_result.stderr, | ||
output_files=[], |
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.
It's important to handle potential exceptions that might occur during the code execution within the subprocess. For example, the code might raise an exception that isn't properly propagated back to the main process. Consider adding a try-except block around the subprocess.run
call to catch and handle such exceptions, providing more informative error messages in the CodeExecutionResult
.
return CodeExecutionResult( | |
stdout=process_result.stdout, | |
stderr=process_result.stderr, | |
output_files=[], | |
try: | |
process_result = subprocess.run( | |
[sys.executable, "-c", code], | |
capture_output=True, | |
text=True | |
) | |
except subprocess.TimeoutExpired as e: | |
return CodeExecutionResult( | |
stdout="", | |
stderr=f"Code execution timed out: {e}", | |
output_files=[], | |
) | |
except Exception as e: | |
return CodeExecutionResult( | |
stdout="", | |
stderr=f"Code execution failed: {e}", | |
output_files=[], | |
) |
|
||
assert isinstance(result, CodeExecutionResult) | ||
assert result.stdout == "" | ||
assert "Test error" in result.stderr |
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.
The test asserts that "Test error" is in result.stderr
. It would be more robust to assert that the ValueError
exception type is also present in the stderr
. This ensures that the correct exception is being raised and captured.
assert "Test error" in result.stderr | |
assert "ValueError" in result.stderr | |
assert "Test error" in result.stderr |
Have added a new Isolated Code Executor that uses subprocesses module to run the code on a new interpreter process, there is a bonus of isolated code execution for local setups as well. Working on support for Docker Integration as well but that is going to be a while.
Implements feature plan partly described in #3075