Skip to content

Commit c354d33

Browse files
Stefan9283stefantm
authored andcommitted
Switch thread polling to a denylist with OCI version fallback
1 parent 46e4c6a commit c354d33

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

awslambdaric/lambda_config.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
"""
44

55
import os
6+
import sys
67

78

89
class LambdaConfigProvider:
9-
SUPPORTED_THREADPOLLING_ENVS = {
10-
"AWS_Lambda_python3.12",
11-
"AWS_Lambda_python3.13",
12-
"AWS_Lambda_python3.14",
13-
"AWS_Lambda_python3.15",
10+
UNSUPPORTED_THREADPOLLING_ENVS = {
11+
"AWS_Lambda_python3.9",
12+
"AWS_Lambda_python3.10",
13+
"AWS_Lambda_python3.11",
1414
}
15+
1516
SOCKET_PATH_ENV = "_LAMBDA_TELEMETRY_LOG_FD_PROVIDER_SOCKET"
1617
AWS_LAMBDA_RUNTIME_API = "AWS_LAMBDA_RUNTIME_API"
1718
AWS_LAMBDA_MAX_CONCURRENCY = "AWS_LAMBDA_MAX_CONCURRENCY"
@@ -38,10 +39,9 @@ def _parse_concurrency(self):
3839
return self._environ.get(self.AWS_LAMBDA_MAX_CONCURRENCY)
3940

4041
def _parse_thread_polling(self):
41-
return (
42-
self._environ.get(self.AWS_EXECUTION_ENV)
43-
in self.SUPPORTED_THREADPOLLING_ENVS
44-
)
42+
if self._environ.get(self.AWS_EXECUTION_ENV) in self.UNSUPPORTED_THREADPOLLING_ENVS:
43+
return False
44+
return sys.version_info >= (3, 4)
4545

4646
def _parse_lmi_socket_path(self):
4747
return self._environ.get(self.SOCKET_PATH_ENV)

tests/test_lambda_config.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import os
6+
import sys
67
import unittest
78
from awslambdaric.lambda_config import LambdaConfigProvider
89

@@ -41,16 +42,45 @@ def test_concurrency_and_is_multi_concurrent(self):
4142
self.assertIsNone(cfg2.max_concurrency)
4243
self.assertFalse(cfg2.is_multi_concurrent)
4344

44-
def test_use_thread_polling_flag(self):
45+
def test_use_thread_polling_disabled_for_unsupported_managed_envs(self):
46+
# Managed runtimes on the denylist never use thread polling,
47+
# regardless of the Python version the code happens to run on.
48+
for exec_env in LambdaConfigProvider.UNSUPPORTED_THREADPOLLING_ENVS:
49+
env = {
50+
"AWS_LAMBDA_RUNTIME_API": "a",
51+
"AWS_EXECUTION_ENV": exec_env,
52+
}
53+
cfg = LambdaConfigProvider(["p", "h.fn"], environ=env)
54+
self.assertFalse(
55+
cfg.use_thread_polling,
56+
msg=f"expected thread polling disabled for {exec_env}",
57+
)
58+
59+
def test_use_thread_polling_enabled_for_custom_oci_image(self):
60+
# Custom OCI images (AWS_Lambda_Image) are not on the denylist and
61+
# fall back to the minimum-supported Python version check.
62+
env = {
63+
"AWS_LAMBDA_RUNTIME_API": "a",
64+
"AWS_EXECUTION_ENV": "AWS_Lambda_Image",
65+
}
66+
cfg = LambdaConfigProvider(["p", "h.fn"], environ=env)
67+
self.assertEqual(cfg.use_thread_polling, sys.version_info >= (3, 4))
68+
69+
def test_use_thread_polling_enabled_for_supported_managed_env(self):
70+
# Managed runtimes not on the denylist (e.g. newer versions) fall
71+
# back to the Python version check.
4572
env = {
4673
"AWS_LAMBDA_RUNTIME_API": "a",
4774
"AWS_EXECUTION_ENV": "AWS_Lambda_python3.12",
4875
}
4976
cfg = LambdaConfigProvider(["p", "h.fn"], environ=env)
50-
self.assertTrue(cfg.use_thread_polling)
51-
env2 = {"AWS_LAMBDA_RUNTIME_API": "a", "AWS_EXECUTION_ENV": "OTHER"}
52-
cfg2 = LambdaConfigProvider(["p", "h.fn"], environ=env2)
53-
self.assertFalse(cfg2.use_thread_polling)
77+
self.assertEqual(cfg.use_thread_polling, sys.version_info >= (3, 4))
78+
79+
def test_use_thread_polling_without_execution_env(self):
80+
# With no AWS_EXECUTION_ENV set, fall back to the version check.
81+
env = {"AWS_LAMBDA_RUNTIME_API": "a"}
82+
cfg = LambdaConfigProvider(["p", "h.fn"], environ=env)
83+
self.assertEqual(cfg.use_thread_polling, sys.version_info >= (3, 4))
5484

5585
def test_lmi_socket_path_property(self):
5686
env = {

0 commit comments

Comments
 (0)