Skip to content

Incomplete input for hash function for @idempotent_function #855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dispyfree opened this issue Nov 29, 2021 · 5 comments
Closed

Incomplete input for hash function for @idempotent_function #855

dispyfree opened this issue Nov 29, 2021 · 5 comments
Labels
bug Something isn't working p1

Comments

@dispyfree
Copy link

When annotating functions via @idempotent_function, the wrapped function's name will not go into the hash computation; instead, only the payload and an environment-specific (but not function-specific) function name is assumed.
This causes idempotency to break in certain cases.

Expected Behavior

When two distinct functions which accept the very same payload as parameters are wrapped using @idempotent_function, invocation of one function should not affect invocation of the other function.

Current Behavior

Only the payload is considered.
The culprit appears to be

generated_hash = self._generate_hash(data=data)
function_name = os.getenv(constants.LAMBDA_FUNCTION_NAME_ENV, "test-func")

(lines 180-181) in persistence/base.py. One can clearly see that only the environment variable or "test-func" is assumed - but that is - of course - not specific to the actual function being wrapped.
So in short, the library currently only really distinguishes by payload and not be function name, hence causing the aforementioned behaviour.

Possible Solution

Pass on the wrapped function's name and use it for the return value of _get_hashed_idempotency_key.

Steps to Reproduce (for bugs)

def test():
    myIdempotentFunc(product = "bananas")
    anotherIdempotentFunc(product="bananas")
    return {}

@idempotent_function(data_keyword_argument="product", persistence_store=dynamodb)
def myIdempotentFunc(product):
    print("I got invoked with {product}".format(product = product))
    return None

@idempotent_function(data_keyword_argument="product", persistence_store=dynamodb)
def anotherIdempotentFunc(product):
    print("I will never get invoked; someone stole my payload :(".format(product = product))
    return None

This only prints myIdempotentFunc's output, so anotherIdempotentFunc never gets invoked.

Environment

Using "aws-lambda-powertools==1.22.0" on Python 3.9.7

Thank you in advance!

@dispyfree dispyfree added bug Something isn't working triage Pending triage from maintainers labels Nov 29, 2021
@boring-cyborg
Copy link

boring-cyborg bot commented Nov 29, 2021

Thanks for opening your first issue here! We'll come back to you as soon as we can.

@heitorlessa
Copy link
Contributor

hey @dispyfree thank you so much for spotting this - it's definitely an edge case we missed :/. function_name in that case is to be added in the DataRecord, not the actual method name being wrapped -- for that we need to do what you suggested.

We're fixing some papercuts elsewhere and this will be prioritized right after - More than happy to help review/guide the fix if you have some spare cycles too!

Thanks again

@heitorlessa heitorlessa added area/idempotency p1 and removed triage Pending triage from maintainers labels Nov 30, 2021
michaelbrewer added a commit to gyft/aws-lambda-powertools-python that referenced this issue Dec 4, 2021
@michaelbrewer
Copy link
Contributor

@heitorlessa @dispyfree - a basic fix for this in my PR (#869). I am just going to add a good unit test to reproduce the issue as well.

@heitorlessa heitorlessa added the pending-release Fix or implementation already in dev waiting to be released label Dec 7, 2021
@heitorlessa
Copy link
Contributor

merged - tagging for the next release hopefully this Friday

@heitorlessa
Copy link
Contributor

Now released as part of 1.23 version - thank you again @dispyfree for flagging this

@heitorlessa heitorlessa removed the pending-release Fix or implementation already in dev waiting to be released label Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p1
Projects
None yet
Development

No branches or pull requests

3 participants