Skip to content
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

Ignore BrokenResourceError in BaseHTTPMiddleware #1318

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions starlette/middleware/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import typing
from contextlib import suppress

import anyio

from starlette.requests import Request
from starlette.responses import Response, StreamingResponse
from starlette.types import ASGIApp, Receive, Scope, Send
from starlette.types import ASGIApp, Message, Receive, Scope, Send

RequestResponseEndpoint = typing.Callable[[Request], typing.Awaitable[Response]]
DispatchFunction = typing.Callable[
Expand All @@ -25,9 +26,13 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
async def call_next(request: Request) -> Response:
send_stream, recv_stream = anyio.create_memory_object_stream()

async def docile_stream_send(msg: Message) -> None:
with suppress(anyio.BrokenResourceError):
await send_stream.send(msg)

async def coro() -> None:
async with send_stream:
await self.app(scope, request.receive, send_stream.send)
await self.app(scope, request.receive, docile_stream_send)

task_group.start_soon(coro)

Expand Down