Repository navigation
Description
Summary
When a client reconnects, it keeps its control tree. Its View looked up the page's _overlay and _dialogs containers once, in initState. If the reconnect replaces those container instances on the client, every later dialog, snack bar and bottom sheet goes to a container the View no longer listens to. Nothing shows again until the app restarts.
Both servers replace them today:
- ruflet_rails (
Ruflet::ConnectionProtocol, run byRuflet::Rails::Protocol::LocalServer). Theregister_clientreply has an emptypage_patch, and the page then arrives as op patches. Those patches re-send_overlay,_servicesand_dialogsas replace ops, and a replace op always builds new control instances on the client. This happens for a fresh Page and for a resumed one.- On native clients the register after a reconnect has no session id: the engine's non-web
SessionStorereturns null. So every native reconnect gets a fresh Page. - Result: on Rails native apps, dialogs, snack bars and bottom sheets stop working after any reconnect.
- On native clients the register after a reconnect has no session id: the engine's non-web
ruflet run(Ruflet::Server). This server sends the whole page in the register reply'spage_patch, which the client merges by control id, so a container whose_imatches is kept. But the container ids come from the allocator after the root controls, so they move with the content. One column at/gives_overlay102 and_dialogs104; the same app reconnecting at/settings, where the content depends on the route, gives 107 and 109. The ids no longer match, the client builds new containers, and the same failure follows. This happens only when the first render after the reconnect registers a different number of controls, so it is less common.
Reproduce (Ruby wire, main a668af6)
Run from a ruflet checkout: ruby -Ipackages/ruflet_core/lib -Ipackages/ruflet_server/lib -Ipackages/ruflet_rails/lib repro.rb
require "ruflet"
require "ruflet_server"
require "ruflet/rails/session_registry"
require "ruflet/rails/protocol/context"
require "ruflet/rails/protocol/execution"
require "ruflet/rails/protocol/local_server"
class WS
attr_reader :out
def initialize(key) = (@key = key; @out = [])
def session_key = @key
def closed? = false
def close; end
def send_binary(bytes) = @out << Ruflet::WireCodec.unpack(bytes)
end
class Ruflet::Rails::Protocol::LocalServer; public :handle_message; end
class Probe < Ruflet::Server; public :handle_message; end
app = ->(page) { rows = page.route == "/" ? 1 : 6; page.add(Ruflet.column(controls: Array.new(rows) { |i| Ruflet.text("row #{i}") })) }
reg = ->(route) { Ruflet::WireCodec.pack([1, { "session_id" => "s", "page_name" => "", "page" => { "route" => route } }]) }
show = lambda do |label, ws|
puts label
ws.out.each do |action, p|
if action == 1
puts " register_client page_patch: " + %w[_overlay _services _dialogs].map { |k| "#{k}=#{p['page_patch'].dig(k, '_i').inspect}" }.join(" ")
else
puts " patch_control #{p['id']} replace ops: " + p["patch"][1..].map { |op| op[2] }.join(", ")
end
end
end
rails = Ruflet::Rails::Protocol::LocalServer.new(session_registry: Ruflet::Rails::SessionRegistry.new, &app)
ws1 = WS.new(1); rails.handle_message(ws1, reg.("/")); show.("Rails, first connect", ws1)
rails.close_connection(ws1)
ws2 = WS.new(2); rails.handle_message(ws2, reg.("/")); show.("Rails, reconnect (fresh Page)", ws2)
[["/", "ruflet run, reconnect at /"], ["/settings", "ruflet run, reconnect at /settings"]].each do |route, label|
ws = WS.new(3); Probe.new(port: 0, &app).handle_message(ws, reg.(route)); show.(label, ws)
endOutput on main:
Rails, first connect
register_client page_patch: _overlay=nil _services=nil _dialogs=nil
patch_control 1 replace ops: views, route, window, _overlay, _services, _dialogs, title
Rails, reconnect (fresh Page)
register_client page_patch: _overlay=nil _services=nil _dialogs=nil
patch_control 1 replace ops: views, route, window, _overlay, _services, _dialogs, title
ruflet run, reconnect at /
register_client page_patch: _overlay=102 _services=103 _dialogs=104
ruflet run, reconnect at /settings
register_client page_patch: _overlay=107 _services=108 _dialogs=109
The same output appears on 0.0.23.
Client evidence
A widget test replayed this exact wire through the real PageControl, Navigator and ViewControl. It ran with Flutter 3.41.2, on engine codex/engine-initialization 00d1df3c and on the vendored engine in template main 8b08569, which every published gem builds with. The client did a real disconnect and reconnect (onDisconnect, a timed reconnect, a new channel, then register) and kept its tree. A dialog was then shown from the new Page. Both engines gave the same results:
| Scenario | _dialogs id before → after |
same instance | dialog shown |
|---|---|---|---|
| Rails, no reconnect (baseline) | 106 → 106 | yes | 1 |
| Rails, fresh Page after reconnect | 106 → 106 | no | 0 |
| Rails, resumed Page | 106 → 106 | no | 0 |
| ruflet run, reconnect on the same route | 106 → 106 | yes | 1 |
| ruflet run, reconnect where the route renders more rows | 106 → 111 | no | 0 |
Expected: after a reconnect, show_dialog, snack_bar=, bottom_sheet= and open show the overlay as before.
Actual: nothing is shown. The patches reach a new container instance that no View listens to.
Cause
packages/ruflet_server/lib/ruflet/server/connection_protocol.rb,on_register_client: it sendsProtocol.register_response(session_id:)with nopage_patch, then runs the app block and callspage.update.packages/ruflet_core/lib/ruflet_ui/ruflet/page.rb:- On a fresh Page the containers are not mounted, and
prepare_for_reconnect!clears the*_mountedflags on a resumed one. Either waybuild_page_patch_opsre-sends them as replace ops on control 1. assign_wire_id: the containers take allocator ids duringrefresh_control_indexes!, after the root controls, so their ids depend on how much content came first.
- On a fresh Page the containers are not mounted, and
- Engine (
models/control.dart): a replace op always builds new instances throughControl.fromMap.Control.update(thepage_patchpath) keeps an instance only when_imatches.controls/view.dartresolves_overlayand_dialogsonly ininitState, and the View's State survives the reconnect because its key (control_20) and route do not change. - The code already describes this failure.
Page#reset_for_reload!says re-sending the containers "replaces their client-side instances and detaches them".
First paint (performance, separate)
The same register flow streams every intermediate patch. With 200 page.add calls in the app block, Rails sends 201 messages and 570 KB, where ruflet run sends 1 message and 5.8 KB. A typical app sends 4 messages and 1.4 KB, against 1 message and 0.7 KB. Most of the large case comes from each page.add re-sending the whole view.
Buffering the whole app block as Ruflet::Server does would change what Rails users see. An app block that shows a spinner, does slow work and then shows content would never show the spinner, because the first message would wait for the whole block. So that part is not proposed as a bug fix here.
Proposal
- Give
_overlay,_servicesand_dialogsfixed wire ids inPage#initialize, as the page (1), window (2) and implicit view (20) already have. Use ids the allocator never hands out, for example 3, 4 and 5. A new Page's containers then always merge into the client's existing ones. - In
ConnectionProtocol#on_register_client, send the containers (and only the containers) as the register reply'spage_patch, before the app block runs, and mark them mounted. The rest of the page keeps streaming as op patches, so first paint stays progressive. - Until the first view patch after that reply, still deliver container content that the app block set without sending anything (
page.overlay << control, a dialog given toset_view_props). Today the first view patch carries the containers whole, which is how that content reaches the client. - Tests: a Rails fresh reconnect and a resumed reconnect both keep the client's containers, a
ruflet runreconnect on a route with more content does the same, and Rails first paint stays progressive.
Re-resolving the containers in the engine's ViewControl.didUpdateWidget would be a separate engine-side hardening.
Related but different: #138 covers how the _dialogs list is patched (whole-list replace) while a client stays connected. #92 (fixed by #97) covered handler errors that forced a reconnect, not what breaks after one.
Activity
fix: flush changes inside a container before the first view patch (#142)