Azure / azure-functions-python-library

Azure Functions Python SDK
MIT License
151 stars 63 forks source link

Ensure ASGI State is added to all subsequent ASGI requests #240

Open ashleysommer opened 3 weeks ago

ashleysommer commented 3 weeks ago

The ASGIMiddleware class has a state (Dict) property. State can gen written to if the ASGI Lifespan startup handler writes to it, but it is never used for anything after that.

The ASGI Spec states that contents of the lifespan startup's state dict is stored by the ASGI server, and the state is added to any subsequent ASGI Requests. https://asgi.readthedocs.io/en/latest/specs/lifespan.html

Applications often want to persist data from the lifespan cycle to request/response handling. ... The server will ensure that a shallow copy of the namespace is passed into each subsequent request/response call into the application.

This PR implements that final missing link, adds a shallow copy of the state dict to every subsequent ASGI Request, as specified in the ASGI spec.

Fixes https://github.com/Azure/azure-functions-python-worker/issues/1566