Skip to content

Bug: HttpResolverLocal with enable_validation hangs under concurrent ASGI requests #8866

Done

Description

@dacevedo12

Expected Behaviour

HttpResolverLocal with enable_validation=True keeps answering requests under uvicorn, whatever the number of concurrent requests.

Current Behaviour

When enough requests arrive at once, the server stops answering for good. It doesn't answer even a single request afterwards, and it has to be killed. With enable_validation=False, the same load is fine.

Each sync middleware runs in asyncio.to_thread (AsyncMiddlewareFrame.__call__ in middlewares/async_utils.py). Its sync_next blocks that thread on run_coroutine_threadsafe(...).result(), and the next frame is the second sync validation middleware, which needs another thread from the same default executor. When the requests in the first validation middleware hold every executor thread, none is left for the second one, so every request waits forever.

The executor holds min(32, os.cpu_count() + 4) threads, which is 16 on a 12-core machine.

Code snippet

import concurrent.futures
import threading
import time
import urllib.request

import uvicorn
from aws_lambda_powertools.event_handler import HttpResolverLocal

app = HttpResolverLocal(enable_validation=True)


@app.get("/ping")
def ping() -> dict[str, str]:
    return {"ok": "yes"}


def call(_: int) -> int:
    with urllib.request.urlopen("http://127.0.0.1:8013/ping", timeout=15) as response:
        return response.status


server = uvicorn.Server(uvicorn.Config(app, port=8013, lifespan="off", log_level="error"))
threading.Thread(target=server.run, daemon=True).start()
while not server.started:
    time.sleep(0.05)

with concurrent.futures.ThreadPoolExecutor(64) as pool:
    try:
        print("burst:", set(pool.map(call, range(64))))
    except TimeoutError as error:
        print("burst:", repr(error))
try:
    print("single request:", call(0))
except TimeoutError as error:
    print("single request:", repr(error))

Possible Solution

Don't hold an executor thread while waiting on the event loop for the next middleware. For example, when the next frame is also sync, call it directly in the same thread. Or run the whole sync middleware chain in one to_thread call.

Steps to Reproduce

  1. pip install aws-lambda-powertools==3.40.0 uvicorn pydantic
  2. Run the snippet above. The burst times out, and so does the single request after it. The process then never exits, because the executor threads stay blocked; stop it with Ctrl-C.
  3. Set enable_validation=False and run it again. It prints burst: {200} and single request: 200, then exits.

With 15 or 16 concurrent requests on a 12-core machine it passes. The threshold depends on how many requests reach the first middleware before any reaches the second.

Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

3.40.0 (the bridge on develop is unchanged)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

No error is logged: requests stop getting answers.

This is a different case from #8187, where a sync middleware raised before calling next.

Activity

  1. sumedh-aerram commented on Nov 18, 2026

    @sumedh-aerram
    Contributor

    I reproduced this on develop (379929b) with the snippet above, on Python 3.12.14, uvicorn 0.54.0, pydantic 2.14.3, on 18 CPUs:

    • enable_validation=False: burst of 64 returns {200: 64}, and the next single request returns 200.
    • enable_validation=True: burst of 64 returns {200: 11, TimeoutError: 53}, and the next single request also times out. The server doesn't recover.

    I'd like to work on this if you confirm it. My plan is to stop holding an executor thread while waiting for the next frame: when consecutive middlewares are sync, run them in the same thread instead of starting a new to_thread hop for each one. I'd add a functional test that fires concurrent requests through HttpResolverLocal with validation on and fails on develop.

  2. leandrodamascena commented on Nov 25, 2026

    @leandrodamascena
    Contributor

    Confirmed, and your diagnosis is exactly right. I reproduced it with your script: on develop the burst and the single request after it both time out, and with enable_validation=False the same load is fine. A tiny executor makes it deterministic, two worker threads and two concurrent requests are enough. It's also wider than validation: two of your own sync middlewares in a row deadlock the same way, with or without validation. Fix is in #8907, consecutive sync middlewares now stay on the thread they're already in and only an async next step goes back to the event loop. Goes out in 3.42.0. Thanks for the clear report, and for the second one on HttpResolverLocal; this one I'm taking myself because it sits in the middle of the async chain.

  3. added this to the 3.42.0 milestone on Nov 25, 2026
  4. powertools-for-aws-oss-automation commented on Nov 25, 2026

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  5. added
    pending-release
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugevent_handlerspending-release

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions