config.py


Overview

The config.py file in the InfiniFlow project is responsible for configuring and managing the lifecycle of the container execution environment within the application. It primarily sets up asynchronous lifecycle management for the FastAPI application, including initialization and teardown of container pools used to execute sandboxed tasks.

The file reads environment variables to configure global timeout settings and the size of the container executor pool. It provides an asynchronous context manager _lifespan that integrates with FastAPI's lifespan events, ensuring container resources are properly initialized before the app starts serving requests and cleaned up on shutdown.


Detailed Explanation of Components

Constants

TIMEOUT

TIMEOUT = parse_timeout_duration(os.getenv("SANDBOX_TIMEOUT", "10s"))

Functions

_lifespan(app: FastAPI)

app = FastAPI(lifespan=config._lifespan)

Here, the _lifespan function is passed to the FastAPI app to handle startup and shutdown lifecycle.


init()

lifespan = config.init()
app = FastAPI(lifespan=lifespan)

This function is a helper to prepare the lifespan context manager while logging configuration details.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[Start: FastAPI app startup] --> B[_lifespan async context manager]
    B --> C[Read SANDBOX_EXECUTOR_MANAGER_POOL_SIZE]
    C --> D[Call init_containers(size)]
    D --> E[Log container initialization status]
    E --> F[Yield control to FastAPI app running]
    F --> G[App runs: handles requests using containers]
    G --> H[App shutdown triggered]
    H --> I[Call teardown_containers()]
    I --> J[Containers cleaned up]
    J --> K[End: FastAPI app shutdown]

Summary

config.py is a critical configuration and lifecycle management file that ensures container pools used for sandboxed execution are properly initialized and cleaned up in alignment with the FastAPI application's lifecycle. It leverages environment variables for configurability, uses asynchronous context management for seamless startup and shutdown, and integrates logging to provide operational insight.

By exporting a ready-to-use lifespan manager through the init() function, it cleanly encapsulates the complexity of container lifecycle management and supports scalable and robust sandbox execution within the InfiniFlow system.