an error "Code execution service is unavailable" when using the code execution function in the workflow. #4022

Closed
opened 2026-02-21 18:04:27 -05:00 by yindo · 3 comments
Owner

Originally created by @hponiang on GitHub (Jun 9, 2024).

Self Checks

  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit report (我已阅读并同意 Language Policy).
  • Please do not modify this template :) and fill in all the required fields.

Provide a description of requested docs changes

I installed the project locally following the community version. To access the OpenAI API, I used a local proxy and set the proxy on the host machine with: http_proxy=".....". The project runs normally, but I encounter an error "Code execution service is unavailable" when using the code execution function in the workflow. How should I adjust the settings?

Originally created by @hponiang on GitHub (Jun 9, 2024). ### Self Checks - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] Please do not modify this template :) and fill in all the required fields. ### Provide a description of requested docs changes I installed the project locally following the community version. To access the OpenAI API, I used a local proxy and set the proxy on the host machine with: http_proxy=".....". The project runs normally, but I encounter an error "Code execution service is unavailable" when using the code execution function in the workflow. How should I adjust the settings?
yindo added the 🐞 bug🌊 feat:workflow labels 2026-02-21 18:04:27 -05:00
yindo closed this issue 2026-02-21 18:04:27 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jun 9, 2024):

To resolve the "Code execution service is unavailable" error when using the code execution function in the workflow after setting up a local proxy for the OpenAI API, you need to ensure that the CODE_EXECUTION_ENDPOINT and CODE_EXECUTION_API_KEY settings are correctly configured.

In the api/config.py file, adjust these settings as follows:

DEFAULTS = {
    # Other settings...
    'CODE_EXECUTION_ENDPOINT': 'http://your-local-proxy-endpoint:port',
    'CODE_EXECUTION_API_KEY': 'your-api-key',
    # Other settings...
}

Replace 'http://your-local-proxy-endpoint:port' with the actual URL and port of your local proxy and 'your-api-key' with the correct API key.

Additionally, ensure that these environment variables are set correctly in your environment or .env file:

CODE_EXECUTION_ENDPOINT=http://your-local-proxy-endpoint:port
CODE_EXECUTION_API_KEY=your-api-key

Refer to the .env.example file for the correct format and placement of these variables:

# CODE EXECUTION CONFIGURATION
CODE_EXECUTION_ENDPOINT=http://your-local-proxy-endpoint:port
CODE_EXECUTION_API_KEY=your-api-key

In the api/core/helper/code_executor/code_executor.py file, ensure that the CODE_EXECUTION_ENDPOINT and CODE_EXECUTION_API_KEY are being fetched correctly:

from config import get_env

# Code Executor
CODE_EXECUTION_ENDPOINT = get_env('CODE_EXECUTION_ENDPOINT')
CODE_EXECUTION_API_KEY = get_env('CODE_EXECUTION_API_KEY')

CODE_EXECUTION_TIMEOUT = (10, 60)

The execute_code method should handle the code execution and raise appropriate exceptions if the service is unavailable:

class CodeExecutor:
    @classmethod
    def execute_code(cls, language: Literal['python3', 'javascript', 'jinja2'], preload: str, code: str, dependencies: Optional[list[CodeDependency]] = None) -> str:
        url = URL(CODE_EXECUTION_ENDPOINT) / 'v1' / 'sandbox' / 'run'
        headers = {'X-Api-Key': CODE_EXECUTION_API_KEY}
        data = {
            'language': cls.code_language_to_running_language.get(language),
            'code': code,
            'preload': preload,
            'enable_network': True
        }
        if dependencies:
            data['dependencies'] = [dependency.dict() for dependency in dependencies]
        try:
            response = post(str(url), json=data, headers=headers, timeout=CODE_EXECUTION_TIMEOUT)
            if response.status_code == 503:
                raise CodeExecutionException('Code execution service is unavailable')
            elif response.status_code != 200:
                raise Exception(f'Failed to execute code, got status code {response.status_code}, please check if the sandbox service is running')
        except CodeExecutionException as e:
            raise e
        except Exception as e:
            raise CodeExecutionException('Failed to execute code, which is likely a network issue, please check if the sandbox service is running. (Error: {str(e)})')
        try:
            response = response.json()
        except:
            raise CodeExecutionException('Failed to parse response')
        response = CodeExecutionResponse(**response)
        if response.code != 0:
            raise CodeExecutionException(response.message)
        if response.data.error:
            raise CodeExecutionException(response.data.error)
        return response.data.stdout

