Skip to content

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

@shalabhc

Description

@shalabhc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions