From 87b5228d07735e7dd9f0d02621a1075a0f0b94df Mon Sep 17 00:00:00 2001 From: Harold Sun Date: Fri, 23 Oct 2026 21:43:23 +0000 Subject: [PATCH] fix(examples): only signal DONE for Lambda invokes in fastapi-background-tasks The middleware queued a DONE after every HTTP request, including LWA's HTTP readiness check during init. The first invoke then consumed that stale DONE and ended before its tasks ran, and every later invoke ran the previous invoke's tasks. Only call done() when the request carries x-amzn-lambda-context, which LWA adds to invoke requests. Fixes #590 --- examples/fastapi-background-tasks/app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/fastapi-background-tasks/app/main.py b/examples/fastapi-background-tasks/app/main.py index 7c2f03ab..86fd8fb7 100644 --- a/examples/fastapi-background-tasks/app/main.py +++ b/examples/fastapi-background-tasks/app/main.py @@ -12,7 +12,9 @@ @app.middleware("http") async def send_done_message(request, call_next): response = await call_next(request) - background_task.done() + # only Lambda invokes carry this header; skip readiness checks + if "x-amzn-lambda-context" in request.headers: + background_task.done() return response def mock_task(seconds):