This should help resolve the "Code execution service is unavailable" error [1][2][3].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Jun 9, 2024): <!-- Answer --> To resolve the "Code execution service is unavailable" error when using the code execution function in the workflow after setting up a local proxy for the OpenAI API, you need to ensure that the `CODE_EXECUTION_ENDPOINT` and `CODE_EXECUTION_API_KEY` settings are correctly configured. In the `api/config.py` file, adjust these settings as follows: ```py DEFAULTS = { # Other settings... 'CODE_EXECUTION_ENDPOINT': 'http://your-local-proxy-endpoint:port', 'CODE_EXECUTION_API_KEY': 'your-api-key', # Other settings... } ``` Replace `'http://your-local-proxy-endpoint:port'` with the actual URL and port of your local proxy and `'your-api-key'` with the correct API key. Additionally, ensure that these environment variables are set correctly in your environment or `.env` file: ```env CODE_EXECUTION_ENDPOINT=http://your-local-proxy-endpoint:port CODE_EXECUTION_API_KEY=your-api-key ``` Refer to the `.env.example` file for the correct format and placement of these variables: ```example # CODE EXECUTION CONFIGURATION CODE_EXECUTION_ENDPOINT=http://your-local-proxy-endpoint:port CODE_EXECUTION_API_KEY=your-api-key ``` In the `api/core/helper/code_executor/code_executor.py` file, ensure that the `CODE_EXECUTION_ENDPOINT` and `CODE_EXECUTION_API_KEY` are being fetched correctly: ```py from config import get_env # Code Executor CODE_EXECUTION_ENDPOINT = get_env('CODE_EXECUTION_ENDPOINT') CODE_EXECUTION_API_KEY = get_env('CODE_EXECUTION_API_KEY') CODE_EXECUTION_TIMEOUT = (10, 60) ``` The `execute_code` method should handle the code execution and raise appropriate exceptions if the service is unavailable: ```py class CodeExecutor: @classmethod def execute_code(cls, language: Literal['python3', 'javascript', 'jinja2'], preload: str, code: str, dependencies: Optional[list[CodeDependency]] = None) -> str: url = URL(CODE_EXECUTION_ENDPOINT) / 'v1' / 'sandbox' / 'run' headers = {'X-Api-Key': CODE_EXECUTION_API_KEY} data = { 'language': cls.code_language_to_running_language.get(language), 'code': code, 'preload': preload, 'enable_network': True } if dependencies: data['dependencies'] = [dependency.dict() for dependency in dependencies] try: response = post(str(url), json=data, headers=headers, timeout=CODE_EXECUTION_TIMEOUT) if response.status_code == 503: raise CodeExecutionException('Code execution service is unavailable') elif response.status_code != 200: raise Exception(f'Failed to execute code, got status code {response.status_code}, please check if the sandbox service is running') except CodeExecutionException as e: raise e except Exception as e: raise CodeExecutionException('Failed to execute code, which is likely a network issue, please check if the sandbox service is running. (Error: {str(e)})') try: response = response.json() except: raise CodeExecutionException('Failed to parse response') response = CodeExecutionResponse(**response) if response.code != 0: raise CodeExecutionException(response.message) if response.data.error: raise CodeExecutionException(response.data.error) return response.data.stdout ``` This should help resolve the "Code execution service is unavailable" error <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/config.py)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/api/tests/integration_tests/.env.example)</sup><sup>[[3]](https://github.com/langgenius/dify/blob/main/api/core/helper/code_executor/code_executor.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@hponiang commented on GitHub (Jun 9, 2024):

@dosu The proxy is for accessing the external network, while the code execution needs to access the local sandbox. My sandbox and Flask API are on the same machine.

@hponiang commented on GitHub (Jun 9, 2024): @dosu The proxy is for accessing the external network, while the code execution needs to access the local sandbox. My sandbox and Flask API are on the same machine.
Author
Owner

@dosubot[bot] commented on GitHub (Jun 9, 2024):

To ensure the code execution function can access the local sandbox correctly, you need to make sure that the sandbox service is properly configured and connected to the necessary network. Here are the key settings to check and ensure:

  1. Environment Variables: Ensure that the environment variables for the sandbox service are correctly set.

    environment:
      API_KEY: dify-sandbox
      GIN_MODE: 'release'
      WORKER_TIMEOUT: 15
      ENABLE_NETWORK: 'true'
      HTTP_PROXY: 'http://ssrf_proxy:3128'
      HTTPS_PROXY: 'http://ssrf_proxy:3128'
      SANDBOX_PORT: 8194
      CODE_EXECUTION_ENDPOINT: "http://sandbox:8194"
      CODE_EXECUTION_API_KEY: dify-sandbox
    
  2. Volumes: Ensure that the volume for dependencies is correctly mounted.

    volumes:
      - ./volumes/sandbox/dependencies:/dependencies
    
  3. Networks: Ensure that the sandbox service is connected to the ssrf_proxy_network.

    networks:
      - ssrf_proxy_network
    
  4. Network Configuration: Ensure that the ssrf_proxy_network is properly defined and set as internal.

    networks:
      ssrf_proxy_network:
        driver: bridge
        internal: true
    
  5. Proxy Configuration: Ensure that the proxy server is correctly configured to allow access to the sandbox. Based on the squid.conf file, the proxy server is set up to handle requests to the sandbox.

    ################################## Reverse Proxy To Sandbox ################################
    http_port 8194 accel vhost
    cache_peer sandbox parent 8194 0 no-query originserver
    acl src_all src all
    http_access allow src_all
    

Additionally, ensure that the CODE_EXECUTION_ENDPOINT and CODE_EXECUTION_API_KEY are correctly set in your code execution function. Here is an example from the code_executor.py file:

# Code Executor
CODE_EXECUTION_ENDPOINT = get_env('CODE_EXECUTION_ENDPOINT')
CODE_EXECUTION_API_KEY = get_env('CODE_EXECUTION_API_KEY')

CODE_EXECUTION_TIMEOUT = (10, 60)

class CodeExecutionException(Exception):
    pass

class CodeExecutionResponse(BaseModel):
    class Data(BaseModel):
        stdout: Optional[str]
        error: Optional[str]

    code: int
    message: str
    data: Data

class CodeExecutor:
    @classmethod
    def execute_code(cls, 
                     language: Literal['python3', 'javascript', 'jinja2'], 
                     preload: str, 
                     code: str, 
                     dependencies: Optional[list[CodeDependency]] = None) -> str:
        url = URL(CODE_EXECUTION_ENDPOINT) / 'v1' / 'sandbox' / 'run'

        headers = {
            'X-Api-Key': CODE_EXECUTION_API_KEY
        }

        data = {
            'language': cls.code_language_to_running_language.get(language),
            'code': code,
            'preload': preload,
            'enable_network': True
        }

        if dependencies:
            data['dependencies'] = [dependency.dict() for dependency in dependencies]
        try:
            response = post(str(url), json=data, headers=headers, timeout=CODE_EXECUTION_TIMEOUT)
            if response.status_code == 503:
                raise CodeExecutionException('Code execution service is unavailable')
            elif response.status_code != 200:
                raise Exception(f'Failed to execute code, got status code {response.status_code}, please check if the sandbox service is running')
        except CodeExecutionException as e:
            raise e
        except Exception as e:
            raise CodeExecutionException('Failed to execute code, which is likely a network issue,'
                                         ' please check if the sandbox service is running.'
                                         f' ( Error: {str(e)} )')
        
        try:
            response = response.json()
        except:
            raise CodeExecutionException('Failed to parse response')
        
        response = CodeExecutionResponse(**response)

        if response.code != 0:
            raise CodeExecutionException(response.message)
        
        if response.data.error:
            raise CodeExecutionException(response.data.error)
        
        return response.data.stdout

By verifying and ensuring these settings, the code execution function should be able to access the local sandbox correctly [1][2][3][4][5].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Jun 9, 2024): <!-- Answer --> To ensure the code execution function can access the local sandbox correctly, you need to make sure that the sandbox service is properly configured and connected to the necessary network. Here are the key settings to check and ensure: 1. **Environment Variables**: Ensure that the environment variables for the sandbox service are correctly set. ```yaml environment: API_KEY: dify-sandbox GIN_MODE: 'release' WORKER_TIMEOUT: 15 ENABLE_NETWORK: 'true' HTTP_PROXY: 'http://ssrf_proxy:3128' HTTPS_PROXY: 'http://ssrf_proxy:3128' SANDBOX_PORT: 8194 CODE_EXECUTION_ENDPOINT: "http://sandbox:8194" CODE_EXECUTION_API_KEY: dify-sandbox ``` 2. **Volumes**: Ensure that the volume for dependencies is correctly mounted. ```yaml volumes: - ./volumes/sandbox/dependencies:/dependencies ``` 3. **Networks**: Ensure that the sandbox service is connected to the `ssrf_proxy_network`. ```yaml networks: - ssrf_proxy_network ``` 4. **Network Configuration**: Ensure that the `ssrf_proxy_network` is properly defined and set as internal. ```yaml networks: ssrf_proxy_network: driver: bridge internal: true ``` 5. **Proxy Configuration**: Ensure that the proxy server is correctly configured to allow access to the sandbox. Based on the `squid.conf` file, the proxy server is set up to handle requests to the sandbox. ```conf ################################## Reverse Proxy To Sandbox ################################ http_port 8194 accel vhost cache_peer sandbox parent 8194 0 no-query originserver acl src_all src all http_access allow src_all ``` Additionally, ensure that the `CODE_EXECUTION_ENDPOINT` and `CODE_EXECUTION_API_KEY` are correctly set in your code execution function. Here is an example from the `code_executor.py` file: ```python # Code Executor CODE_EXECUTION_ENDPOINT = get_env('CODE_EXECUTION_ENDPOINT') CODE_EXECUTION_API_KEY = get_env('CODE_EXECUTION_API_KEY') CODE_EXECUTION_TIMEOUT = (10, 60) class CodeExecutionException(Exception): pass class CodeExecutionResponse(BaseModel): class Data(BaseModel): stdout: Optional[str] error: Optional[str] code: int message: str data: Data class CodeExecutor: @classmethod def execute_code(cls, language: Literal['python3', 'javascript', 'jinja2'], preload: str, code: str, dependencies: Optional[list[CodeDependency]] = None) -> str: url = URL(CODE_EXECUTION_ENDPOINT) / 'v1' / 'sandbox' / 'run' headers = { 'X-Api-Key': CODE_EXECUTION_API_KEY } data = { 'language': cls.code_language_to_running_language.get(language), 'code': code, 'preload': preload, 'enable_network': True } if dependencies: data['dependencies'] = [dependency.dict() for dependency in dependencies] try: response = post(str(url), json=data, headers=headers, timeout=CODE_EXECUTION_TIMEOUT) if response.status_code == 503: raise CodeExecutionException('Code execution service is unavailable') elif response.status_code != 200: raise Exception(f'Failed to execute code, got status code {response.status_code}, please check if the sandbox service is running') except CodeExecutionException as e: raise e except Exception as e: raise CodeExecutionException('Failed to execute code, which is likely a network issue,' ' please check if the sandbox service is running.' f' ( Error: {str(e)} )') try: response = response.json() except: raise CodeExecutionException('Failed to parse response') response = CodeExecutionResponse(**response) if response.code != 0: raise CodeExecutionException(response.message) if response.data.error: raise CodeExecutionException(response.data.error) return response.data.stdout ``` By verifying and ensuring these settings, the code execution function should be able to access the local sandbox correctly <sup>[[1]](https://github.com/langgenius/dify/blob/main/docker/docker-compose.middleware.yaml)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/docker/docker-compose.yaml)</sup><sup>[[3]](https://github.com/langgenius/dify-docs/blob/main/en/getting-started/install-self-hosted/local-source-code.md)</sup><sup>[[4]](https://github.com/langgenius/dify/blob/main/docker/volumes/ssrf_proxy/squid.conf)</sup><sup>[[5]](https://github.com/langgenius/dify/blob/main/api/core/helper/code_executor/code_executor.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#4022