From a1db57eee29d134a394c6399715159b72c27fd9b Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Thu, 5 Mar 2026 23:43:48 +0000 Subject: [PATCH] Switch to `sansio` websockets back-end Uvicorn uses the now deprecated `websockets.legacy` back end by default. It also supports the newer `sansio` interface. As far as I can tell, either works, so I'm not sure why legacy is the default: backwards compatibility I guess. I'd like to switch to the newer implementation now, before we are using websockets in anger, as it feels wrong to start out with deprecation warnings in the test suite! --- src/labthings_fastapi/server/cli.py | 4 ++-- tests/test_fallback.py | 2 +- tests/test_mjpeg_stream.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/labthings_fastapi/server/cli.py b/src/labthings_fastapi/server/cli.py index 66f854cd..a97c7701 100644 --- a/src/labthings_fastapi/server/cli.py +++ b/src/labthings_fastapi/server/cli.py @@ -161,7 +161,7 @@ def serve_from_cli( server = ThingServer.from_config(config, True if args.debug else False) if dry_run: return server - uvicorn.run(server.app, host=args.host, port=args.port) + uvicorn.run(server.app, host=args.host, port=args.port, ws="websockets-sansio") except BaseException as e: if args.fallback: print(f"Error: {e}") @@ -174,7 +174,7 @@ def serve_from_cli( ) ) - uvicorn.run(app, host=args.host, port=args.port) + uvicorn.run(app, host=args.host, port=args.port, ws="websockets-sansio") else: if isinstance(e, (ValidationError, ThingImportFailure)): print(f"Error reading LabThings configuration:\n{e}") diff --git a/tests/test_fallback.py b/tests/test_fallback.py index 56a367f1..e3a1136f 100644 --- a/tests/test_fallback.py +++ b/tests/test_fallback.py @@ -170,7 +170,7 @@ def test_actual_server_fallback(): # Starting the server is a SystemExit with pytest.raises(SystemExit, match="3") as excinfo: - uvicorn.run(server.app, port=5000) + uvicorn.run(server.app, port=5000, ws="websockets-sansio") server_error = excinfo.value assert server.startup_failure is not None assert server.startup_failure["thing"] == "bad_thing" diff --git a/tests/test_mjpeg_stream.py b/tests/test_mjpeg_stream.py index 0effde36..f00b43b1 100644 --- a/tests/test_mjpeg_stream.py +++ b/tests/test_mjpeg_stream.py @@ -78,4 +78,4 @@ def test_mjpeg_stream(client): assert isinstance(telly, Telly) telly.framerate = 6 telly.frame_limit = -1 - uvicorn.run(server.app, port=5000) + uvicorn.run(server.app, port=5000, ws="websockets-sansio")