container.py


Overview

The container.py module is a core part of the InfiniFlow application responsible for managing sandboxed container environments used to safely execute code snippets in supported languages, specifically Python and Node.js. It provides asynchronous lifecycle management of Docker containers, including creation, allocation, release, recreation after crashes, and teardown.

This file ensures efficient container pooling, safe concurrent access, and resource constraint enforcement, enabling isolated and repeatable execution of user or system code within sandboxed containers. The container management supports concurrency via asyncio primitives and enforces memory limits and security configurations for sandbox containers.


Classes and Functions

Module-level Variables


async def init_containers(size: int) -> tuple[int, int]

Initializes the container pools for each supported language by creating a fixed number (size) of Docker containers for Python and Node.js.

success, total = await init_containers(5)
print(f"Created {success}/{total} containers successfully.")

async def teardown_containers()

Gracefully removes all containers from both Python and Node.js queues by stopping and deleting the corresponding Docker containers.


async def _prepare_container(name: str, language: SupportLanguage) -> bool

Internal helper to prepare a single container instance.


async def create_container(name: str, language: SupportLanguage) -> bool

Creates and starts a container asynchronously with the appropriate runtime, resource limits, and base image.

success = await create_container("sandbox_python_1", SupportLanguage.PYTHON)
if success:
    print("Container created successfully.")

async def recreate_container(name: str, language: SupportLanguage) -> bool

Recreates a container by forcefully removing the existing one and creating a new container with the same name and language.

await recreate_container("sandbox_nodejs_3", SupportLanguage.NODEJS)

async def release_container(name: str, language: SupportLanguage)

Releases a container back into the available pool if it is still running; otherwise, attempts to recreate it before returning it to the pool.


async def allocate_container_blocking(language: SupportLanguage, timeout=10) -> str

Attempts to allocate an available container from the pool for the given language, blocking asynchronously until timeout if none are immediately available.

container_name = await allocate_container_blocking(SupportLanguage.PYTHON, timeout=5)
if container_name:
    print(f"Allocated container: {container_name}")
else:
    print("No container available within timeout.")

async def container_is_running(name: str) -> bool

Checks asynchronously whether the specified container is currently running.


Implementation Details & Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class ContainerManager {
        <<module>>
        - _CONTAINER_QUEUES: dict[SupportLanguage, Queue]
        - _CONTAINER_LOCK: asyncio.Lock
        - _CONTAINER_EXECUTION_SEMAPHORES: dict[SupportLanguage, asyncio.Semaphore]
        + init_containers(size: int) tuple[int, int]
        + teardown_containers()
        - _prepare_container(name: str, language: SupportLanguage) bool
        + create_container(name: str, language: SupportLanguage) bool
        + recreate_container(name: str, language: SupportLanguage) bool
        + release_container(name: str, language: SupportLanguage)
        + allocate_container_blocking(language: SupportLanguage, timeout=10) str
        + container_is_running(name: str) bool
    }
    ContainerManager ..> SupportLanguage : uses
    ContainerManager ..> async_run_command : calls
    ContainerManager ..> logger : logs events

Summary

The container.py module in InfiniFlow efficiently manages isolated Docker containers for executing Python and Node.js code in a sandboxed environment. It supports asynchronous operations to create, allocate, release, recreate, and teardown containers with robust concurrency control and health checking. By leveraging Docker features and asyncio synchronization primitives, it ensures secure, scalable, and performant container lifecycle management critical for multi-tenant code execution workloads.