Open FastAPI/Quart websocket only when a websocket callback needs it - #3941
Open
T4rk1n wants to merge 2 commits into
Open
Open FastAPI/Quart websocket only when a websocket callback needs it#3941T4rk1n wants to merge 2 commits into
T4rk1n wants to merge 2 commits into
Conversation
The FastAPI and Quart backends always advertise websocket infrastructure (url/worker_url) in the page config, and the renderer keyed the connect decision on the mere presence of that infra, so every app on these backends opened a socket on page load even with no websocket callbacks. Register the websocket message handlers whenever the infra is present, but only open the socket eagerly when websocket_callbacks=True (config.enabled). Per-callback websocket=True opens it lazily on first dispatch via the existing ensureConnected path; an app with no websocket callbacks never connects. Also guard the visibility reconnect so it never opens a first connection. Add regression tests using a websocket_connect hook as a server-side probe: never-connect (HTTP only), lazy-connect (per-callback), eager-connect (global).
|
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.



Closes #3939
Problem
The FastAPI and Quart backends always advertise WebSocket infrastructure (
url/worker_url) in the page config, because they havewebsocket_capability = Trueregardless of whether any WebSocket callback exists. The renderer keyed its "open the socket" decision on the mere presence of that infra, so every app on these backends opened a WebSocket on page load — even apps with no WebSocket callbacks at all.config.websocket.enabled(bool(self._websocket_callbacks)) was already computed but ignored for the connect decision.Repro: a plain
Dash(backend="fastapi")app with only HTTP callbacks opens a socket on load (WebSocket /_dash-ws-callback [accepted]).Fix (renderer only)
Split "register handlers" from "open the socket":
observers/websocketObserver.ts—initializeWebSocketstill registers all message handlers (onSetProps,onGetPropsRequest,onConnected, …) whenever the infra is present, but only opens the socket eagerly whenconfig.websocket.enabledis true. Keeping handlers registered is what lets the lazy path work: a per-callbackwebsocket=Trueopens the socket on first dispatch via the existinghandleWebsocketCallback → workerClient.ensureConnectedpath, and the handlers are already there to route server-pushedset_props/get_props.visibilitychangereconnect withwasDisconnected, so tab focus never opens a first connection for an app that never had one.AppProvider.react.tsx— the effect now registers the observer whenever the infra is available; the enabled/lazy decision lives entirely insideinitializeWebSocket.Resulting behavior
websocket_callbacks=True(global)websocket=TrueTests
tests/websocket/test_ws_lazy_connect.pyadds three regression tests that use awebsocket_connecthook as a precise server-side probe for "did a socket actually open": never-connect (HTTP only), lazy-connect (per-callback), eager-connect (global). Verified they fail on the pre-fix bundle (the never/lazy cases saw a socket open on load) and pass with the fix. Existingtest_ws_basic.py+test_ws_hooks.py(15 tests) still pass.