Emit App.modify_state deltas only to tokens a browser connected with
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- Bug fix (non-breaking change which fixes an issue)
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully ran tests with your changes locally?
Description
As described in #7913, app.modify_state(rx.BaseStateToken(ident=shared_token, cls=MySharedState)) (from an API route or a background task) logs Attempting to send delta to disconnected client '<shared_token>' on every call. modify_state finishes with emit_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_update takes a new suppress_warnings keyword argument (default False), and App.modify_state passes suppress_warnings=True. emit_update already 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.
- A shared token that clients link to: no socket emit and no warning. The linked clients still get the fan-out.
- A shared token that no client has linked to yet: no socket emit and no warning.
- A browser token that another client links to: the browser is connected, so it still gets the emit.
- A browser whose state expired while it stayed connected: the browser has a socket, so it still gets the emit.
- A browser that connected and then disconnected:
modify_statesends nothing and logs no warning. A regularemit_updateto that token still logs the warning. - A token that no browser ever connected with: no socket emit and no warning. Before, the emit could not reach anyone either.
modify_state adds no lookup of its own: the only check is the socket lookup emit_update already 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_tokens in tests/units/test_app.py connects client, target, expired and gone with link_token_to_sid, purges the state of expired, and disconnects gone. It checks the exact socket ids each update is emitted to, that none of these modify_state calls logs a warning, and that a direct emit_update to gone still logs exactly one warning. The test fails on main, where the shared, unlinked and gone modifications log the warning. test_app_modify_state_clean checks that modify_state passes suppress_warnings=True.
closes #7913