Skip to content

Commit

Permalink
handle egroup in starlette
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Dec 28, 2023
1 parent 38d04a3 commit d7f4726
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/py/reactpy/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"exceptiongroup >=1.0",
"typing-extensions >=3.10",
"mypy-extensions >=0.4.3",
"anyio >=3",
Expand Down
15 changes: 11 additions & 4 deletions src/py/reactpy/reactpy/backend/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dataclasses import dataclass
from typing import Any, Callable

from exceptiongroup import BaseExceptionGroup
from starlette.applications import Starlette
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
Expand Down Expand Up @@ -137,8 +138,6 @@ async def serve_index(request: Request) -> HTMLResponse:
def _setup_single_view_dispatcher_route(
options: Options, app: Starlette, component: RootComponentConstructor
) -> None:
@app.websocket_route(str(STREAM_PATH))
@app.websocket_route(f"{STREAM_PATH}/{{path:path}}")
async def model_stream(socket: WebSocket) -> None:
await socket.accept()
send, recv = _make_send_recv_callbacks(socket)
Expand All @@ -162,8 +161,16 @@ async def model_stream(socket: WebSocket) -> None:
send,
recv,
)
except WebSocketDisconnect as error:
logger.info(f"WebSocket disconnect: {error.code}")
except BaseExceptionGroup as egroup:
for e in egroup.exceptions:
if isinstance(e, WebSocketDisconnect):
logger.info(f"WebSocket disconnect: {e.code}")
break
else:
raise

app.add_websocket_route(str(STREAM_PATH), model_stream)
app.add_websocket_route(f"{STREAM_PATH}/{{path:path}}", model_stream)


def _make_send_recv_callbacks(
Expand Down

0 comments on commit d7f4726

Please sign in to comment.