Bug: Redis layer fails the invocation instead of retrying when a record expires between SET NX and GET

Abierto Apto para principiantes
#8,727 4 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@coughlmao ya está trabajando en esto.

Desde el 1/11/2026.

  • #8736 de @coughlmao — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
85/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python, redis
Área
databases

Línea de trabajo

Edita aws_lambda_powertools/utilities/idempotency/persistence/redis.py en _put_record: captura IdempotencyItemNotFoundError después del fallido SET NX y lanza IdempotencyInconsistentStateError. Añade una prueba con un cliente falso que devuelva None en GET, afirmando que la función decorada se ejecuta. Hecho cuando esa prueba pasa y el snippet en el issue devuelve "ok".

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug good first issue help wanted idempotency

[!WARNING]
For contributors, human or agent. Comment here to claim this issue and wait for the assignment before opening a PR; if two PRs show up, the assignee's stays. AI-assisted work is welcome, say so in the PR and tell us how you verified it: run the tests yourself and paste what you saw, do not describe runs you did not do. Read AGENTS.md first, it has the commands and the rules the CI enforces. This goes into 3.41.0.

Expected Behaviour

When a Redis record expires between the SET NX that fails and the GET that follows, the Redis layer behaves like the DynamoDB layer: it raises IdempotencyInconsistentStateError, which the handler's retry loop turns into a retry, and the invocation succeeds.

Current Behaviour

In CachePersistenceLayer._put_record, the GET after a failed SET NX goes through _get_record, which raises IdempotencyItemNotFoundError. That exception is not handled there, escapes _put_record and is wrapped as IdempotencyPersistenceLayerError, so the invocation fails for a race that is expected and recoverable.

Code snippet
# fake client: SET NX returns None (key existed), the following GET returns None (key expired in between)
layer = CachePersistenceLayer(client=racy_client)

@idempotent_function(data_keyword_argument="order", persistence_store=layer)
def process(order):
    return "ok"

process(order={"id": 1})  # IdempotencyPersistenceLayerError, expected: retried and "ok"

Compare with DynamoDBPersistenceLayer._put_record, where the equivalent race raises IdempotencyInconsistentStateError.

Possible Solution

In aws_lambda_powertools/utilities/idempotency/persistence/redis.py, _put_record: catch IdempotencyItemNotFoundError around the read that follows the failed SET NX and raise IdempotencyInconsistentStateError. Add a test with a fake client that returns None on that GET and assert the decorated function runs.

Item 10 of the Idempotency audit in #8603. No AWS resources needed.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

No response

Lenguaje dominante
Python
Estrellas
3.3k
Forks
511
Merge medio
3 d 8 h
PR fusionados (30 d)
92

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de aws-powertools/powertools-lambda-python

Todos los issues de aws-powertools/powertools-lambda-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.