Skip to content

ruflet_rails: a raising event handler closes the WebSocket and restarts the session (ruflet run keeps it) #92

Closed

Description

@AdamMusa

Summary

When an event handler raises under ruflet_rails, the client gets session_crashed and the WebSocket is closed. The same app under ruflet run logs the error and keeps the session going.

ruflet_rails hosts its sessions in Ruflet::Rails::Protocol::LocalServer, which includes Ruflet::ConnectionProtocol. ConnectionProtocol#handle_message does not rescue errors per message, while Ruflet::Server#handle_message does. So the handler's exception reaches ConnectionProtocol#run_connection, which:

  1. logs it (RUFLET CRASH: ... in the Rails log),
  2. sends session_crashed, so the client shows its error screen,
  3. closes the socket.

The client then reconnects and registers again. The app block runs from scratch, and everything the session held is lost.

This happens on main (dd924eb) and in the published gems: connection_protocol.rb, local_server.rb and endpoint.rb are identical at v0.0.23.

Minimal repro

Any handler that raises once is enough. For example, a Form validator that raises, or this button:

clicks = 0
app = lambda do |page|
  page.add(Ruflet.button(content: "Save", on_click: lambda { |_e|
    clicks += 1
    raise ArgumentError, "validation failed" if clicks == 1
  }))
end

Run it through each receiver. Register, then send the same control_event click twice:

  • Ruflet::Server (what ruflet run uses)
  • Ruflet::Rails::Protocol::LocalServer, through the Rack-hijack Endpoint (what ruflet_rails uses)

Result over real socket pairs with masked WebSocket frames, the same bytes the Flutter client sends:

[ruflet run   ] clicks: 2, after_first: [], after_second: ["patch_control(count=1)"], socket_eof_after_clicks: false,
                log: "[embedded server] handle_message error: ArgumentError: validation failed"
[ruflet_rails ] clicks: 1, after_first: ["session_crashed(validation failed)"], after_second: [], socket_eof_after_clicks: true,
                log: "server error: ArgumentError: validation failed"

Expected

The handler error is logged, and the connection and session stay up, the same as under ruflet run. ConnectionProtocol describes itself as "one connection loop shared by every server". Ruflet::Server#handle_message rescues per-message errors with this comment: "A per-message handler error ... must not tear down the whole WebSocket connection ... Log it and keep the session alive". Commit 035df8d added that rescue as a fix to the standalone server, but it never reached ConnectionProtocol.

Actual

Under ruflet_rails, one raising handler sends session_crashed, closes the socket and forces a reconnect that re-runs the app. Clicks after the first never reach their handler on that connection.

Code location

  • packages/ruflet_server/lib/ruflet/server/connection_protocol.rb: run_connection (lines 57-70), handle_message (lines 84-107) and on_control_event. Nothing rescues around page.dispatch_event.
  • For comparison, packages/ruflet_server/lib/ruflet/server.rb lines 583-591 (Server#handle_message rescue).

Errors while registering the app (the app block raising on register) should still send session_crashed, as they do today on both receivers. Protocol errors (unknown action, undecodable frame, event before register) are a separate case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions