Skip to content

Commit 5d6c449

Browse files
committed
Tweak autoreload logic to be compatible with websockets>=13.0
1 parent ad9995d commit 5d6c449

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies = [
3131
"typing-extensions>=4.10.0",
3232
"uvicorn>=0.16.0;platform_system!='Emscripten'",
3333
"starlette",
34-
"websockets>=10.0",
34+
"websockets>=13.0",
3535
"python-multipart",
3636
"htmltools>=0.6.0",
3737
"click>=8.1.4;platform_system!='Emscripten'",

shiny/_autoreload.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def reload_begin():
5959
# Called from child process when new application instance starts up
6060
def reload_end():
6161
import websockets
62+
import websockets.asyncio.client
6263

6364
# os.kill(os.getppid(), signal.SIGUSR1)
6465

@@ -75,7 +76,7 @@ async def _() -> None:
7576
}
7677
}
7778
try:
78-
async with websockets.connect(
79+
async with websockets.asyncio.client.connect(
7980
url, **options # pyright: ignore[reportArgumentType]
8081
) as websocket:
8182
await websocket.send("reload_end")
@@ -202,7 +203,7 @@ async def _coro_main(
202203
) -> None:
203204
import websockets
204205
import websockets.asyncio.server
205-
import websockets.http11
206+
from websockets.http11 import Request, Response
206207

207208
reload_now: asyncio.Event = asyncio.Event()
208209

@@ -245,24 +246,12 @@ async def reload_server(conn: websockets.asyncio.server.ServerConnection):
245246
# about only WebSockets being supported. This is not an academic problem as the
246247
# VSCode extension used in RSW sniffs out ports that are being listened on, which
247248
# leads to confusion if all you get is an error.
248-
async def process_request_legacy(
249-
path: str, request_headers: websockets.datastructures.Headers
250-
) -> Optional[tuple[http.HTTPStatus, websockets.datastructures.HeadersLike, bytes]]:
251-
# If there's no Upgrade header, it's not a WebSocket request.
252-
if request_headers.get("Upgrade") is None:
253-
# For some unknown reason, this fixes a tendency on GitHub Codespaces to
254-
# correctly proxy through this request, but give a 404 when the redirect is
255-
# followed and app_url is requested. With the sleep, both requests tend to
256-
# succeed reliably.
257-
await asyncio.sleep(1)
258-
return (http.HTTPStatus.MOVED_PERMANENTLY, [("Location", app_url)], b"")
259-
260-
async def process_request_new(
249+
async def process_request(
261250
connection: websockets.asyncio.server.ServerConnection,
262-
request: websockets.http11.Request,
263-
) -> websockets.http11.Response | None:
251+
request: Request,
252+
) -> Response | None:
264253
if request.headers.get("Upgrade") is None:
265-
return websockets.http11.Response(
254+
return Response(
266255
status_code=http.HTTPStatus.MOVED_PERMANENTLY,
267256
reason_phrase="Moved Permanently",
268257
headers=websockets.Headers(Location=app_url),
@@ -274,11 +263,8 @@ async def process_request_new(
274263
# logging.getLogger("websockets").addHandler(logging.NullHandler())
275264
logging.getLogger("websockets").setLevel(loglevel)
276265

277-
async with websockets.serve(
278-
reload_server,
279-
"127.0.0.1",
280-
port,
281-
process_request=process_request_new,
266+
async with websockets.asyncio.server.serve(
267+
reload_server, "127.0.0.1", port, process_request=process_request
282268
):
283269
await asyncio.Future() # wait forever
284270

0 commit comments

Comments
 (0)