Skip to content

Emit App.modify_state deltas only to tokens a browser connected with - #7921

Merged
FarhanAliRaza merged 5 commits into
reflex-dev:mainfrom
mmporong:fix-shared-token-disconnect-warning
Nov 20, 2026
Merged

FarhanAliRaza merged 5 commits into
reflex-dev:mainfrom
mmporong:fix-shared-token-disconnect-warning

Conversation

@mmporong

@mmporong mmporong commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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_state sends nothing and logs no warning. A regular emit_update to 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

View guided diff

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
@mmporong
mmporong requested a review from a team as a code owner November 18, 2026 00:38
@greptile-apps

greptile-apps Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge.

Summary

App.modify_state now suppresses warnings for tokens without a connected browser, without changing where updates go.

  • State edits stay quiet when their token has no browser socket.

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

Comment thread reflex/app.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/units/test_app.py Resolved
Comment thread tests/units/test_app.py Resolved
Comment thread reflex/app.py Resolved
@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 6.61%

3 warnings

⚠️ 14 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

❌ 1 regressed benchmark
✅ 145 untouched benchmarks
⏩ 8 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_route_matching[1000] 90 ms 96.4 ms -6.61%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mmporong:fix-shared-token-disconnect-warning (4288c7f) with main (810bbd2)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 18, 2026
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.
@FarhanAliRaza FarhanAliRaza changed the title Do not emit to a shared state token in App.modify_state Emit App.modify_state deltas only to tokens a browser connected with Nov 18, 2026
Comment thread reflex/app.py Resolved
Comment thread tests/units/test_app.py Resolved
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.
@greptile-apps

greptile-apps Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

Comment thread reflex/app.py Resolved
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.
@FarhanAliRaza
FarhanAliRaza merged commit 465527b into reflex-dev:main Nov 20, 2026
167 of 168 checks passed
benedikt-bartscher added a commit to benedikt-bartscher/reflex that referenced this pull request Nov 22, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

modify state on a SharedState token warns "disconnected client" for every update

3 participants