A programming framework for agentic AI
using thread safe timeout to allow code execution to be compatible with multi-threading/multi-processing (#224)
Ragy Abraham3991f860e4b6
diff --git a/autogen/code_utils.py b/autogen/code_utils.pyindex 814a1e15b..caaf09072 100644--- a/autogen/code_utils.py+++ b/autogen/code_utils.py@@ -1,4 +1,3 @@-import signalimport subprocessimport sysimport os@@ -9,6 +8,7 @@ import timefrom hashlib import md5import loggingfrom autogen import oai+from concurrent.futures import ThreadPoolExecutor, TimeoutErrortry:import docker@@ -307,21 +307,20 @@ def execute_code(text=True,)else:- signal.signal(signal.SIGALRM, timeout_handler)- try:- signal.alarm(timeout)- # run the code in a subprocess in the current docker container in the working directory- result = subprocess.run(+ with ThreadPoolExecutor(max_workers=1) as executor:+ future = executor.submit(+ subprocess.run,cmd,cwd=work_dir,capture_output=True,text=True,)- signal.alarm(0)- except TimeoutError:- if original_filename is None:- os.remove(filepath)- return 1, TIMEOUT_MSG, None+ try:+ result = future.result(timeout=timeout)+ except TimeoutError:+ if original_filename is None:+ os.remove(filepath)+ return 1, TIMEOUT_MSG, Noneif original_filename is None:os.remove(filepath)if result.returncode: