Skip to content

Commit a7e43ba

Browse files
committed
[FIX] fastapi: Ensure no default eventloop thread is created
Before this change, an eventloop thread was created on each instanciation of the ASGIMiddleware class. We want to avoid this behavior to avoid the creation of zombie threads
1 parent 9cde646 commit a7e43ba

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

fastapi/middleware.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,26 @@
1313

1414
import a2wsgi
1515
from a2wsgi.asgi import ASGIResponder
16+
from a2wsgi.asgi_typing import ASGIApp
1617
from a2wsgi.wsgi_typing import Environ, StartResponse
1718

1819
from .pools import event_loop_pool
1920

2021

2122
class ASGIMiddleware(a2wsgi.ASGIMiddleware):
23+
def __init__(
24+
self,
25+
app: ASGIApp,
26+
wait_time: float | None = None,
27+
) -> None:
28+
# We don't want to use the default event loop policy
29+
# because we want to manage the event loop ourselves
30+
# using the event loop pool.
31+
# Since the the base class check if the given loop is
32+
# None, we can pass False to avoid the initialization
33+
# of the default event loop
34+
super().__init__(app, wait_time, False)
35+
2236
def __call__(
2337
self, environ: Environ, start_response: StartResponse
2438
) -> Iterable[bytes]:

0 commit comments

Comments
 (0)