Skip to content

Commit bd7a242

Browse files
committed
Fix live reload infinite loop on uvicorn >= 0.39
Keep WebSocket handler alive by waiting on receive() instead of returning immediately after accept(). Fixes #817.
1 parent 05feba0 commit bd7a242

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

fasthtml/live_reload.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from starlette.routing import WebSocketRoute
2+
from starlette.websockets import WebSocketDisconnect
23
from fasthtml.basics import FastHTML, Script
34

45
__all__ = ["FastHTMLWithLiveReload"]
@@ -23,7 +24,11 @@ def LiveReloadJs(reload_attempts:int=20, reload_interval:int=1000, **kwargs):
2324
"""
2425
return Script(src % (reload_attempts, reload_interval))
2526

26-
async def live_reload_ws(websocket): await websocket.accept()
27+
async def live_reload_ws(websocket):
28+
await websocket.accept()
29+
try:
30+
while True: await websocket.receive()
31+
except WebSocketDisconnect: pass
2732

2833
class FastHTMLWithLiveReload(FastHTML):
2934
"""

0 commit comments

Comments
 (0)