execution.py


Overview

The execution.py file provides core functionality to execute user-submitted code snippets in a secure, isolated environment using Docker containers. It supports asynchronous execution of Python and Node.js code, managing container allocation, code preparation, execution, result collection, and cleanup.

This file handles:

It is a critical component of the InfiniFlow system's code execution pipeline, enabling safe and scalable dynamic code evaluation.


Classes and Functions

1. async def execute_code(req: CodeExecutionRequest) -> CodeExecutionResult

Purpose

Asynchronously executes given code in a Docker container sandbox according to the specified programming language and input arguments.

Parameters

Returns

Description

Usage Example

from models.schemas import CodeExecutionRequest
from models.enums import SupportLanguage

req = CodeExecutionRequest(
    language=SupportLanguage.PYTHON,
    code_b64=base64.b64encode(b"def main(x): return x * 2").decode(),
    arguments={"x": 5}
)

result = await execute_code(req)
print(result.stdout)  # Expected output: "10"

2. def analyze_error_result(stderr: str, exit_code: int) -> CodeExecutionResult

Purpose

Analyzes stderr output and exit code from the containerized code execution to classify the error type and provide a structured response.

Parameters

Returns

Description

Usage Example

stderr = "Permission denied: cannot open file"
exit_code = 1
result = analyze_error_result(stderr, exit_code)
print(result.status)  # ResultStatus.UNAUTHORIZED_ACCESS

Important Implementation Details


Interaction with Other System Components

This file acts as a bridge between the user-facing code submission interface and the underlying containerized execution infrastructure.


Mermaid Diagram: Class and Function Structure

classDiagram
    class execution.py {
        <<module>>
        +async execute_code(req: CodeExecutionRequest) CodeExecutionResult
        +analyze_error_result(stderr: str, exit_code: int) CodeExecutionResult
    }

Summary

The execution.py file is a robust, asynchronous code execution engine integral to InfiniFlow's runtime environment. It carefully orchestrates container allocation, secure code preparation, execution with argument passing, result collection, error classification, and resource cleanup. Supporting multiple languages (currently Python and Node.js), it ensures safe and scalable code evaluation utilizing Docker container isolation and detailed error handling.

This module interacts closely with container management, configuration, logging, and utility components to provide a seamless and secure execution experience.