Repository navigation
bare websockets - #6932
bare websockets#6932benedikt-bartscher wants to merge 105 commits into
Conversation
Merging this PR will not alter performance1 warning
Performance Changes
Comparing Footnotes
|
[High impact] The changes since the previous review appear safe to merge. SummaryReplaces the default Socket.IO connection with a plain WebSocket and adds channels for custom messages and binary data.
Reviews (87) · Last reviewed commit: "Report a connect timeout at once instead..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 19 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
…ace, awaited disconnect cleanup; revert server-wide ws_max_size
There was a problem hiding this comment.
All reported issues were addressed across 17 files
Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 4 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
… message shape, log protocol closes; add transport benchmarks
…cut client watchdog churn
There was a problem hiding this comment.
All reported issues were addressed across 4 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 6 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
…icorn] extra with gunicorn
There was a problem hiding this comment.
All reported issues were addressed across 6 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
Granian sets TCP_NODELAY on listeners it binds itself, but the harness binds its own socket, so with the Redis state manager each update stalled ~40 ms on delayed ACKs and test_client_side_state raced it. Also poll the client storage reads that follow an event or navigation.
Port upstream's reconnect back-off (reflex-dev#7605) to the transport abstraction: retryConnect keeps the branch's addConnectError helper and caps the attempt count at MAX_CONNECT_ERRORS. Socket.IO reports a transport closed before the namespace connected as a disconnect, which now retries with back-off; the plain WebSocket reports it as connect_error, so it gets the same back-off. The state.js back-off tests run over both transports, with the fake socket reporting a close before connecting the way each transport does, and read connect errors through upstream's lazily applied updaters.
Port upstream's EventNamespace changes to the moved namespace modules: the trusted proxy hops for the client IP (reflex-dev#7614) apply in build_static_router_data, and the cancellation of a disconnected socket's page-load chains (reflex-dev#7415) runs in BaseEventNamespace.handle_disconnect, so both transports get them. The branch already decoded non-UTF-8 header values (reflex-dev#7602). Their tests move from tests/units/test_app.py to tests/units/test_event_namespace.py, and the transport benchmark's app double gains the event processor attribute the disconnect reads. Port the oversized hydrate check of reflex-dev#7551 to the Socket.IO transport only: state.js wraps the socket.io encoder where it creates the Socket.IO socket, since the plain WebSocket has no engine.io encoder. Its state.js tests run over Socket.IO, whose fake socket now encodes CONNECT packets, and the reconnect back-off of reflex-dev#7605 stays in place for both transports. uv.lock is upstream's, relocked for the branch's dependencies.
The plain WebSocket sends its hydrate with the connect frame, before any handshake could announce the server's message limit. A frame over the limit closed the socket with 1009, which the client reported as a generic connection failure and retried with back-off forever. The server now names the limit in that close's reason, and the transport reports such a close as the hydration_payload_too_large error reflex-dev#7551 added for Socket.IO, with the frame's size and the limit when the reason gives it. A reconnect over a limit an earlier handshake announced is refused before it is sent. retryConnect treats the error as final for both transports: it records it once, schedules no retry and keeps wait_connect set, so reconnect() does not rebuild the hydrate, and the toast and banner keyed on the error code show it.
When a token moved to another socket, the transport dropped the stale recipient but send_to_token still returned True; the sender now reports whether it accepted anyone and the fan-out returns that.
There was a problem hiding this comment.
All reported issues were addressed across 4 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
A linked session whose connection was closing, overflowed its send backlog, or was already gone still counted as a recipient; the connection now reports whether it queued the frame.
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
A frame that overflowed the send backlog was queued before the connection closed, so the writer could still deliver a send reported as a miss.
…g it An app that replaced EventNamespace and overrode on_event saw every event but the hydrate on connect. The boot event goes through on_event again, re-raised while booting so a failure still refuses the connect, and overriding on_event now warns in favor of handle_event, which runs for every event on both transports.
The added tests checked fragments of errors, warnings and log output; compare each in full, with session ids normalized to a placeholder and the expected traceback output derived from the raised exception. Capture the token-loss test's log on the reflex logger, so its check still sees debug records when that logger has a level of its own.
The websocket library and frame size warnings were checked by fragment; compare the captured records in full.
There was a problem hiding this comment.
All reported issues were addressed across 8 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
The app double's enqueue returned None, so in dev mode handle_event's add_done_callback raised AttributeError on every inbound event and both inbound benchmarks measured the failure policy instead of the transport. It now returns a resolved future, like the EventFuture enqueue returns, and every transport benchmark fails if anything was logged at error level. Walltime, 100 inbound events: websocket 146 us -> 140 us, Socket.IO 318-324 us -> 313-317 us.
The error code and message for a hydrate over the backend's limit were defined in both websocket.js and state.js. websocket.js now exports HYDRATION_PAYLOAD_TOO_LARGE and hydrationTooLargeError(size, limit, unit), which the socket.io path in state.js uses too; the messages are unchanged. The state.js harness builds the error with the real helper and pins the socket.io message exactly.
monkeypatch.setattr saves the old value through getattr, which for reflex.app's lazy Socket.IO exports runs the module __getattr__; its undo then stores the resolved class as a real module attribute. That shadows __getattr__, so test_app_socketio_reexports_without_the_extra failed whenever it ran after a namespace-replacing test (17 of 20 pytest-randomly seeds). Patch the module dict instead, which undo restores exactly, and check after those tests that no Socket.IO name is left behind.
There was a problem hiding this comment.
All reported issues were addressed across 1 file (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Port upstream's EventNamespace changes to the moved namespace modules, so both transports get them. The X-Forwarded-For validation of reflex-dev#7880 runs in build_static_router_data, with _is_port and _forwarded_client_ip next to _trusted_forwarded_value; _clip moves there too for its debug line, and reflex.app imports it back. The suppress_warnings parameter of reflex-dev#7921 is on BaseEventNamespace.emit_update, and the awaited lost-and-found subscription of reflex-dev#7938 runs in BaseEventNamespace.handle_connect. The X-Forwarded-For tests move to tests/units/test_event_namespace.py. The modify_state test of reflex-dev#7921 builds a WebsocketEventNamespace and reads the warning from reflex.event_namespace, which logs it now.
The check ran as a test of its own, so it only caught a leak when the random order put it after the tests that replace the namespace class. Both tests now replace it through one fixture, which checks reflex.app once its patch is undone.
A warning another logger emits during the test no longer fails them.
page.route_web_socket opens a held socket for the page right away. The plain WebSocket sends its connect frame as soon as its socket opens, so the frame carried the first route and was dropped before the test navigated, and the session never opened. Socket.IO first waits for the engine.io open packet, which the route held back. An init script now keeps the event socket CONNECTING, as the page sees it, until the test releases it, which holds both transports before the connect request.
The 20-second session timeout only closed the socket. On a dead link the browser fires onclose when its closing handshake times out, up to a minute later, and until then no connect_error reached the event loop, so it waited to retry. The timeout now tears the socket down and reports the connect error itself.
saves ~11.6 KB gz / ~41 KB raw frontend size (a small js chunk replaces whole socketio)
dropped python deps: wsproto, simple-websocket, python-socketio, python-engineio, bidict
new benchmarks in test_event_transport.py show about 2x performance increase compared to socketio
also migrates AppHarness to granian for consistency. otherwise downstream tests using AppHarness still need uvicorn and wsproto/websockets
related reflex-dev/xy#513