ecerami / fastapi_azure

MIT License
17 stars 15 forks source link

Error running external on Azure #1

Open boring-joey opened 2 years ago

boring-joey commented 2 years ago

Found your Medium post some months ago. Got the API working on Azure and running for a couple of months now, thanks for that.

But.. I tried to update the API today and pushed the new code to Azure and ended up with http 500 error codes. Seems to be an error in the http_asgi.py if i am not mistaken. This is the error i got from the Application Insights.

Do you know what the problem might be?

Exception while executing function: Functions.my-appname <--- Result: Failure Exception: TypeError: '<' not supported between instances of 'NoneType' and 'int' Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 402, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 611, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/my-appname/__init__.py", line 230, in main return AsgiMiddleware(app).handle(req, context) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure/functions/_http_asgi.py", line 146, in handle return self._handle(req, context) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure/functions/_http_asgi.py", line 152, in _handle asgi_response = self._loop.run_until_complete( File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete return future.result() File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure/functions/_http_asgi.py", line 65, in from_app await app(scope, res._receive, res._send) File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/applications.py", line 212, in __call__ await super().__call__(scope, receive, send) File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/applications.py", line 119, in __call__ await self.middleware_stack(scope, receive, send) File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/middleware/errors.py", line 181, in __call__ raise exc File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/middleware/errors.py", line 159, in __call__ await self.app(scope, receive, _send) File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/exceptions.py", line 87, in __call__ raise exc File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/exceptions.py", line 76, in __call__ await self.app(scope, receive, sender) File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/routing.py", line 659, in __call__ await route.handle(scope, receive, send) File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/routing.py", line 259, in handle await self.app(scope, receive, send) File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/routing.py", line 61, in app response = await func(request) File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/routing.py", line 216, in app solved_result = await solve_dependencies( File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/dependencies/utils.py", line 465, in solve_dependencies response = response or Response( File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/responses.py", line 50, in __init__ self.init_headers(headers) File "/home/site/wwwroot/.python_packages/lib/site-packages/starlette/responses.py", line 77, in init_headers and not (self.status_code < 200 or self.status_code in (204, 304))

jacobboon commented 2 years ago

We were getting this error recently as well. It appeared to be an issue between Starlette and FastAPI. This was not in an Azure function, but just running FastAPI locally using uvicorn.

The (magical) fix for us was to reinstall all the dependencies in the virtual environment, but I'm not sure how that works within an Azure Function app. You could try to version pin your dependencies in requirements.txt, potentially even including Starlette at it's most recent stable version, even though it's a dependency of FastAPI.

sdementen commented 2 years ago

I have the issue with starlette==0.18.0 but not with starlette==0.17.1 Maybe due to this change https://github.com/encode/starlette/pull/1397 to fix https://github.com/encode/starlette/issues/1178