Resolve the "Process exited unexpectedly before completing request" error

更新时间:
复制 MD 格式

An instance process in a custom runtime exited unexpectedly. The following sections describe possible causes and solutions.

HTTP server connection actively closed

Function Compute maintains a keep-alive connection to the HTTP server of a custom runtime. This connection may close unexpectedly for the following reasons:

  • Keep-alive mode is not configured for the connection.

  • The HTTP server closes the connection after an idle period.

  • A read or write operation times out or fails.

When the connection drops, the behavior differs by request type:

  • Idempotent requests (GET, HEAD, OPTIONS, TRACE): The system automatically retries the connection when an EOF or connection reset by peer error occurs.

  • Non-idempotent requests (POST, PATCH): A 502 error is returned if the connection fails.

Solution

Configure the HTTP server of the custom runtime as follows:

  1. Set the connection mode to keep-alive.

  2. Disable the idle timeout, or set the idle timeout period to more than 15 minutes.

The specific parameters vary by framework. The following table lists examples for commonly used frameworks.

Framework Parameter Recommended value
GoFrame SetIdleTimeout 0 (disabled)
GoFrame SetReadTimeout Set based on your workload
Python Uvicorn --timeout-keep-alive (CLI parameter) Greater than 900 (seconds)

Note: Verify that the HTTP server does not disconnect during sparse invocations from an HTTP client in keep-alive mode.

Function execution exception

The process may exit due to an unhandled exception or an explicit exit call, such as:

  • The function code calls the exit interface.

  • Exceptions during function execution are not caught.

The following example shows a function that calls os._exit(-1) to force-terminate the process:

# -*- coding: utf-8 -*-
import os
import logging

def handler(event, context):
    logger = logging.getLogger()
    logger.info('something is wrong')
    os._exit(-1)
    return 'hello world'

Solution

To resolve this issue:

  1. Check whether your code contains an explicit exit call such as os._exit().

  2. Add exception handling at the top level of the process to prevent unhandled exceptions from terminating it.

Note: Do not use os._exit(-1) to exit a process. This method prevents Function Compute from capturing the stack trace at the time of exit. Instead, raise an exception or manually print the stack trace to logs before the process exits.

OOM due to insufficient memory

The instance process exits when memory usage exceeds the configured limit, triggering an out of memory (OOM) error. To analyze request memory usage, enable request-level metric logs and check the Logs page in the Function Compute console. For more information, see Request-level metric logs.

Solution

Increase the memory specification for the function:

  1. Log on to the Function Compute console.

  2. In the left-side navigation pane, click Functions.

  3. In the top navigation bar, select a region. On the Functions page, click the target function.

  4. On the function details page, click the Configurations tab. In the Instance Configuration section, click Modify.

  5. Increase the memory specification and click Deploy.

Invalid startup command

The startup command may fail for the following reasons:

  • The startup command lacks executable permissions.

  • The file specified in the startup command does not exist.

  • The file format is invalid.

Solution

Verify the startup command configuration. Make sure the target file exists, has executable permissions, and uses a valid format. For more information, see Function instance exited unexpectedly.