azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

[BUG] Starlette ASGI app hangs when using middlewares derived from BaseHTTPMiddleware

Open robintw opened this issue 4 years ago • 4 comments

Investigative information

Please provide the following:
  • Timestamp: 2021-10-22 10:13:00 UTC
  • Function App name: http://robin-starlette-test.azurewebsites.net/
  • Function name(s) (as appropriate): HttpTrigger1
  • Core Tools version: 3.0.3785

Repro steps

Provide the steps required to reproduce the problem:

I have deployed a function to Azure Functions that runs a simple Starlette ASGI app, using the AsgiMiddleware provided by the Azure Functions Python library. The code for this function, and the simple app, is available here.

The Azure Functions part of it is very simple and just uses the AsgiMiddleware with the app:

import azure.functions as func
from azure.functions import AsgiMiddleware

from .simple_app import app

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return AsgiMiddleware(app).handle(req, context) 

The Starlette ASGI app is also very simple, and just defines a simple route. As you can see from the comments here, running this without middleware works, and running with the CORSMiddleware (which is not derived from Starlette's BaseHTTPMiddleware) works fine, but running with a middleware that is derived from BaseHTTPMiddleware fails.

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.cors import CORSMiddleware

async def homepage(request):
    return JSONResponse({"hello": "world"})


class CustomHeaderMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        response = await call_next(request)
        response.headers["Custom"] = "Example"
        return response


app = Starlette(
    debug=True,
    routes=[
        Route("/", homepage),
    ],
    # Works with no middlewares defined
    #
    # Hangs with this line:
    # middleware=[Middleware(CustomHeaderMiddleware)]
    #
    # Works with this line:
    middleware = [Middleware(CORSMiddleware)]
)

Expected behavior

Provide a description of the expected behavior.

The app should run without hanging, regardless of which middleware is defined.

Actual behavior

Provide a description of the actual behavior observed.

When a middleware derived from BaseHTTPMiddleware is used, the app hangs on every request.

I don't know how to go about debugging a request just hanging (and eventually timing out) on Azure Functions. It occurs both when testing locally and when deploying to a live function. I have deployed the version which hangs to http://robin-starlette-test.azurewebsites.net/ if that helps you with debugging.

Known workarounds

Provide a description of any known workarounds.

None

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions
starlette

Related information

Provide any related information

robintw avatar Oct 22 '21 09:10 robintw

This bug has been investigated by one of the developers of Starlette, and they have come to the conclusion that it is a bug in the Azure Functions ASGI interface. See https://github.com/encode/starlette/issues/1320#issuecomment-980633054 for details - and a suggested fix.

robintw avatar Nov 27 '21 18:11 robintw

Proposed fix in the library API https://github.com/Azure/azure-functions-python-library/pull/98

tonybaloney avatar Jan 11 '22 06:01 tonybaloney

PR merged.

YunchuWang avatar Mar 14 '22 22:03 YunchuWang

I'm running into a very similar issue. I see the PR was merged, but has the fix actually been implemented yet in Azure Functions @YunchuWang? Just want to make sure I'm chasing the correct problem, thanks!

DCMattyG avatar Jul 10 '22 22:07 DCMattyG

image @DCMattyG Hello, its merged in 1.10.1 worker release.

YunchuWang avatar Dec 07 '22 23:12 YunchuWang