Skip to content

Commit

Permalink
using thread safe timeout to allow code execution to be compatible wi…
Browse files Browse the repository at this point in the history
…th multi-threading/multi-processing (microsoft#224)

Co-authored-by: Li Jiang <[email protected]>
Co-authored-by: Victor Dibia <[email protected]>
Co-authored-by: Chi Wang <[email protected]>
  • Loading branch information
4 people authored Oct 24, 2023
1 parent a66da4f commit d693bd7
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions autogen/code_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import signal
import subprocess
import sys
import os
Expand All @@ -9,6 +8,7 @@
from hashlib import md5
import logging
from autogen import oai
from concurrent.futures import ThreadPoolExecutor, TimeoutError

try:
import docker
Expand Down Expand Up @@ -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, None
if original_filename is None:
os.remove(filepath)
if result.returncode:
Expand Down

0 comments on commit d693bd7

Please sign in to comment.