Skip to content

fix: notify ASGI app of startup only once under concurrent first requests - #382

Open
Aditya Nikam (adityaanikam) wants to merge 1 commit into
Azure:devfrom
adityaanikam:fix-asgi-concurrent-lifespan-startup
Open

Aditya Nikam (adityaanikam) wants to merge 1 commit into
Azure:devfrom
adityaanikam:fix-asgi-concurrent-lifespan-startup

Conversation

@adityaanikam

Copy link
Copy Markdown

Description

Fixes Azure/azure-functions-python-worker#1954

AsgiFunctionApp notified the ASGI app of startup from inside the request handler, guarded only by the startup_task_done flag:

if not self.startup_task_done:
    success = await asgi_middleware.notify_startup()
    ...
    self.startup_task_done = True

notify_startup() awaits the startup event, so requests arriving together on a cold worker all observed the flag as False and each notified startup. The extra lifespan.startup messages went onto the same lifespan queue, so the app received one of them where the ASGI protocol says lifespan.shutdown comes next, and ran the shutdown half of its lifespan while the worker kept serving traffic. startup_task_done was left True, so startup never ran again and every later request on that worker saw resources that had already been released, until the worker was recycled.

The startup is now guarded by an asyncio.Lock, with the flag re-checked inside it, so the first request performs startup and the others wait for it. The flag is still checked before taking the lock, so requests after startup are unaffected.

Pull Request Checklist

Host-Worker Contract

  • Does this PR impact the host-worker contract (e.g., gRPC messages, shared interfaces)?
    • If no, please explain why: the change is confined to how AsgiFunctionApp drives the ASGI lifespan protocol within a worker process. No gRPC message or shared interface is touched.

Worker Execution Logic

  • Does this PR affect worker execution logic (e.g., function invocation, bindings, lifecycle)?

Python Version Coverage

  • Does this change apply to all supported worker models?
  • The fix lives in azure.functions.decorators.function_app, which this library ships to every supported Python version, so it applies wherever AsgiFunctionApp is used. AsgiFunctionApp and AsgiMiddleware are defined only here, not in the worker repository.

Programming Model Compatibility

  • V2 programming model: AsgiFunctionApp is part of the V2 decorator API, so the fix applies there.
  • V1 programming model: not applicable, V1 does not expose AsgiFunctionApp.

Testing

test_concurrent_first_requests_notify_startup_once drives two concurrent first requests through the registered function with a lifespan-tracking ASGI app. Before the change the app's lifespan is entered twice and the second startup message arrives where the shutdown is expected; after it, the lifespan is entered once and shutdown is not called. tests/decorators/test_function_app.py and tests/test_http_asgi.py pass together (83 tests), and flake8 is clean.

The issue was reported against the worker repository, but AsgiFunctionApp lives here, so the fix belongs in this repository.

Requests arriving together on a cold worker all saw startup_task_done as
False and each called notify_startup(). The extra startup messages landed
in the same lifespan queue, so the app read one of them where it expected
lifespan.shutdown and tore itself down while the worker kept serving
traffic. startup_task_done was left True, so startup never ran again and
every later request on that worker saw resources that had been released.

Guard the startup with an asyncio.Lock and re-check the flag inside it, so
the first request performs startup and the rest wait for it.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 10:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] AsgiFunctionApp: concurrent first requests run FastAPI lifespan shutdown right after startup

2 participants