Fix "Process exited unexpectedly before completing request"
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.
Enable keep-alive and set the request timeout to 24 hours or longer. 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');
Ensure the HTTP server meets these requirements:
-
Listens on
0.0.0.0:CAPortor*:CAPort, not127.0.0.1. The default port is 9000. -
Starts within 120 seconds.
For the full list of HTTP server requirements, see Requirements on HTTP server configurations.