Review generated ASGI middleware

from WSGI and ASGI
Python 3.14 advanced 6 min 5 issues to find

Review this generated request-ID middleware against the task and identify five distinct risks.

Add one server-generated x-request-id header to every HTTP response without consuming the request, blocking the loop, losing repeated headers, or changing non-HTTP scopes.

Python
import time

class RequestIdMiddleware:
    def __init__(self, app):
        self.app = app

    async def __call__(self, scope, receive, send):
        if scope["type"] != "http":
            return

        first = await receive()
        request_body = first.get("body", b"")
        time.sleep(0.05)
        request_id = scope["headers"][0][1].decode()

        async def send_wrapper(message):
            if message["type"] == "http.response.start":
                message["headers"] = dict(message.get("headers", []))
                message["headers"][b"x-request-id"] = request_id.encode()
            await send(message)

        await self.app(scope, receive, send_wrapper)

generated code is illustrative, not from any one model

Open in playground
Report an error