What do I do if I receive a 502 error with the error message Process exited unexpectedly before completing request?

Updated at:

Possible causes

The HTTP server actively closes the connection. The possible causes of the active closure are as follows:

  • Keep-Alive is not configured for the connection.

  • The connection is closed after it remains idle for a period of time.

  • The connection is closed when a read or write operation times out or fails.

Solution

Function Compute reuses Keep-Alive connections to access the HTTP server in a custom image. For idempotent requests such as GET, HEAD, OPTIONS, and TRACE, it automatically retries when a connection fails, for example, with EOF and connection reset by peer. However, for non-idempotent requests such as POST and PATCH, it directly returns a 502 error when a connection fails. To avoid 502 errors, the server side of the custom image must configure the following two types of parameters:

  • Set the connection mode (Connection) to Keep-Alive.

  • Disable the idle timeout, or set the idle timeout to 15 minutes or longer.

The way you configure the preceding two parameters may vary depending on the HTTP server framework. For example, for the GoFrame framework, you must set SetIdleTimeout to 0 and also configure the ReadTimeout and python uvicorn parameters. For python uvicorn, you must also set parameters such as --timeout-keep-alive on the command line. Verify whether an HTTP client in Keep-Alive mode triggers the HTTP server to actively close the connection during infrequent calls.

Possible causes

The process exits because of the function itself. The possible causes are as follows:

  • The function actively calls an interface such as exit to terminate the process.

  • An exception that occurs during runtime is not caught.

Solution

Check your code in the following ways:

  • Check whether your code contains logic that actively exits.

  • Add exception handling or overriding at the top level of the runtime environment process to prevent the process from exiting when an exception occurs.