Skip to content

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

@robintw

Description

@robintw

Investigative information

Please provide the following:

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

Metadata

Metadata

Assignees

Labels

pending-releaseFixed but pending docker image, runtime or core tools release.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions