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.
The idempotency key is cleared when a response flows through the
send_wrapperhere:asgi-idempotency-header/idempotency_header_middleware/middleware.py
Line 75 in 8f89a5c
and here:
asgi-idempotency-header/idempotency_header_middleware/middleware.py
Line 83 in 8f89a5c
However if the underlying application raises an exception, it does not flow through the
send_wrapperso the key is not cleared. Theself.app()call can be wrapped in atry/finallyto handle that case:asgi-idempotency-header/idempotency_header_middleware/middleware.py
Line 95 in 8f89a5c
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.