Repository navigation
Emit App.modify_state deltas only to tokens a browser connected with - #7921
Conversation
Modifying a SharedState by its shared token (from an API route or a background task) ended with emit_update to the shared token. No browser connects with that token, so every call logged "Attempting to send delta to disconnected client" even though the linked clients got the update through the shared state fan-out. Skip the emit when the modified state is the shared copy for that token, which is the SharedState whose _linked_to is the token's ident. A client token that is not connected still logs the warning. Closes #7913
[Low impact] The PR appears safe to merge. Summary
The previous delivery and docstring findings are fixed. No new actionable issues were found. Tests were inspected, not run. Reviews (7) · Last reviewed commit: "Suppress the disconnected client warning..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Merging this PR will degrade performance by 6.61%3 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
A shared state token never has a socket, so App.modify_state now gates the emit on the root state's rx_router_session.session_id, which the socket connect records. This replaces the per-class _linked_to check: a connected browser token that another client links to keeps its emit, a shared token that no client linked to yet no longer warns, and the check no longer depends on the class passed in the token.
A state that expires while its browser stays connected comes back without a session id. App.modify_state now also emits when the token manager has a socket for the token, so that browser keeps getting out-of-band updates. The test gets an expired-state case, docstrings on its helpers, and only counts warnings from the reflex.app logger.
With REFLEX_REDIS_URL set, EventNamespace creates a RedisTokenManager: the disconnected token then logs no warning, and the socket records the test links stay in redis and break test_redis_token_manager_enumerate_tokens.
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
Add a suppress_warnings keyword to EventNamespace.emit_update and pass it from App.modify_state, as suggested in review. emit_update already looks up the socket of the token, so connected tokens still get the emit and only the warning for a token without a socket is skipped. This replaces the session id and connection check added to modify_state. The modify_state emit test now checks the socket ids each update is emitted to, that modify_state logs no warning for a token without a socket, and that a regular emit_update to a disconnected token still logs it.
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.
All Submissions:
Type of change
Changes To Core Features:
Description
As described in #7913,
app.modify_state(rx.BaseStateToken(ident=shared_token, cls=MySharedState))(from an API route or a background task) logsAttempting to send delta to disconnected client '<shared_token>'on every call.modify_statefinishes withemit_update(..., token=token.ident)whenever the delta is not empty. No browser ever connects with a shared token, so the emit has no receiver. The linked clients get the update through the shared state fan-out (_do_update_other_tokens).EventNamespace.emit_updatetakes a newsuppress_warningskeyword argument (defaultFalse), andApp.modify_statepassessuppress_warnings=True.emit_updatealready looks up the socket of the token, so a connected token still gets the emit and a token without a socket still gets nothing. The flag only skips the warning. Every other caller keeps the default, so with the local token manager it still logs the warning.modify_statesends nothing and logs no warning. A regularemit_updateto that token still logs the warning.modify_stateadds no lookup of its own: the only check is the socket lookupemit_updatealready makes. With the Redis token manager, a token whose socket lives on another worker still goes to lost and found, and a token without an owner is dropped without a warning, as before.Test:
test_modify_state_emits_only_to_connected_tokensintests/units/test_app.pyconnectsclient,target,expiredandgonewithlink_token_to_sid, purges the state ofexpired, and disconnectsgone. It checks the exact socket ids each update is emitted to, that none of thesemodify_statecalls logs a warning, and that a directemit_updatetogonestill logs exactly one warning. The test fails onmain, where theshared,unlinkedandgonemodifications log the warning.test_app_modify_state_cleanchecks thatmodify_statepassessuppress_warnings=True.closes #7913