From d56fd45406e77ea2a4278dc0af411ebc4f7c42c3 Mon Sep 17 00:00:00 2001 From: Cleisson Date: Thu, 26 Nov 2026 10:12:21 -0300 Subject: [PATCH 1/2] test(idempotency): cover record expiry across daylight saving changes --- .../idempotency/_boto3/test_idempotency.py | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/functional/idempotency/_boto3/test_idempotency.py b/tests/functional/idempotency/_boto3/test_idempotency.py index 4e07a5f87d0..d6589f65bd5 100644 --- a/tests/functional/idempotency/_boto3/test_idempotency.py +++ b/tests/functional/idempotency/_boto3/test_idempotency.py @@ -1,6 +1,7 @@ import copy import dataclasses import datetime as dt +import time import warnings from decimal import Decimal from enum import Enum @@ -1021,6 +1022,89 @@ def function(record): ) +class RecordingPersistenceLayer(BasePersistenceLayer): + # Keeps the records it is asked to save. Replays come from the local cache. + def __init__(self): + super().__init__() + self.saved_records: list[DataRecord] = [] + + def _put_record(self, data_record: DataRecord) -> None: + self.saved_records.append(data_record) + + def _update_record(self, data_record: DataRecord) -> None: + self.saved_records.append(data_record) + + def _get_record(self, idempotency_key) -> DataRecord: ... + + def _delete_record(self, data_record: DataRecord) -> None: ... + + +@pytest.fixture +def local_timezone(request): + with pytest.MonkeyPatch.context() as monkeypatch: + monkeypatch.setenv("TZ", request.param) + time.tzset() + yield + time.tzset() + + +@pytest.mark.parametrize( + ("local_timezone", "start"), + [ + ("UTC", "2025-11-02T05:59:59+00:00"), + # 01:59:59 in New York, one second before the clocks go back an hour + ("America/New_York", "2025-11-02T05:59:59+00:00"), + # 01:59:59 in New York, one second before the clocks go forward an hour + ("America/New_York", "2026-03-08T06:59:59+00:00"), + # 01:30 in New York, during the hour that repeats after the clocks go back + ("America/New_York", "2025-11-02T06:30:00+00:00"), + ], + ids=["utc", "dst-ends", "dst-starts", "repeated-hour"], + indirect=["local_timezone"], +) +def test_idempotent_function_expiry_across_daylight_saving_change(local_timezone, start, monkeypatch, lambda_context): + """ + Test idempotent_function when the local UTC offset changes while its records are stored. + Records must expire after the configured number of seconds, whatever the wall clock does. + """ + start_time = dt.datetime.fromisoformat(start).timestamp() + current_time = start_time + + class FrozenDatetime(dt.datetime): + @classmethod + def now(cls, tz=None): + return cls.fromtimestamp(current_time, tz) + + monkeypatch.setattr(dt, "datetime", FrozenDatetime) + + persistence_store = RecordingPersistenceLayer() + config = IdempotencyConfig(expires_after_seconds=7200, use_local_cache=True) + config.register_lambda_context(lambda_context) # one second left in the invocation + calls = 0 + + @idempotent_function(data_keyword_argument="order", persistence_store=persistence_store, config=config) + def process(order): + nonlocal calls + calls += 1 + return calls + + # GIVEN a first call at the start time + assert process(order={"id": 1}) == 1 + + # WHEN the same order arrives 7200 seconds later + current_time += 7200 + # THEN the stored result is returned + assert process(order={"id": 1}) == 1 + + # WHEN it arrives one second after that + current_time += 1 + # THEN the function runs again + assert process(order={"id": 1}) == 2 + + # AND the first in-progress record expired when its invocation ran out of time + assert persistence_store.saved_records[0].in_progress_expiry_timestamp == int(start_time * 1000) + 1000 + + def test_data_record_invalid_status_value(): data_record = DataRecord("key", status="UNSUPPORTED_STATUS") with pytest.raises(IdempotencyInvalidStatusError) as e: From 593e72751ffbe5a0fd46854237d31a9bf28bfc7a Mon Sep 17 00:00:00 2001 From: Cleisson Date: Thu, 26 Nov 2026 10:12:24 -0300 Subject: [PATCH 2/2] fix(idempotency): compute expiry deadlines in UTC --- .../utilities/idempotency/persistence/base.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/aws_lambda_powertools/utilities/idempotency/persistence/base.py b/aws_lambda_powertools/utilities/idempotency/persistence/base.py index 81256b654c6..148f0530214 100644 --- a/aws_lambda_powertools/utilities/idempotency/persistence/base.py +++ b/aws_lambda_powertools/utilities/idempotency/persistence/base.py @@ -275,7 +275,8 @@ def _get_expiry_timestamp(self) -> int: unix timestamp of expiry date for idempotency record """ - now = dt.datetime.now() + # Add the period in UTC. With naive local time, a daylight saving change would add or remove an hour. + now = dt.datetime.now(dt.timezone.utc) period = dt.timedelta(seconds=self.expires_after_seconds) return int((now + period).timestamp()) @@ -387,7 +388,7 @@ def save_inprogress(self, data: dict[str, Any], remaining_time_in_millis: int | # Therefore, we need to check if remaining_time_in_millis is not None (>=0) to handle this case. # See: https://github.com/aws-powertools/powertools-lambda-python/issues/4759 if remaining_time_in_millis is not None: - now = dt.datetime.now() + now = dt.datetime.now(dt.timezone.utc) period = dt.timedelta(milliseconds=remaining_time_in_millis) timestamp = (now + period).timestamp() data_record.in_progress_expiry_timestamp = int(timestamp * 1000)