question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
YunchuWangcommented, Dec 7, 2022
image @DCMattyG Hello, its merged in 1.10.1 worker release.
2reactions
DCMattyGcommented, Jul 10, 2022

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Middleware
An abstract class that allows you to write ASGI middleware against a request/response interface. Usage. To implement a middleware class using BaseHTTPMiddleware ......
Read more >
Problem with ASGI lifespan when i use custom middleware
When using it, you should only implement the dispatch method, as mentioned in the documentation: https://www.starlette.io/middleware/# ...
Read more >
Release 0.3.5 Tomasz Wojcik - starlette-context
The middleware effectively creates the context for the request, so you must configure your app to use it. More usage detail along with...
Read more >
Leveraging Starlette in Django Applications.
I came to know about it from following the development of Uvicorn, a lightning-fast ASGI server. After going through the documentation, ...
Read more >
encode/community
hello, in uvicorn why I cannot use SCRIPT_NAME environment variable to set ... Set the ASGI 'root_path' for applications submounted below a given...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found