Skip to main content

AsyncExitStackMiddleware

This class provides an ASGI middleware that adds an AsyncExitStack to the request scope, enabling downstream components to manage the lifecycle of asynchronous resources. It ensures that all registered cleanups and context managers are properly closed after the request is processed by wrapping the application call in an asynchronous context manager.

Attributes

AttributeTypeDescription
appASGIAppThe ASGI application instance that this middleware wraps and delegates calls to.
context_namestr = fastapi_middleware_astackThe key name used to store the AsyncExitStack instance within the ASGI scope dictionary.

Constructor

Signature

def AsyncExitStackMiddleware(
app: ASGIApp,
context_name: str = fastapi_middleware_astack
) - > None

Parameters

NameTypeDescription
appASGIAppThe ASGI application instance.
context_namestr = fastapi_middleware_astackThe key name used to store the AsyncExitStack in the ASGI scope.

Signature

def AsyncExitStackMiddleware(
app: ASGIApp,
context_name: string = fastapi_middleware_astack
) - > null

Parameters

NameTypeDescription
appASGIAppThe next ASGI application or middleware in the request pipeline to be wrapped.
context_namestring = fastapi_middleware_astackThe key name used to store the AsyncExitStack instance within the ASGI scope dictionary.