Skip to content

Commit 0637145

Browse files
fix(idempotency): treat empty or unparsable POWERTOOLS_IDEMPOTENCY_DISABLED as not disabled (#8734)
* fix(idempotency): treat empty or unparsable POWERTOOLS_IDEMPOTENCY_DISABLED as not disabled strtobool("") raised ValueError, so an empty or non-boolean value crashed every invocation before the handler ran. Resolve the variable once in a helper that returns False (with a debug log) for values strtobool rejects, and use it in both idempotent and idempotent_function. * fix(idempotency): warn with PowertoolsUserWarning when the disabled flag is ignored * fix(idempotency): use the shared resolve_bool_env_var for POWERTOOLS_IDEMPOTENCY_DISABLED --------- Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent 3f54976 commit 0637145

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎aws_lambda_powertools/utilities/idempotency/idempotency.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
import functools
88
import logging
9-
import os
109
import sys
1110
import warnings
1211
from inspect import isclass, unwrap
1312
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, get_type_hints
1413

1514
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
1615
from aws_lambda_powertools.shared import constants
17-
from aws_lambda_powertools.shared.functions import strtobool
16+
from aws_lambda_powertools.shared.functions import resolve_bool_env_var
1817
from aws_lambda_powertools.shared.types import AnyCallableT
1918
from aws_lambda_powertools.utilities.idempotency.base import IdempotencyHandler
2019
from aws_lambda_powertools.utilities.idempotency.config import IdempotencyConfig
@@ -107,7 +106,7 @@ def handler(event, context):
107106

108107
# Skip idempotency controls when POWERTOOLS_IDEMPOTENCY_DISABLED has a truthy value
109108
# Raises a warning if not running in development mode
110-
if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "false")):
109+
if resolve_bool_env_var(constants.IDEMPOTENCY_DISABLED_ENV):
111110
warnings.warn(
112111
message="Disabling idempotency is intended for development environments only "
113112
"and should not be used in production.",
@@ -245,7 +244,7 @@ def process_order(customer_id: str, order: dict, **kwargs):
245244
def decorate(*args, **kwargs):
246245
# Skip idempotency controls when POWERTOOLS_IDEMPOTENCY_DISABLED has a truthy value
247246
# Raises a warning if not running in development mode
248-
if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "false")):
247+
if resolve_bool_env_var(constants.IDEMPOTENCY_DISABLED_ENV):
249248
warnings.warn(
250249
message="Disabling idempotency is intended for development environments only "
251250
"and should not be used in production.",

‎tests/functional/idempotency/_boto3/test_idempotency.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,44 @@ def dummy_handler(event, context):
18921892
assert len(persistence_store.client.method_calls) == 4
18931893

18941894

1895+
@pytest.mark.parametrize("idempotency_disabled_value", ["", " ", "maybe", None])
1896+
def test_idempotency_disabled_envvar_empty_or_unparsable(
1897+
monkeypatch,
1898+
lambda_context,
1899+
persistence_store: DynamoDBPersistenceLayer,
1900+
idempotency_disabled_value,
1901+
):
1902+
# Scenario to validate an empty, unset or unparsable 'POWERTOOLS_IDEMPOTENCY_DISABLED'
1903+
# keeps idempotency enabled instead of crashing the invocation
1904+
mock_event = {"data": "value"}
1905+
1906+
persistence_store.client = MagicMock()
1907+
1908+
if idempotency_disabled_value is None:
1909+
monkeypatch.delenv("POWERTOOLS_IDEMPOTENCY_DISABLED", raising=False)
1910+
else:
1911+
monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", idempotency_disabled_value)
1912+
1913+
@idempotent_function(data_keyword_argument="data", persistence_store=persistence_store)
1914+
def dummy(data):
1915+
return {"message": "hello"}
1916+
1917+
@idempotent(persistence_store=persistence_store)
1918+
def dummy_handler(event, context):
1919+
return {"message": "hi"}
1920+
1921+
if idempotency_disabled_value is None:
1922+
assert dummy(data=mock_event) == {"message": "hello"}
1923+
assert dummy_handler(mock_event, lambda_context) == {"message": "hi"}
1924+
else:
1925+
with pytest.warns(PowertoolsUserWarning, match="unrecognised value"):
1926+
assert dummy(data=mock_event) == {"message": "hello"}
1927+
with pytest.warns(PowertoolsUserWarning, match="unrecognised value"):
1928+
assert dummy_handler(mock_event, lambda_context) == {"message": "hi"}
1929+
1930+
assert len(persistence_store.client.method_calls) == 4
1931+
1932+
18951933
@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": True}], indirect=True)
18961934
def test_idempotent_function_duplicates(
18971935
idempotency_config: IdempotencyConfig,

0 commit comments

Comments
 (0)