Skip to content

Commit 7a02448

Browse files
committed
fix(rc): skip shared memory allocation in AWS Lambda
AWS Lambda does not provide /dev/shm, so the multiprocessing.Array allocation in PublisherSubscriberConnector always fails with FileNotFoundError. Since the single-subscriber refactor (74c3ab4) the connector is eagerly created at import time via the module-level remoteconfig_poller singleton, causing a noisy warning on every cold start even when remote configuration is disabled. Check in_aws_lambda() before attempting the allocation and fall back directly to _DummySharedArray without logging a warning, since this is expected behavior in Lambda — not an error condition. Resolves: DataDog/datadog-lambda-python#785
1 parent a5ff574 commit 7a02448

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

ddtrace/internal/remoteconfig/_connectors.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,26 @@ class PublisherSubscriberConnector:
4343
"""
4444

4545
def __init__(self):
46-
try:
47-
self.data = get_mp_context().Array("c", SHARED_MEMORY_SIZE, lock=False)
48-
# FileNotFoundError: /dev/shm may not exist or be inaccessible.
49-
# ImportError: multiprocessing.Array imports multiprocessing.sharedctypes,
50-
# which imports ctypes and requires the _ctypes C extension module. Some
51-
# environments (e.g. Alpine Linux, minimal Docker images, or custom
52-
# Python builds without libffi) do not provide _ctypes and raise
53-
# ModuleNotFoundError.
54-
# See: https://app.datadoghq.com/error-tracking/issue/25b34008-bb9f-11f0-abbd-da7ad0900002
55-
except (FileNotFoundError, ImportError):
56-
log.warning(
57-
"Unable to create shared memory. Features relying on remote configuration will not work as expected."
58-
)
46+
from ddtrace.internal.serverless import in_aws_lambda
47+
48+
if in_aws_lambda():
5949
self.data = _DummySharedArray()
50+
else:
51+
try:
52+
self.data = get_mp_context().Array("c", SHARED_MEMORY_SIZE, lock=False)
53+
# FileNotFoundError: /dev/shm may not exist or be inaccessible.
54+
# ImportError: multiprocessing.Array imports multiprocessing.sharedctypes,
55+
# which imports ctypes and requires the _ctypes C extension module. Some
56+
# environments (e.g. Alpine Linux, minimal Docker images, or custom
57+
# Python builds without libffi) do not provide _ctypes and raise
58+
# ModuleNotFoundError.
59+
# See: https://app.datadoghq.com/error-tracking/issue/25b34008-bb9f-11f0-abbd-da7ad0900002
60+
except (FileNotFoundError, ImportError):
61+
log.warning(
62+
"Unable to create shared memory. "
63+
"Features relying on remote configuration will not work as expected."
64+
)
65+
self.data = _DummySharedArray()
6066
# Checksum attr validates if the Publisher send new data
6167
self.checksum = -1
6268
# shared_data_counter attr validates if the Subscriber send new data
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
remote config: Fixes a spurious ``Unable to create shared memory`` warning
5+
on every AWS Lambda cold start by skipping the ``multiprocessing.Array``
6+
allocation when running in Lambda, where ``/dev/shm`` is unavailable.

tests/internal/remoteconfig/test_remoteconfig_connector.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ def test_connector_fallback_on_array_creation_failure():
6868
assert isinstance(connector.data, _DummySharedArray)
6969

7070

71+
def test_connector_uses_dummy_shared_array_in_aws_lambda(monkeypatch):
72+
"""In AWS Lambda, shared memory (/dev/shm) is unavailable. The connector
73+
should skip the allocation attempt entirely and use _DummySharedArray
74+
without logging a warning."""
75+
from ddtrace.internal.remoteconfig._connectors import _DummySharedArray
76+
77+
monkeypatch.setenv("AWS_LAMBDA_FUNCTION_NAME", "my-function")
78+
connector = PublisherSubscriberConnector()
79+
assert isinstance(connector.data, _DummySharedArray)
80+
81+
7182
global_connector = PublisherSubscriberConnector()
7283

7384

0 commit comments

Comments
 (0)