Skip to content

Commit 4b61071

Browse files
authored
Ensure ASGI State is added to all subsequent ASGI requests
Fixes Azure/azure-functions-python-worker#1566
1 parent 3873bab commit 4b61071

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

azure/functions/_http_asgi.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from typing import Dict, List, Tuple, Optional, Any, Union
55
import logging
6+
from copy import copy
67
import asyncio
78
from asyncio import Event, Queue
89
from urllib.parse import ParseResult, urlparse
@@ -36,7 +37,7 @@ def _get_server_address(self):
3637
return (self.server_name, int(self.server_port))
3738
return None
3839

39-
def to_asgi_http_scope(self):
40+
def to_asgi_http_scope(self, state: Optional[Dict] = None):
4041
if self.path_info is not None:
4142
_raw_path = self.path_info.encode("utf-8")
4243
else:
@@ -60,6 +61,7 @@ def to_asgi_http_scope(self):
6061
"headers": self._get_encoded_http_headers(),
6162
"server": self._get_server_address(),
6263
"client": None,
64+
"state": state,
6365
"azure_functions.function_directory": self.af_function_directory,
6466
"azure_functions.function_name": self.af_function_name,
6567
"azure_functions.invocation_id": self.af_invocation_id,
@@ -210,7 +212,8 @@ async def main(req, context):
210212

211213
async def _handle_async(self, req, context):
212214
asgi_request = AsgiRequest(req, context)
213-
scope = asgi_request.to_asgi_http_scope()
215+
# Shallow copy the state as-per the ASGI spec
216+
scope = asgi_request.to_asgi_http_scope(state=copy(self.state))
214217
asgi_response = await AsgiResponse.from_app(self._app,
215218
scope,
216219
req.get_body())

0 commit comments

Comments
 (0)