Skip to content

Bug: Idempotency middleware doesn't work for functions without return value #2987

Closed
@wassil

Description

@wassil

Expected Behavior

When using the makeHandlerIdempotent middleware with a handler function that has no return value(undefined), the middleware should only call the handler once.

Current Behavior

The handler is called multiple times for the same event, even though the idempotency record exists in Dynamo.

The issue is that the package returns the stored return value from previous run from the middleware.
Here: https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/packages/idempotency/src/IdempotencyHandler.ts#L243
And here: https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/packages/idempotency/src/middleware/makeHandlerIdempotent.ts#L140

If the value returned from middleware is truthy, it causes middy to perform an early return(https://middy.js.org/docs/intro/early-interrupt/).
This works for handler that have a return value, but doesn't work when the return value is undefined - middy doesn't early return and the handler code is executed again.

Code snippet

export handler = middy().use(makeHandlerIdempotent(...)).handler(() => console.log("stuff"))

Steps to Reproduce

Use the code snippet, call the lambda multiple times with the same event

Possible Solution

In order to early return, a truthy value has to be returned from the middleware.
This kind of sucks, because the first time a lambda runs, it returns undefined and we'd have to return something else when idempotency key is present.
In our use case, the return value doesn't matter, but I'm not sure what would make sense as a generic solution.

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

No response

Metadata

Metadata

Assignees

Labels

bug-upstreamThis item is related to a bug caused by upstream dependencycompletedThis item is complete and has been merged/shippedidempotencyThis item relates to the Idempotency Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions