Skip to content

A programming framework for agentic AI

[FIX] DockerCommandLineCodeExecutor multi event loop aware (#6402)

EeS516a21195448
diff --git a/python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py b/python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py
index aaef37c0b..cccf78cb6 100644
--- a/python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py
+++ b/python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py
@@ -11,6 +11,7 @@ import tempfile
import uuid
import warnings
from collections.abc import Sequence
+from concurrent.futures import Future as ConcurrentFuture
from hashlib import sha256
from pathlib import Path
from typing import Any, Callable, ClassVar, Dict, List, Optional, ParamSpec, Tuple, Union
@@ -243,7 +244,9 @@ $functions"""
self._container: Container | None = None
self._running = False
- self._cancellation_tasks: List[asyncio.Task[None]] = []
+
+ self._loop: Optional[asyncio.AbstractEventLoop] = None
+ self._cancellation_futures: List[ConcurrentFuture[None]] = []
@property
def timeout(self) -> int:
@@ -305,7 +308,22 @@ $functions"""
return output, exit_code
except asyncio.CancelledError:
# Schedule a task to kill the running command in the background.
- self._cancellation_tasks.append(asyncio.create_task(self._kill_running_command(command)))
+ if self._loop and not self._loop.is_closed():
+ try:
+ logging.debug(f"Scheduling kill command via run_coroutine_threadsafe on loop {self._loop!r}")
+ future: ConcurrentFuture[None] = asyncio.run_coroutine_threadsafe(
+ self._kill_running_command(command), self._loop
+ )
+ self._cancellation_futures.append(future)
+ logging.debug(f"Kill command scheduled, future: {future!r}")
+ except RuntimeError as e:
+ logging.error(f"Failed to schedule kill command on loop {self._loop!r}: {e}")
+ except Exception as e:
+ logging.exception(f"Unexpected error scheduling kill command: {e}")
+ else:
+ logging.warning(
+ f"Cannot schedule kill command: Executor loop is not available or closed (loop: {self._loop!r})."
+ )
return "Code execution was cancelled.", 1
async def _execute_code_dont_check_setup(
@@ -429,15 +447,52 @@ $functions"""
client = docker.from_env()
try:
- container = await asyncio.to_thread(client.containers.get, self.container_name)
- # Wait for all cancellation tasks to finish before stopping the container.
- await asyncio.gather(*self._cancellation_tasks)
- # Stop the container.
+ try:
+ container = await asyncio.to_thread(client.containers.get, self.container_name)
+ except NotFound:
+ logging.debug(f"Container {self.container_name} not found during stop...")
+ self._running = False
+ self._cancellation_futures.clear()
+ return
+
+ if self._cancellation_futures:
+ if not self._loop or self._loop.is_closed():
+ logging.warning(
+ f"Executor loop ({self._loop!r}) is closed or unavailable. Cannot reliably wait for "
+ f"{len(self._cancellation_futures)} cancellation futures."
+ )
+ self._cancellation_futures.clear()
+ else:
+ # concurrent.futures.Future -> asyncio.Future
+ asyncio_futures = [asyncio.wrap_future(f, loop=self._loop) for f in self._cancellation_futures]
+
+ if asyncio_futures:
+ logging.debug(
+ f"Waiting for {len(asyncio_futures)} cancellation futures to complete on loop {self._loop!r}..."
+ )
+ results = await asyncio.gather(*asyncio_futures, return_exceptions=True)
+ for i, result in enumerate(results):
+ original_future = self._cancellation_futures[i]
+ if isinstance(result, Exception):
+ logging.warning(f"Cancellation future {original_future!r} failed: {result}")
+ else:
+ logging.debug(f"Cancellation future {original_future!r} completed successfully.")
+ else:
+ logging.debug("No valid cancellation futures to await.")
+
+ self._cancellation_futures.clear()
+
+ logging.debug(f"Stopping container {self.container_name}...")
await asyncio.to_thread(container.stop)
- except NotFound:
- pass
+ logging.debug(f"Container {self.container_name} stopped.")
+
+ except DockerException as e:
+ logging.error(f"Docker error while stopping container {self.container_name}: {e}")
+ except Exception as e:
+ logging.exception(f"Unexpected error during stop operation for container {self.container_name}: {e}")
finally:
self._running = False
+ self._cancellation_futures.clear()
async def start(self) -> None:
"""(Experimental) Start the code executor.
@@ -511,6 +566,10 @@ $functions"""
logs_str = self._container.logs().decode("utf-8")
raise ValueError(f"Failed to start container from image {self._image}. Logs: {logs_str}")
+ self._loop = asyncio.get_running_loop()
+ self._cancellation_futures = []
+ logging.debug(f"Executor started, associated with event loop: {self._loop!r}")
+
self._running = True
def _to_config(self) -> DockerCommandLineCodeExecutorConfig:
diff --git a/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py b/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py
index 17bb2e7a6..a2923d108 100644
--- a/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py
+++ b/python/packages/autogen-ext/tests/code_executors/test_docker_commandline_code_executor.py
@@ -361,3 +361,27 @@ async def test_delete_tmp_files() -> None:
assert result.code_file is not None
# Verify file is deleted even after error
assert not Path(result.code_file).exists()
+
+
+async def test_docker_commandline_code_executor_with_multiple_tasks() -> None:
+ if not docker_tests_enabled():
+ pytest.skip("Docker tests are disabled")
+
+ async def run_cancellation_scenario(executor: DockerCommandLineCodeExecutor) -> None:
+ token = CancellationToken()
+ code_block = CodeBlock(language="bash", code="sleep 10")
+ exec_task = asyncio.create_task(executor.execute_code_blocks([code_block], cancellation_token=token))
+ await asyncio.sleep(1)
+ token.cancel()
+ try:
+ await exec_task
+ except asyncio.CancelledError:
+ pass
+
+ def run_scenario_in_new_loop(executor_instance: DockerCommandLineCodeExecutor) -> None:
+ asyncio.run(run_cancellation_scenario(executor_instance))
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ async with DockerCommandLineCodeExecutor(work_dir=temp_dir) as executor:
+ await asyncio.get_running_loop().run_in_executor(None, run_scenario_in_new_loop, executor)