Skip to content

Fix artisan serve crash: make uvicorn WebSocket backend configurable - #211

Open
tmgbedu wants to merge 1 commit into
mainfrom
task/serve-ws-option-1556
Open

Fix artisan serve crash: make uvicorn WebSocket backend configurable#211
tmgbedu wants to merge 1 commit into
mainfrom
task/serve-ws-option-1556

Conversation

@tmgbedu

@tmgbedu tmgbedu commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Problem

artisan serve crashed with ModuleNotFoundError: No module named 'websockets' on apps that never use WebSockets. The serve command hardcoded ws="websockets-sansio" in the uvicorn.run kwargs, and uvicorn imports that backend at startup — requiring the optional websockets package even when no WebSocket is ever opened.

Fix

  • Replace the hardcoded ws="websockets-sansio" with a proper --ws option accepting the values uvicorn supports: auto, none, websockets, websockets-sansio, wsproto.
  • Resolution order: CLI flag > fastapi.ws config > default.
  • Safe default auto — uvicorn resolves it lazily and never requires the websockets package unless a WebSocket connection is actually opened, so serve always boots cleanly.
  • Invalid --ws values print a clear error listing the allowed backends and exit non-zero, instead of raising a stack trace.
  • Added a ws field to FastAPIConfig (env APP_WS, default auto) so the backend is configurable and documented.
  • host / port / reload behaviour is unchanged.

Verification

  • Reproduced the crash in an isolated env with neither websockets nor wsproto installed: ws="auto" and ws="none" load cleanly (ws_protocol_class=None), while ws="websockets-sansio" (the old hardcoded value) raises ModuleNotFoundError — confirming the root cause and the fix.
  • WebSocket backends still work when the user opts into a backend whose package is installed.

Tests

  • Updated the default-backend assertion to expect auto.
  • Added TestWsOption covering: flag pass-through, websockets-sansio opt-in, none, config-driven selection, CLI-overrides-config, and invalid value (non-zero exit, uvicorn.run not called, error lists allowed values).
  • Full suite: 163 passed (tests/fastapi/ + config tests).

…ault

The serve command hardcoded ws="websockets-sansio" in the uvicorn.run
kwargs. Uvicorn imports that backend at startup, so `artisan serve`
crashed with ModuleNotFoundError: No module named 'websockets' on apps
that never use WebSockets and don't have the optional package installed.

Replace the hardcoded value with a --ws option (auto, none, websockets,
websockets-sansio, wsproto) resolved as CLI flag > fastapi config >
default. The default is 'auto', which uvicorn resolves lazily and never
requires the websockets package unless a WebSocket connection is opened.
Invalid values report a clear error and exit non-zero instead of raising
a stack trace. A matching fastapi.ws config field (env APP_WS) is added.
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant