Skip to content

fix: a raising event handler no longer ends the ruflet_rails session - #97

Merged
AdamMusa merged 5 commits into
mainfrom
fix/rails-handler-error-keeps-session
Nov 20, 2026
Merged

AdamMusa merged 5 commits into
mainfrom
fix/rails-handler-error-keeps-session

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #92

What changed

Under ruflet_rails, an event handler that raised ended the session. Ruflet::ConnectionProtocol is the loop ruflet_rails' LocalServer runs, and it let the exception reach run_connection. That sent session_crashed, closed the socket and made the client register again into a fresh session, so the app block ran again from the start. ruflet run (Ruflet::Server) already logs these errors and keeps the session (035df8d).

packages/ruflet_server/lib/ruflet/server/connection_protocol.rb:

  • on_control_event now rescues StandardError around page.dispatch_event and passes it to a new overridable hook, handler_error(ws, error).
  • By default, handler_error logs through log_connection_error. LocalServer still sends that to Rails.logger with the same RUFLET CRASH: ... text and backtrace as before.

The rescue covers only event dispatch, because that is the one place app handler code runs: on_click, validators, page.on_route_change and the like.

  • update_control runs no app code.
  • Invoke-result callbacks are already rescued in Page#handle_invoke_method_result.

These cases behave exactly as today:

No app code changes are needed, and ruflet run is unaffected because Ruflet::Server does not include ConnectionProtocol.

Behavior changes

  • ruflet_rails / any ConnectionProtocol host: an exception raised by an event handler is logged, and the connection and session stay up. Before, the client got session_crashed (its error screen), the socket was closed, and the reconnect re-ran the app with all session state lost. One sub-case differed: a handler that raised a disconnect-class error (IOError, Errno::EINVAL and the others in disconnect_error?) closed the socket silently, with no log and no session_crashed. That error is now logged too and the connection stays up. This now matches ruflet run. It is a bug fix: the server-side rescue was added to the standalone server as a fix, and ConnectionProtocol describes itself as the loop every server shares.

How verified

Regression tests fail without the fix: I restored the old connection_protocol.rb with the new tests in place.

  • packages/ruflet_server/test/connection_protocol_test.rb, 5 new tests:
    • a raising on_click is logged and the next click on the same connection still runs, with no session_crashed and one session removal (at disconnect);
    • the session and page stay in place after the error;
    • a raising page.on_route_change is logged too;
    • boundary: a register-time raise still sends session_crashed and closes;
    • boundary: an event before register still raises Session not found.
    • Without the fix, 3 fail (1 failure, 2 errors). The two boundary tests pass either way.
  • packages/ruflet_rails/test/protocol_endpoint_test.rb, 1 new end-to-end test through the real Rack-hijack Endpoint, LocalServer and ConnectionProtocol, using masked WebSocket frames over a socket pair:
    • the first click raises, and the second click's patch_control (count=2) arrives;
    • no session_crashed, the registry keeps the session, and the error is logged.
    • Without the fix it fails with session_crashed (action 6) received.

Suites on the final head fa4a57ef (this branch merged with main bc45ad66; the PR diff against main is unchanged), macOS, one process per test file:

Ruflet::ConnectionProtocol, the loop ruflet_rails' LocalServer runs, let
an exception from application code reach run_connection. run_connection
logged it, sent session_crashed (the client shows its error screen) and
closed the socket. The client then re-registered into a fresh session:
the app block ran again and everything the session held was lost. One
on_click, validator or page.on_route_change that raised was enough.

Ruflet::Server (ruflet run) already rescues per-message errors, logs
them and keeps the session alive (035df8d). ConnectionProtocol now
does the same for event dispatch: on_control_event rescues StandardError
around page.dispatch_event and passes it to a new handler_error(ws,
error) hook, which logs it through log_connection_error. LocalServer
still sends that to Rails.logger, with the same text as before.

Unchanged: an app that raises while registering still gets
session_crashed and a closed socket; unknown actions, undecodable frames
and events before register still raise. update_control runs no app code,
and invoke callbacks are already rescued in Page.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant