Fix "Process exited unexpectedly before completing request"

Updated at:

The function process terminated before completing a request, typically due to incorrect function logic or missing HTTP server keep-alive configuration.

Incorrect function logic

A bug in function code can cause the process to exit prematurely, often involving downstream database operations. In the following Python example, os._exit(-1) terminates the process before the function returns a response:

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

def handler(event, context):
    logger = logging.getLogger()
    logger.info('hello world')
    os._exit(-1)  # Terminates the process immediately
    return 'hello world'

Add logging with logging.getLogger() and check the function logs. Look for unhandled exceptions and explicit exit calls such as os._exit().

Missing keep-alive in custom runtimes

When you use a custom runtime or Custom Container runtime with a function execution timeout under 15 minutes, the HTTP server must have keep-alive enabled. Otherwise, the server may close idle connections and cause the process to exit unexpectedly. The following Node.js example shows the required configuration:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200);
  res.end('OK');
});

server.timeout = 0;
server.keepAliveTimeout = 0;

server.listen(9000, '0.0.0.0');

For more information, see HTTP server requirements.