powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Bug: Idempotency keeps a cached result an extra hour when daylight saving time ends

Open cleissonom opened this issue 1 week ago • 4 comments

Expected Behaviour

With expires_after_seconds=7200, a second call with the same key after 7,201 elapsed seconds should execute the function again. The configured window is measured in seconds. Idempotency configuration

Current Behaviour

The first call runs at 00:30 EDT on November 2, 2025, before the clocks go back in New York. With the local cache enabled, Powertools still returns the first result three hours later. Running the same code with TZ=UTC expires the record after the configured two hours.

The numbers below are the number of times the function body has executed. A repeated value means the retry received the cached result.

Elapsed seconds TZ=UTC TZ=America/New_York
0 1 1
60 1 1
7,201 2 1
10,800 2 1
10,801 2 2

Code snippet

# /// script
# requires-python = ">=3.12"
# dependencies = ["aws-lambda-powertools[aws-sdk]==3.41.0", "time-machine"]
# ///
from datetime import datetime
from itertools import count
from types import SimpleNamespace

import time_machine
from aws_lambda_powertools.utilities.idempotency import (
    BasePersistenceLayer,
    IdempotencyConfig,
    idempotent_function,
)


class CacheOnlyStore(BasePersistenceLayer):
    def _put_record(self, data_record):
        pass

    def _update_record(self, data_record):
        pass

    def _delete_record(self, data_record):
        pass

    def _get_record(self, idempotency_key):
        raise AssertionError("unexpected store read")


config = IdempotencyConfig(expires_after_seconds=7200, use_local_cache=True)
config.register_lambda_context(SimpleNamespace(get_remaining_time_in_millis=lambda: 30000))
calls = count(1)


@idempotent_function(data_keyword_argument="order", persistence_store=CacheOnlyStore(), config=config)
def process(order):
    return next(calls)


start = datetime.fromisoformat("2025-11-02T04:30:00+00:00").timestamp()
for elapsed in (0, 60, 7201, 10800, 10801):
    with time_machine.travel(start + elapsed, tick=False):
        print(elapsed, process(order="A"))

Steps to Reproduce

Save the snippet as repro_idempotency_dst.py and run it once in each time zone on macOS or Linux. The script uses a no-op persistence layer so all replays go through the Powertools local cache.

TZ=UTC uv run --python 3.12.15 repro_idempotency_dst.py
TZ=America/New_York uv run --python 3.12.15 repro_idempotency_dst.py

Possible Solution

The expiry calculation adds a duration to a naive local datetime before converting it to a Unix timestamp. Across this transition, adding two wall-clock hours advances the timestamp by three hours. Computing the deadline from the current Unix timestamp plus expires_after_seconds would preserve the elapsed duration. Expiry calculation

Did you run the reproduction yourself?

I used an AI assistant to draft this report and execute the snippet locally. The two runs produced the results in the table above. A public-API test also reproduced the failure on develop at 0bbc252e.

Powertools for AWS Lambda (Python) version

3.41.0

AWS Lambda function runtime

CPython 3.12.15 on macOS. The reproduction exercises the local cache. A deployed Lambda function and DynamoDB were outside the test.

Packaging format used

PyPI

cleissonom avatar Nov 25 '26 19:11 cleissonom

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out our GitHub Discussions: Join the conversation

I'd like to take this. I have a fix with tests ready on a branch and will follow AGENTS.md. I'll note AI assistance (Claude Code, reviewed and tested by me) in the PR.

ibrhmyi avatar Nov 26 '26 03:11 ibrhmyi

I've opened a draft fix in #8915. It also fixes the opposite case, where records expired an hour early when the clocks went forward, and the same problem in the in-progress expiry. It stays a draft until a maintainer triages this issue.

@ibrhmyi, thanks for offering. Since I reported this one, I've gone ahead with the fix above; a review there is welcome.

cleissonom avatar Nov 26 '26 13:11 cleissonom

Thanks @cleissonom. I read #8915 and it covers more than my fix did (the clocks-forward case and the in-progress expiry), so I'm stepping back and won't open a competing PR.

ibrhmyi avatar Nov 26 '26 21:11 ibrhmyi