Skip to content

Commit 8e2343c

Browse files
committed
test complete
1 parent d9ea89f commit 8e2343c

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

packages/service-library/src/servicelib/fastapi/cancellation_middleware.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,22 @@ async def _handler(app: ASGIApp, scope: Scope, queue: asyncio.Queue, send: Send)
2727
return await app(scope, queue.get, send)
2828

2929

30-
class CancellationMiddleware:
30+
class RequestCancellationMiddleware:
31+
"""ASGI Middleware to cancel server requests in case of client disconnection.
32+
Reason: FastAPI-based (e.g. starlette) servers do not automatically cancel
33+
server requests in case of client disconnection. This middleware will cancel
34+
the server request in case of client disconnection via asyncio.CancelledError.
35+
36+
WARNING: FastAPI BackgroundTasks will also get cancelled. Use with care.
37+
TIP: use asyncio.Task in that case
38+
"""
39+
3140
def __init__(self, app: ASGIApp) -> None:
3241
self.app = app
42+
_logger.warning(
43+
"CancellationMiddleware is in use, in case of client disconection, "
44+
"FastAPI BackgroundTasks will be cancelled too!",
45+
)
3346

3447
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
3548
if scope["type"] != "http":

packages/service-library/tests/fastapi/test_cancellation_middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import uvicorn
1010
from fastapi import APIRouter, BackgroundTasks, FastAPI
1111
from pytest_simcore.helpers.logging_tools import log_context
12-
from servicelib.fastapi.cancellation_middleware import CancellationMiddleware
12+
from servicelib.fastapi.cancellation_middleware import RequestCancellationMiddleware
1313
from servicelib.utils import unused_port
1414
from yarl import URL
1515

@@ -68,7 +68,7 @@ async def sleep_with_background_task(
6868
def fastapi_app(fastapi_router: APIRouter) -> FastAPI:
6969
app = FastAPI()
7070
app.include_router(fastapi_router)
71-
app.add_middleware(CancellationMiddleware)
71+
app.add_middleware(RequestCancellationMiddleware)
7272
return app
7373

7474

services/storage/src/simcore_service_storage/core/application.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from fastapi.middleware.gzip import GZipMiddleware
1111
from fastapi_pagination import add_pagination
1212
from servicelib.fastapi import timing_middleware
13+
from servicelib.fastapi.cancellation_middleware import RequestCancellationMiddleware
1314
from servicelib.fastapi.client_session import setup_client_session
1415
from servicelib.fastapi.openapi import override_fastapi_openapi_method
1516
from servicelib.fastapi.profiler import ProfilerMiddleware
@@ -103,6 +104,8 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
103104

104105
app.add_middleware(GZipMiddleware)
105106

107+
app.add_middleware(RequestCancellationMiddleware)
108+
106109
if settings.STORAGE_TRACING:
107110
initialize_tracing(app, settings.STORAGE_TRACING, APP_NAME)
108111
if settings.STORAGE_MONITORING_ENABLED:

0 commit comments

Comments
 (0)