-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: timeout for interpreter module #1851
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: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ class BaseInterpreter(ABC): | |
| r"""An abstract base class for code interpreters.""" | ||
|
|
||
| @abstractmethod | ||
| def run(self, code: str, code_type: str) -> str: | ||
| def run(self, code: str, code_type: str, timeout: int = None) -> str: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer Optional[int] in the type hints for clarity.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to update the argument |
||
| r"""Executes the given code based on its type. | ||
|
|
||
| Args: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,11 +78,12 @@ def __init__( | |
| require_confirm: bool = True, | ||
| print_stdout: bool = False, | ||
| print_stderr: bool = True, | ||
| default_timeout: int = 60, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also update the docstring? |
||
| ) -> None: | ||
| self.require_confirm = require_confirm | ||
| self.print_stdout = print_stdout | ||
| self.print_stderr = print_stderr | ||
|
|
||
| self.default_timeout = default_timeout | ||
| # lazy initialization of container | ||
| self._container: Optional[Container] = None | ||
|
|
||
|
|
@@ -152,6 +153,7 @@ def _run_file_in_container( | |
| self, | ||
| file: Path, | ||
| code_type: str, | ||
| timeout: int = None, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer Optional[int] in the type hints for clarity. |
||
| ) -> str: | ||
| code_type = self._check_code_type(code_type) | ||
| commands = shlex.split( | ||
|
|
@@ -166,6 +168,7 @@ def _run_file_in_container( | |
| stdout, stderr = self._container.exec_run( | ||
| commands, | ||
| demux=True, | ||
| timeout=timeout if timeout is not None else self.default_timeout, | ||
| ).output | ||
|
|
||
| if self.print_stdout and stdout: | ||
|
|
@@ -184,6 +187,7 @@ def run( | |
| self, | ||
| code: str, | ||
| code_type: str, | ||
| timeout: int = None, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer Optional[int] in the type hints for clarity. |
||
| ) -> str: | ||
| r"""Executes the given code in the conatiner attached to the | ||
| interpreter, and captures the stdout and stderr streams. | ||
|
|
@@ -228,7 +232,7 @@ def run( | |
|
|
||
| try: | ||
| temp_file_path = self._create_file_in_container(code) | ||
| result = self._run_file_in_container(temp_file_path, code_type) | ||
| result = self._run_file_in_container(temp_file_path, code_type,timeout=timeout) | ||
| except docker.errors.APIError as e: | ||
| raise InterpreterError( | ||
| f"Execution halted due to docker API error: {e.explanation}. " | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| import importlib | ||
| import typing | ||
| from typing import Any, ClassVar, Dict, List, Optional | ||
|
|
||
| import concurrent.futures | ||
| from camel.interpreters.base import BaseInterpreter | ||
| from camel.interpreters.interpreter_error import InterpreterError | ||
|
|
||
|
|
@@ -87,15 +87,17 @@ def __init__( | |
| import_white_list: Optional[List[str]] = None, | ||
| unsafe_mode: bool = False, | ||
| raise_error: bool = False, | ||
| default_timeout:int = 60 | ||
| ) -> None: | ||
| self.action_space = action_space or dict() | ||
| self.state = self.action_space.copy() | ||
| self.fuzz_state: Dict[str, Any] = dict() | ||
| self.import_white_list = import_white_list or list() | ||
| self.raise_error = raise_error | ||
| self.unsafe_mode = unsafe_mode | ||
| self.default_timeout = default_timeout | ||
|
|
||
| def run(self, code: str, code_type: str) -> str: | ||
| def run(self, code: str, code_type: str, timeout:int =None) -> str: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer Optional[int] in the type hints for clarity. |
||
| r"""Executes the given code with specified code type in the | ||
| interpreter. | ||
|
|
||
|
|
@@ -145,7 +147,13 @@ def run(self, code: str, code_type: str) -> str: | |
|
|
||
| return result | ||
| else: | ||
| return str(self.execute(code)) | ||
| try: | ||
| with concurrent.futures.ThreadPoolExecutor() as executor: | ||
| future = executor.submit(self._execute_ast) | ||
| line_result = future.result(timeout=timeout or self.default_timeout) | ||
| return str(line_result) | ||
| except concurrent.futures.TimeoutError: | ||
| raise InterpreterError("Code execution timed out.") | ||
|
|
||
| def update_action_space(self, action_space: Dict[str, Any]) -> None: | ||
| r"""Updates action space for *python* interpreter.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ def run_file( | |
| self, | ||
| file: Path, | ||
| code_type: str, | ||
| timeout: int = None | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer Optional[int] in the type hints for clarity. |
||
| ) -> str: | ||
| r"""Executes a code file in a subprocess and captures its output. | ||
|
|
||
|
|
||
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.
we also need to update the docstring