Repository navigation
fix: a raising event handler no longer ends the ruflet_rails session - #97
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #92
What changed
Under
ruflet_rails, an event handler that raised ended the session.Ruflet::ConnectionProtocolis the loopruflet_rails'LocalServerruns, and it let the exception reachrun_connection. That sentsession_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_eventnow rescuesStandardErroraroundpage.dispatch_eventand passes it to a new overridable hook,handler_error(ws, error).handler_errorlogs throughlog_connection_error.LocalServerstill sends that toRails.loggerwith the sameRUFLET 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_changeand the like.update_controlruns no app code.Page#handle_invoke_method_result.These cases behave exactly as today:
session_crashedand a closed socket. PR ruflet_server: test every protocol action on both receivers and every codec format; normalize invoke replies like ConnectionProtocol #89'stest_connection_protocol_an_app_that_raises_on_register_reports_session_crashedstill passes.Session not found) still raise.No app code changes are needed, and
ruflet runis unaffected becauseRuflet::Serverdoes not includeConnectionProtocol.Behavior changes
ConnectionProtocolhost: an exception raised by an event handler is logged, and the connection and session stay up. Before, the client gotsession_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::EINVALand the others indisconnect_error?) closed the socket silently, with no log and nosession_crashed. That error is now logged too and the connection stays up. This now matchesruflet run. It is a bug fix: the server-side rescue was added to the standalone server as a fix, andConnectionProtocoldescribes itself as the loop every server shares.How verified
Regression tests fail without the fix: I restored the old
connection_protocol.rbwith the new tests in place.packages/ruflet_server/test/connection_protocol_test.rb, 5 new tests:on_clickis logged and the next click on the same connection still runs, with nosession_crashedand one session removal (at disconnect);page.on_route_changeis logged too;session_crashedand closes;Session not found.packages/ruflet_rails/test/protocol_endpoint_test.rb, 1 new end-to-end test through the real Rack-hijackEndpoint,LocalServerandConnectionProtocol, using masked WebSocket frames over a socket pair:patch_control(count=2) arrives;session_crashed, the registry keeps the session, and the error is logged.session_crashed(action 6) received.Suites on the final head
fa4a57ef(this branch merged with mainbc45ad66; the PR diff against main is unchanged), macOS, one process per test file:ruflet_server: 14 files, 0 failures on Ruby 4.0.5. Also on Ruby 3.4.5 (CI's.ruby-version): 14 files, 0 failures.ruflet_rails(bundle exec, exported tree): 17 files, 0 failures.cd packages/ruflet && ruby test/compat/compat.rb --jobs 4: 292 goldens checked, exit 0. This includes the--rails-endpointpage goldens that runLocalServerandConnectionProtocol.ruby -Ilib -Itest test/compat_goldens_test.rb: 300 runs, 0 failures.protocol_action_contract_test.rbas of ruflet_server: test every protocol action on both receivers and every codec format; normalize invoke replies like ConnectionProtocol #89dc0ec81b, run onfa4a57ef: 21 runs, and everyconnection_protocolcase passes. The one failure istest_server_invoke_control_method_reply_reaches_on_result, which needs ruflet_server: test every protocol action on both receivers and every codec format; normalize invoke replies like ConnectionProtocol #89's ownserver.rbfix. (ruflet_server: test every protocol action on both receivers and every codec format; normalize invoke replies like ConnectionProtocol #89 has moved since; its newer invoke-reply cases need thatserver.rbchange too.)IOErrororErrno::EINVAL, onfa4a57ef:clicks=2, the error is logged, and nosession_crashedis sent.fa4a57ef, gives the same result on both receivers:clicks: 2, after_second: ["patch_control(count=1)"], socket_eof_after_clicks: false.