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] Azure Function AsgiMiddleware Not Responding

See original GitHub issue

Investigative information

Please provide the following:
  • Timestamp: Tue 20 Sep 2022 16:43:04 IST
  • Function App name: locally testing in VSCode
  • Core Tools version: 4.0.4736

Repro steps

Provide the steps required to reproduce the problem:
  1. Create a new Function App of type HTTP trigger and the following code in __init__.py:
import azure.functions as func
import time
import nest_asyncio
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware

nest_asyncio.apply()

app = FastAPI(title="FastAPI on Azure")
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response



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


@app.get("/")
async def root():
    return {"message": "Hello from Azure Function"}

Complete code including config is available in GitHub repo: https://github.com/yks0000/az-function-fastapi/tree/azure-worker-issue To start function app: func host start --verbose

Expected behavior

Provide a description of the expected behavior.

It should return a response when we access http://0.0.0.0:7071/

Actual behavior

Provide a description of the actual behavior observed.

No response and running forever.

Known workarounds

Provide a description of any known workarounds.

No workaround but If we stop using middleware (as below), it works, but the requirement is to use middleware.

Middleware Doc: https://fastapi.tiangolo.com/tutorial/middleware/

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-common
azure-core
azure-functions
fastapi
nest-asyncio

Related information

Provide any related information

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ramya894commented, Sep 22, 2022

@yks0000 We will check this issue with our next level team and update you.

0reactions
tonybaloneycommented, Dec 2, 2022

I’ve submitted a fix to the related package, it might take a while for this to roll through. https://github.com/Azure/azure-functions-python-library/pull/153

In the meantime only 1 piece of middleware is supported.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot error: Azure Functions Runtime is unreachable
Your function app might be in an unresponsive state due to conflicting port assignment upon startup. This can happen in the following cases:....
Read more >
"detail": "Not Found" using Fastapi with Azure function app
Hi My fastapi code works well in VS code. I am planning to use my code using Azure Function app.
Read more >
Azure - TiTiler - Development Seed
Note: there is a bug in azure.functions.AsgiMiddleware which prevent using starlette.BaseHTTPMiddleware middlewares (see: Azure/azure-functions-python-worker# ...
Read more >
Azure Functions and FastAPI - DEV Community ‍ ‍
If a HTTP method is used that was not specified, the Azure function throws a "method not allowed" exception. Furthermore, the host.json file ......
Read more >
wSGI and aSGI Python Applications on Azure Functions -
Azure Functions can support wSGI and aSGI frameworks with HTTP triggered Python Functions. This functionality is now provided by middleware ...
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