Skip to content

Navigation Menu

Sign in
Sign up

Switch thread polling to a denylist with OCI version fallback - #212

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

Switch thread polling to a denylist with OCI version fallback #212
Stefan9283 wants to merge 1 commit into
main from
stefantm/shutdown-hook-fix

Conversation

@Stefan9283

@Stefan9283 Stefan9283 commented Jul 2, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

Issue #, if available: #105

Description of changes:

This change enables graceful shutdown hooks for custom OCI images (when AWS_EXECUTION_ENV is not defined the default is AWS_Lambda_Image) as long as the Python version is high enough for the thread pool to exist.

Also fewer stuff to maintain since we're turning it into a denylist 🎉

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月02日T17:51:39.669+01:00	LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [Platform]	
	2026年07月02日T17:51:40.201+01:00	EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE, SHUTDOWN]
	2026年07月02日T17:51:40.208+01:00	START RequestId: be18ce5e-0ef9-4933-917f-ebfc53522ab7 Version: $LATEST
	2026年07月02日T17:51:40.218+01:00	END RequestId: be18ce5e-0ef9-4933-917f-ebfc53522ab7
	2026年07月02日T17: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月02日T17:57:39.206+01:00	[runtime] SIGTERM received
	2026年07月02日T17:57:39.206+01:00	[runtime] cleaning up
	2026年07月02日T17: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.

gordo-datavant reacted with rocket emoji
Stefan9283 force-pushed the stefantm/shutdown-hook-fix branch from bd8bff7 to 114109c Compare July 3, 2026 11:21
Stefan9283 force-pushed the stefantm/shutdown-hook-fix branch 2 times, most recently from c354d33 to ce46970 Compare August 13, 2026 16:20
@Stefan9283 Stefan9283 changed the title (削除) Infer thread polling config from sys version (削除ここまで) (追記) Switch thread polling to a denylist with OCI version fallback (追記ここまで) Aug 13, 2026
Comment thread awslambdaric/lambda_config.py Outdated
in self.UNSUPPORTED_THREADPOLLING_ENVS
):
return False
return sys.version_info >= (3, 4)

@darklight3it darklight3it Aug 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this simply

sys.version_info>=(3,12)

@Stefan9283 Stefan9283 Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the floor version given the RIC supports 3.9+ (higher than what's needed for the ThreadPoolExecutor)

"AWS_Lambda_python3.13",
"AWS_Lambda_python3.14",
"AWS_Lambda_python3.15",
UNSUPPORTED_THREADPOLLING_ENVS = {

@darklight3it darklight3it Aug 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. If someone is using it, it's going to break. You need to mark that as deprecated and we need to get rid of it in the next major enum release.

Stefan9283 force-pushed the stefantm/shutdown-hook-fix branch from ce46970 to 031eaaa Compare August 25, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@darklight3it darklight3it darklight3it left review comments
@maxday maxday Awaiting requested review from maxday

At least 2 approving reviews are required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /