Skip to content
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

Exception in application code does not clear the idempotency key #16

Open
shalabhc opened this issue Jul 11, 2024 · 0 comments
Open

Exception in application code does not clear the idempotency key #16

shalabhc opened this issue Jul 11, 2024 · 0 comments

Comments

@shalabhc
Copy link

The idempotency key is cleared when a response flows through the send_wrapper here:

await self.backend.clear_idempotency_key(idempotency_key)

and here:

await self.backend.clear_idempotency_key(idempotency_key)

However if the underlying application raises an exception, it does not flow through the send_wrapper so the key is not cleared. The self.app() call can be wrapped in a try/finally to handle that case:

await self.app(scope, receive, send_wrapper)

If there is an exception converted to a 500 or another response by an outer middleware, the idempotency key for that request will not be cleared.

Wrapping the app() in a middleware is a common pattern, eg:

https://github.com/steinnes/timing-asgi/blob/2a0dd6535b740481f4354831a37b92b47a70d4c2/timing_asgi/middleware.py#L70
https://github.com/DataDog/dd-trace-py/blob/6d98a05ff80b572b265fac3562e207f17c2d9adb/ddtrace/contrib/asgi/middleware.py#L291

The change is simple but I am unable to set up a working test environment so I wasn't able to write a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant