Repository navigation
fix: notify ASGI app of startup only once under concurrent first requests - #382
Open
Aditya Nikam (adityaanikam) wants to merge 1 commit into
Open
Aditya Nikam (adityaanikam) wants to merge 1 commit into
Aditya Nikam (adityaanikam) wants to merge 1 commit into
Conversation
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.
Aditya Nikam (adityaanikam)
requested review from
Gavin Aguiar (gavin-aguiar),
hallvictoria and
Varad Meru (vrdmr)
as code owners
November 20, 2026 10:08
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes Azure/azure-functions-python-worker#1954
AsgiFunctionAppnotified the ASGI app of startup from inside the request handler, guarded only by thestartup_task_doneflag:notify_startup()awaits the startup event, so requests arriving together on a cold worker all observed the flag asFalseand each notified startup. The extralifespan.startupmessages went onto the same lifespan queue, so the app received one of them where the ASGI protocol sayslifespan.shutdowncomes next, and ran the shutdown half of its lifespan while the worker kept serving traffic.startup_task_donewas leftTrue, 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
AsgiFunctionAppdrives the ASGI lifespan protocol within a worker process. No gRPC message or shared interface is touched.Worker Execution Logic
Python Version Coverage
azure.functions.decorators.function_app, which this library ships to every supported Python version, so it applies whereverAsgiFunctionAppis used.AsgiFunctionAppandAsgiMiddlewareare defined only here, not in the worker repository.Programming Model Compatibility
AsgiFunctionAppis part of the V2 decorator API, so the fix applies there.AsgiFunctionApp.Testing
test_concurrent_first_requests_notify_startup_oncedrives 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.pyandtests/test_http_asgi.pypass together (83 tests), andflake8is clean.The issue was reported against the worker repository, but
AsgiFunctionApplives here, so the fix belongs in this repository.