Fix artisan serve crash: make uvicorn WebSocket backend configurable - #211
Open
tmgbedu wants to merge 1 commit into
Open
Fix artisan serve crash: make uvicorn WebSocket backend configurable#211tmgbedu wants to merge 1 commit into
tmgbedu wants to merge 1 commit into
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
artisan servecrashed withModuleNotFoundError: No module named 'websockets'on apps that never use WebSockets. The serve command hardcodedws="websockets-sansio"in theuvicorn.runkwargs, and uvicorn imports that backend at startup — requiring the optionalwebsocketspackage even when no WebSocket is ever opened.Fix
ws="websockets-sansio"with a proper--wsoption accepting the values uvicorn supports:auto,none,websockets,websockets-sansio,wsproto.fastapi.wsconfig > default.auto— uvicorn resolves it lazily and never requires thewebsocketspackage unless a WebSocket connection is actually opened, so serve always boots cleanly.--wsvalues print a clear error listing the allowed backends and exit non-zero, instead of raising a stack trace.wsfield toFastAPIConfig(envAPP_WS, defaultauto) so the backend is configurable and documented.host/port/reloadbehaviour is unchanged.Verification
websocketsnorwsprotoinstalled:ws="auto"andws="none"load cleanly (ws_protocol_class=None), whilews="websockets-sansio"(the old hardcoded value) raisesModuleNotFoundError— confirming the root cause and the fix.Tests
auto.TestWsOptioncovering: flag pass-through,websockets-sansioopt-in,none, config-driven selection, CLI-overrides-config, and invalid value (non-zero exit,uvicorn.runnot called, error lists allowed values).163 passed(tests/fastapi/+ config tests).