Skip to content

[16.0][FIX] fastapi: Ensure no default eventloop thread is created #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions fastapi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@

import a2wsgi
from a2wsgi.asgi import ASGIResponder
from a2wsgi.asgi_typing import ASGIApp
from a2wsgi.wsgi_typing import Environ, StartResponse

from .pools import event_loop_pool


class ASGIMiddleware(a2wsgi.ASGIMiddleware):
def __init__(
self,
app: ASGIApp,
wait_time: float | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we need to keep the parent signature?

loop: Optional[asyncio.AbstractEventLoop] = None,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO no. This class is designed to be used in the specific context of the fastapi addon where we enforce the use of the pool of eventloop threads.

) -> None:
# We don't want to use the default event loop policy
# because we want to manage the event loop ourselves
# using the event loop pool.
# Since the the base class check if the given loop is
# None, we can pass False to avoid the initialization
# of the default event loop
super().__init__(app, wait_time, False)
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit hacky but I don't see any other option.


def __call__(
self, environ: Environ, start_response: StartResponse
) -> Iterable[bytes]:
Expand Down