Skip to content

Infer thread polling config from sys version#212

Open
Stefan9283 wants to merge 1 commit into
mainfrom
stefantm/shutdown-hook-fix
Open

Infer thread polling config from sys version#212
Stefan9283 wants to merge 1 commit into
mainfrom
stefantm/shutdown-hook-fix

Conversation

@Stefan9283

Copy link
Copy Markdown
Contributor

Issue #, if available: #105

Description of changes:

This change backports the graceful shutdown hooks to Python 3.10, 3.11 (not as relevant given the deprecation date for August 2027) and also custom OCI images (the later using AWS_Lambda_Image for AWS_EXECUTION_ENV)

Target (OCI, Managed Runtime, both): both

Testing:

Dockerfile.python310

# Python 3.10 Lambda container image using AWS Lambda base image
FROM public.ecr.aws/lambda/python:3.10 AS build-image

# Install build dependencies
RUN yum install -y \
    gcc \
    gcc-c++ \
    make \
    cmake3 \
    libcurl-devel \
    autoconf \
    automake \
    libtool \
    tar \
    gzip \
    && ln -sf /usr/bin/cmake3 /usr/bin/cmake \
    && yum clean all

# Build the runtime interface client from local source
ARG RIC_BUILD_DIR="/home/build/"
RUN mkdir -p ${RIC_BUILD_DIR}
WORKDIR ${RIC_BUILD_DIR}
COPY . .

RUN pip install setuptools
RUN make init build
RUN mv ./dist/awslambdaric-*.tar.gz ./dist/awslambdaric-test.tar.gz

# Copy the function handler
ARG FUNCTION_DIR="/var/task"
RUN mkdir -p ${FUNCTION_DIR}
COPY tests/integration/test-handlers/shutdown-hook/* ${FUNCTION_DIR}


# Final stage - clean runtime image
FROM public.ecr.aws/lambda/python:3.10

ARG FUNCTION_DIR="/var/task"
WORKDIR ${FUNCTION_DIR}

# Copy the function code
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

# Copy the built RIC tarball and install it, overriding the bundled awslambdaric in /var/runtime
COPY --from=build-image /home/build/dist/awslambdaric-test.tar.gz /tmp/
RUN yum install -y gcc gcc-c++ make cmake3 libcurl-devel tar gzip autoconf automake libtool && \
    ln -sf /usr/bin/cmake3 /usr/bin/cmake && \
    rm -rf /var/runtime/awslambdaric* && \
    rm -f /var/runtime/runtime_client* && \
    pip install /tmp/awslambdaric-test.tar.gz --target /var/runtime && \
    rm /tmp/awslambdaric-test.tar.gz && \
    yum history undo -y last && yum clean all && rm -rf /var/cache/yum

# Install Lambda Insights extension layer (required for graceful shutdown support)
# Download manually before building:
# curl -f -s -o lambda-insights-amd64-64.zip "$(aws lambda get-layer-version-by-arn --region eu-west-1 --arn "arn:aws:lambda:eu-west-1:580247275435:layer:LambdaInsightsExtension:64" --query 'Content.Location' --output text)"
COPY lambda-insights-amd64-64.zip /tmp
# hadolint ignore=DL3033
RUN yum install -y unzip && unzip -q -d /opt /tmp/lambda-insights-amd64-64.zip && rm /tmp/*.zip && \
    yum history undo -y last && yum clean all && rm -rf /var/cache/yum

# Replace AWS_EXECUTION_ENV so the base image's bundled RIC path isn't triggered
RUN sed -i 's/export AWS_EXECUTION_ENV=.*/export AWS_EXECUTION_ENV=AWS_Lambda_Image/' /var/runtime/bootstrap

CMD [ "app.lambda_handler" ]

Handler (tests/integration/test-handlers/shutdown-hook/app.py) - same as the official demo

import json
import platform
import signal
import sys
import time


def exit_gracefully(signum, frame):
    r"""
    SIGTERM Handler: https://docs.aws.amazon.com/lambda/latest/operatorguide/static-initialization.html
    Listening for os signals that can be handled,reference: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html
    Termination Signals: https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
    """
    print("[runtime] SIGTERM received")

    print("[runtime] cleaning up")
    # perform actual clean up work here.
    time.sleep(0.2)

    print("[runtime] exiting")
    sys.exit(0)


signal.signal(signal.SIGTERM, exit_gracefully)


def lambda_handler(event, context):
    return "SUCCESS"

Cloudwatch logs Python 3.10

2026-07-02T17:51:39.669+01:00	LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [Platform]	
	2026-07-02T17:51:40.201+01:00	EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE, SHUTDOWN]
	2026-07-02T17:51:40.208+01:00	START RequestId: be18ce5e-0ef9-4933-917f-ebfc53522ab7 Version: $LATEST
	2026-07-02T17:51:40.218+01:00	END RequestId: be18ce5e-0ef9-4933-917f-ebfc53522ab7
	2026-07-02T17:51:40.218+01:00	REPORT RequestId: be18ce5e-0ef9-4933-917f-ebfc53522ab7 Duration: 3.36 ms Billed Duration: 642 ms Memory Size: 512 MB Max Memory Used: 39 MB Init Duration: 637.70 ms
	2026-07-02T17:57:39.206+01:00	use_thread_for_polling_next True
	2026-07-02T17:57:39.206+01:00	[runtime] SIGTERM received
	2026-07-02T17:57:39.206+01:00	[runtime] cleaning up
	2026-07-02T17:57:39.406+01:00	[runtime] exiting

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant