Skip to content

fix: report hot reload readiness for apps that start the server directly - #70

Merged
AdamMusa merged 25 commits into
mainfrom
fix/run-reload-readiness-fallback
Nov 20, 2026
Merged

AdamMusa merged 25 commits into
mainfrom
fix/run-reload-readiness-fallback

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Problem

Since #57 and #58, ruflet run holds an r pressed during boot until the hot-reload harness writes a byte on RUFLET_READY_FD. The harness wrote that byte only on the Ruflet.run interceptor path, and two ways of starting an app never take it:

  • DSL apps (Ruflet.app { ... }.run, app { ... }.run, Ruflet::DSL::App#run) and scripts that call Ruflet::Server.new(...).start themselves.
  • A bundled ruflet_core without Ruflet.with_run_interceptor: the harness logged "running without hot reload" and never reported.

For those apps every r printed Reload queued: the app is still starting and will reload once it is up, and nothing reloaded. The file watcher never started either, so saves did nothing. On Ctrl-C the session ended with HotReload::Error: ... never called Ruflet.run and exit 1. The end-to-end table under "Verification" shows this on main.

The reviews of this PR found more cases on the same path. All of them are fixed here:

  • The start is in a file the script requires (main.rb does require_relative "app"). The first reload hung on that require's lock. That includes the case where bootsnap compiles the file from source, so its frames read <compiled>.
  • The script's own $VERBOSE = nil was replaced while its server served.
  • A USR1 handler the app installs itself stopped running.
  • A handler that calls the one it replaced (previous = Signal.trap("USR1") { |s| ...; previous.call(s) if previous.respond_to?(:call) }) recursed until a SystemStackError ended the app. This was a regression in 4863c406, fixed in 8296f411.

Fix (packages/ruflet only)

The PR changes lib/ruflet/hot_reload.rb, lib/ruflet/cli/run_command.rb and their two test files. ruflet_core, ruflet_server, ruby_runtime, the template and the engine are untouched.

  1. The app's own Server#start. HotReload::ServerStartCapture is prepended to Ruflet::Server once the app defines that class. It acts only on a start that the script makes synchronously, on the fiber that loads it, while the runner loads it.
    • First load. The app's own server object serves the app, with the class, host and port the app gave it. The runner adds hot reload around it: it starts the watcher, re-traps USR1, reports ready, and points the server's @app_block at the latest loaded block.
    • Reload. The start hands over the new block, and the load ends there (throw). A script that keeps its server across loads ($server ||= ...) keeps serving it.
    • Once that server stops, the rest of the script runs, as under plain ruby. A later Ruflet.run is served by the runner's own server, with hot reload. On a reload, the stopped server's start returns at once without starting anything.
    • $VERBOSE is the app's own while that server serves. That is the value from before the load, unless something assigned $VERBOSE, $-v or $-w while the script loaded (seen through trace_var), in which case it is what the load left.
    • USR1 (AppTrapChain). While that server serves, USR1 asks for one reload and then runs the handler the app installed, with the signal number.
      • It runs that handler once per USR1. While the handler runs, a call back into the chain does nothing, and a call to the runner's own handler asks for no second reload.
      • A handler that raises raises in the main thread, and the next USR1 still reloads and runs it.
      • A reload that traps USR1 again hands its new handler to the chain. A reload that does not keeps the previous one, so a handler installed only on the first load keeps running.
      • When the server stops, the app's latest handler is put back, unless the app trapped USR1 again since.
      • A command string ("IGNORE" and the like) is not run while the server serves, and it is put back when the server stops.
    • Not touched: starts on threads or fibers the app creates, starts in a forked child, starts outside a runner, in-process-bridge servers, and (on the first load) a start after an intercepted Ruflet.run. They run as without the runner.
  2. A start in a file the script requires. The runner notes the files whose require the start is nested in. Those are the top-level frames (<top (required)>, <main> or <compiled>) above the script's own bottom-most frame.
    • While a reload of such an app runs, and only then, HotReload::InFlightRequires (prepended to Object) lets require and require_relative of those files load them on the reloading fiber, once per reload.
    • Every other require, and every require on another thread or fiber, goes to Kernel's.
  3. ruflet_server is still loaded by the app. A TracePoint(:class) waits for class Ruflet::Server to open, then disables itself.
  4. Trap first. The harness traps USR1 before any app code. It reports t only if that trap succeeded, and . only once the trap is in place again after the app code. Otherwise it closes the fd without reporting, so ruflet run never sends USR1 to an untrapped app.
  5. Bounded fallback (run_command.rb). Suppose an app has reported t but not . within READY_FALLBACK_SECONDS (5 s). ruflet run then sends the queued r as USR1, the old way. The harness's early trap keeps that signal and applies it once the watcher runs. Nothing is sent before t.
  6. R and the old app's late report. mark_child_ready uses main's :retired_pid guard (New controls and their protocol, client actions, local auth, and review follow-ups #72). Neither a late . nor the old app's fallback can then mark the session ready between R and the respawn.

What changes for existing apps

  • ruflet run --no-reload and plain ruby main.rb: no change, because the harness is not used. The CLI's options and messages are unchanged.
  • Ruflet.run apps: no change, except as follows.
    • USR1 is trapped before their code loads, so a USR1 that arrives during boot is applied once the app is up. On v0.0.23 and main it killed the booting app. An r pressed during boot is still queued and applied once the app is up, as on main.
    • Boot over 5 s. If boot takes more than 5 s, a queued r is sent at the fallback instead of at the ready report. It is still applied once, after boot, as on main, unless the app's own code replaced the USR1 trap by then. Then that r runs the app's handler and does not reload (with "DEFAULT" it ends the app, as any r during boot did on v0.0.23).
    • A USR1 handler such an app installs is handled as on main: once the app is up, the runner's trap replaces it.
    • Two reload hang fixes. One is a Ruflet.run followed by a synchronous Server#start. The other is the app's own start followed by Ruflet.run. On v0.0.23 and main, the first reload of either blocked in a new server's start until Ctrl-C, and later saves did nothing. Now every save reloads, and nothing more is bound.
  • Apps that start Ruflet::Server synchronously, in the script or in a file it requires, are served by their own server as before. They now also get hot reload:
    • The server's @app_block is replaced by one that renders the latest block.
    • A reload re-runs the script up to that start, as reloads of Ruflet.run apps always have.
    • In the required-file layout, a reload loads the files that hold the start.
    • Warnings while the server serves. They now show, unless the script (or a gem it loads) assigned $VERBOSE while loading. This is the one change from v0.0.23, where that server ran inside the harness's silenced load. A script's own $VERBOSE = nil also silences the harness's reloaded in lines, as RUBYOPT=-W0 does on main.
    • An app that traps USR1 itself gets both a reload and its handler on every r and outside USR1, and has its handler back once the server stops. On v0.0.23, r ran only the handler; on main, r stayed queued.
    • Ctrl-C exits 0, without the HotReload::Error backtrace.
  • Still not reloaded: a start reached through an explicit Kernel.require or Kernel.require_relative receiver, or inside a lock the script holds while serving. The app keeps being served, but saves and r do nothing.
  • Apps that serve from their own threads are served as before and never report ready. Their r reaches the harness's trap at the fallback, where it does nothing. On v0.0.23 it killed the app; on main it stayed queued.
  • Dev-only costs:
    • one unstarted Ruflet::Server per reload of a DSL app;
    • the TracePoint until Ruflet::Server is defined;
    • a trace_var on the $VERBOSE names during the first load;
    • in the required-file layout, the InFlightRequires module in Object's ancestors after the first reload;
    • one realpath per stack frame when the app's own server starts.

Tests

The new tests are deterministic. They synchronise on FIFOs, pipes, queues and polling, and their timeouts only stop hangs. Two tests depend on timing:

  • test_the_ready_fallback_waits_its_window_after_the_trap_report checks a lower bound on time.
  • test_a_usr1_handler_that_calls_the_one_it_replaced_asks_for_no_second_reload waits 0.5 s to see that no second reload follows.

The two files:

  • test/hot_reload_test.rb: 57 new tests, 70 runs in the file. They cover DSL and direct starts; required files, also under compile caches that label frames <main> and <compiled>; requires during such a reload; $VERBOSE; and the app's own USR1 handlers. Two byte-protocol assertions change from . to t..
  • test/run_command_test.rb: 16 new tests, 51 runs in the file. They drive the real harness in a child process, ruflet run's own spawn and readiness pipe, and the fallback's pipe-level watcher. read_progress gains an optional byte count, and the existing boot-queue tests pass ready_fallback_seconds: nil.

New this round (15274af0, tests only):

  • assert_r_reloads_and_runs_the_apps_handler now presses r presses: times and expects one USR1 per press; before, it always pressed twice. So test_r_reloads_a_dsl_app_whose_usr1_handler_calls_the_one_it_replaced presses r 3 times, and test_r_reloads_a_dsl_app_that_traps_usr1_itself_and_still_runs_its_handler still presses it twice.
  • test_a_usr1_handler_installed_only_on_the_first_load_runs_on_every_usr1 uses $usr1_trapped ||= Signal.trap("USR1") { ... }. Three USR1s each run that handler once and reload, and once the server stops, a USR1 runs it once. This pins the unless previous.equal?(@handler) check in AppTrapChain#take_over.

Verification (this round, on the final head 15274af0)

Homebrew Ruby 4.0.5, GEM_HOME unset, no -W0, one process per test file.

  • Scope.

    • git diff --stat origin/main...HEAD lists only the 4 files above.
    • git diff origin/main...HEAD -- packages/ruflet_core packages/ruflet_server ruby_runtime tools is empty. The embedded VM compiles only ruflet_core/lib and ruflet_server/lib (ruby_runtime/vm/mrbgems/ruflet-framework/mrbgem.rake), so this PR changes nothing it builds.
    • Published 0.0.15-0.0.23 gems, which fetch the template live, are unaffected.
    • git diff --quiet 8296f411 HEAD -- packages/ruflet/lib succeeds: the lib is unchanged since the last review.
  • Suites, file by file:

    suite files passing
    ruflet_core 189/189
    ruflet_server 13/13
    ruflet 13/13 (hot_reload_test 70 runs, run_command_test 51)
    ruflet_rails 17/17

    The worktree stayed clean.

  • Other Rubies, both files, all passing: rbenv 4.0.0 (bootsnap 1.24.6), rbenv 3.4.5 (bootsnap 1.26.1), and Homebrew 3.2.9 without bootsnap (its bootsnap test is skipped).

  • Under load: both files 30 times in a row, twice.

    • The first set ran beside the planted-bug runs and the other checks, at a 1-minute load average of 9 to 106.
    • The second set also had 8 CPU-spinning Ruby processes and the end-to-end sessions, at 79 to 145.
    • All 120 file runs passed. That includes every timing-sensitive test: the USR1 tests, and the run_command tests that wait for the harness's reloaded in line between presses of r.
  • Planted bugs: 83 bugs, each planted alone in a copy of this head with both files run against it, four copies at a time. An unplanted copy passed both files.

    • All 65 bug rows fail at least one test. They cover the start capture, required-file routing, requires during a reload, $VERBOSE, the fallback and AppTrapChain.
    • This round's row removes the take_over check. It fails test_a_usr1_handler_installed_only_on_the_first_load_runs_on_every_usr1 with "the app's handler on USR1 2 never ran"; last round it passed both files.
    • The 18 equivalents pass every test, and none changes what an app sees:
      • Redundant rescues: no rescue in RequiresInFlight#path_for, where a bad feature then raises what Kernel's require raises; name instead of Module#name, or no rescue, in the TracePoint, two guards for one case (an app class whose name takes arguments).
      • Idempotent repeats: InFlightRequires' methods defined, or the module prepended, on every reload; no uniq on the in-flight files, which are hash keys.
      • Guards for cases that do not occur: no @app_block guard; no autoload guard (nothing registers an autoload for Ruflet::Server); .so features not told apart (only .rb files are recorded); in-flight files keyed by path rather than real path; frames without a path not skipped (CRuby reports none).
      • State nothing reads afterwards: no entrypoint mark before the reload's throw; a second t resetting the fallback deadline (t is reported once per process); reloads tracing $VERBOSE too (a reload never serves, and its trace goes when it ends).
      • AppTrapChain checks covered elsewhere: the runner's own trap chained as the app's handler (that call asks for nothing while the chain runs); no String check (no command string responds to call); restore without its armed check (it only follows arm); a nested call to the chain still calling the runner's handler (which asks for nothing while the chain runs the app's).
  • Older libs, with this head's two test files:

    • 4e526dc4, the head before 8296f411:
      • hot_reload_test: 3/70 fail. The two idiom tests error with SystemStackError, and the no-second-reload test sees 2 reloads.
      • run_command_test: 1/51 fails. The chaining test's second r reads ["H2", "H2"].
    • main 095cbf55:
      • hot_reload_test: 50/70 fail (47 failures, 3 errors). Two errors come from the new API (HotReload.hook_server_start, HotReload::SERVER_START_CAPTURE). The third is a NoMethodError in the unguarded own-server test, which renders a nil block. Among the tests that pass are test_a_ruflet_run_apps_own_usr1_handler_is_replaced_once_it_is_up_as_before, test_ruflet_run_in_a_file_the_script_requires_reloads and test_files_are_required_again_once_the_apps_own_server_stops.
      • run_command_test: 14/51 fail (11 failures, 3 errors). This run uses a shim that only lets new_run_state accept ready_fallback_seconds:. The failures include the fallback tests (main has no READY_FALLBACK_SECONDS), the DSL and no-interceptor boot tests, the two DSL required-file tests and the two USR1 tests.
  • H1 goldens: origin/main 4f64623d (which now includes test: CLI and page compatibility goldens (WP-00 H1) #73, refactor: page prop, Page event and page method registries (WP-00 R4) #74 and refactor: split build_command.rb into cli/build modules (WP-00 R1) #75) is merged into this branch as 87754aa7; against main the branch still changes only its 4 files. On 87754aa7, ruby packages/ruflet/test/compat/compat.rb checked 234 goldens (exit 0), compat_goldens_test.rb passed 242 runs, every packages/ruflet/test file passed, including hot_reload_test (70 runs) and run_command_test (51 runs).

  • Other PRs:

  • End to end: a real ruflet run in a pty, driven by keys, saves, signals and port probes, then Ctrl-C. Trees: v0.0.23, main 095cbf55 and this head.

    app, steps v0.0.23 main this head
    DSL app in main.rb: r, r, a save the first r kills the app, and the session ends (exit 1) each r is queued and the save does nothing; Ctrl-C prints HotReload::Error (exit 1) each r and the save reload; exit 0
    the same app in app.rb, required by main.rb (bootsnap 1.24.6): r, r, a save of app.rb as above (exit 1) as above (exit 1) each reloads; Ctrl-C runs the code after .run, then after the require; exit 0
    DSL app whose handler calls the one it replaced: r 3 times, then an outside USR1 each runs load 1's handler, and nothing reloads; Ctrl-C prints HotReload::Error (exit 1) each r is queued, the USR1 runs the handler, and nothing reloads (exit 1) each runs the latest load's handler once and reloads (loads 2-5); on Ctrl-C the latest handler is back and runs once; exit 0
    own server (stopped after 3 s), then Ruflet.run: two saves the first reload starts a new own server and blocks until Ctrl-C; the second save does nothing as v0.0.23 each save reloads, passing the start without starting it; exit 0
    Ruflet.run, then a direct start (stopped after 3 s): two saves the first reload blocks in a new server's start until Ctrl-C as v0.0.23 each save reloads, ending at that start; exit 0
    Ruflet.run app that loads for 2 s, with r pressed at once the r kills the booting app (exit 1) queued, then reloads once the app is up; exit 0 as main
    the same app, with an outside USR1 at once the USR1 kills the booting app (exit 1) as v0.0.23 applied once the app is up, and it reloads; exit 0
    DSL app that warns from a thread while it serves, then r $VERBOSE is nil and no warning shows; the r kills the app (exit 1) nil, no warning; the r is queued (exit 1) false, and the warning shows; the r reloads; exit 0
    the same with the script's own $VERBOSE = nil nil, no warning; the r kills the app (exit 1) nil, no warning; the r is queued (exit 1) nil, no warning; the r reloads with no reloaded in line; exit 0
    app serving from its own thread: r, then 8 s the r kills it (exit 1) queued; still served and running still served and running after the 5 s fallback

    This head only, each session exiting 0:

    • A DSL app whose handler is installed only on the first load ($trapped ||= Signal.trap("USR1") { ... }). r 3 times and an outside USR1 each run that handler once and reload. On Ctrl-C it is back and runs once.
    • main.rb does Kernel.require_relative "app", or wraps require_relative "app" in a Monitor#synchronize. In both, a save and r do not reload, and the app stays served, as listed under "Still not reloaded". On Ctrl-C, the code after .run and after the require runs.
  • CI: on 15274af0, 11 checks passed and 1 was skipped (publish). On 4adf04ae (main c2d547b9 merged), all 11 passed, and Verify Ruflet Explorer ran compat_goldens_test.rb (242 runs, 0 failures). CI runs again on 87754aa7.

Not run this round, because their inputs are unchanged: Flutter (the engine and template are untouched), a VM rebuild, and the doc harness (none of its examples goes through ruflet run).

Since #57/#58, `ruflet run` queues "r" until the hot-reload harness reports
on RUFLET_READY_FD that its USR1 trap is installed, and the harness only
reported from the Ruflet.run interceptor. An app started with the DSL
(`Ruflet.app { ... }.run` calls Ruflet::Server#start itself) or on a
ruflet_core without Ruflet.with_run_interceptor never reported, so "r"
printed "Reload queued" forever and file changes never reloaded either.

- The harness also captures Ruflet::Server#start (a prepended hook in the
  CLI gem, active only while a hot-reload runner runs, never for the
  runner's own server), so those apps are reloaded and report ready like
  Ruflet.run apps.
- The harness traps USR1 before loading any app code and reports that
  with a "t" byte; "." still means the app is up.
- If an app has reported "t" but not "." within 5 seconds, `ruflet run`
  stops queueing and sends USR1 as before the queue existed. That is safe
  only after "t", so nothing is sent before it; the pipe stays open until
  the app closes it, so a late report never hits a closed pipe.
…lazily

Review follow-ups for the hot reload readiness change.

- Server#start is no longer captured process-wide while a runner is
  active. The capture lives in a fiber-local set only while the runner
  loads the app script, so only a start the script makes synchronously
  (the one that would block that load without the runner) is captured.
  Servers the app starts on its own threads or fibers, in children it
  forks, or after the load are started as on v0.0.23, so an app that
  serves from a background thread while its main script keeps running is
  served again. Within one load only the first start is the app's; a later
  one (never reached without the runner) is logged and ignored instead of
  replacing the served block.
- The harness no longer requires ruflet_server before the app code.
  ServerStartCapture is prepended once Ruflet::Server exists, through a
  TracePoint on class bodies when the app loads it later. A DSL app that
  never requires ruflet_server fails with the same NameError under
  `ruflet run` as under plain ruby, as on v0.0.23.
- "t" (and ".") are only reported when Signal.trap("USR1") actually
  succeeded; otherwise the fd is closed without a report, so `ruflet run`
  never sends USR1 to an untrapped app.
- After "R", the old app's readiness fallback or a late "." can no longer
  mark the session ready before the respawn: mark_restarting sets
  :restarting, only spawn_app_child clears it, and mark_child_ready and
  "r" respect it.
Second review round for the hot reload readiness change.

A Server#start that the app script makes while the runner loads it is no
longer replaced by the runner's own server and turned into a call that
returns at once. On the first load the app's own server object serves the
app from inside the script, as it does without the runner, and the runner
only arms hot reload around it (watcher, USR1 trap, ready report) and
makes that server serve the latest block. So:

- code after `.run` runs once the server stops, as under plain ruby and
  v0.0.23; code there that blocks (joining a thread, a loop) no longer
  keeps the app from being served;
- a Ruflet::Server subclass the app starts keeps its own behaviour;
- a second start later in the script is reached only once the first
  server stops, and then runs as without the runner (it used to be
  ignored with a log line);
- a server the script starts after an intercepted Ruflet.run runs as is,
  as on v0.0.23.

On a reload, a captured start hands the new block over and ends the load
there (throw), where the first load blocks; a script that keeps its server
across loads (`$server ||= ...`) keeps serving it.

The lazy Ruflet::Server hook now reads class names through Module#name and
never raises into the class definition it watches: an app class that
defines its own `name` with arguments made the TracePoint raise into it.

Tests: subclass, blocking code after .run, second start after the first
stops, start after an intercepted Ruflet.run, kept server, lazy hook with
an odd `name` (fresh process), and an app serving from its own thread
under the real harness: served, and "r" reaches only the USR1 trap.
…ess-fallback

main (#72) now guards readiness after "R" with :retired_pid. This branch
had its own :restarting flag for the same race; it now uses main's
:retired_pid in mark_child_ready instead, so the readiness fallback and a
late "." are both refused for the app "R" retired. "r" between the reap
and the respawn is queued, as on main.
A script whose own server the runner served from inside the script, and
which then calls Ruflet.run once that server stops, had that Ruflet.run
intercepted and served by the runner's own server after the load on
v0.0.23. The runner kept the stopped server as the one it serves instead,
so it served nothing and the script ended.

Once the app's own server stops, the runner now forgets it, so the rest
of the script runs as on v0.0.23: a later Ruflet.run is served by the
runner's server, with hot reload; a later Server#start runs as is. A
script that hands nothing else over still ends without the
"never called Ruflet.run" error.
The previous review planted six bugs in the server start capture that no
test caught. Each now fails a test:

- a forked child that captured its own start would arm a watcher and
  report on the ready fd it inherited: the fork test now checks that the
  child stays unarmed and that only the runner reports ("t.");
- a later start replacing the block on a reload: the test with a server
  started after an intercepted Ruflet.run now also reloads;
- dropping the "@server already set" guard on the first load: new test
  with a Ruflet.run on an app thread, then a start on the script's fiber;
- dropping the in-process-bridge guard: new test;
- not restoring the app's $VERBOSE, or leaving the script load set on the
  fiber while the app's own server serves: new test that looks from
  inside that server's accept loop.

Test-only: these all pass on the code as it was.
main.rb requires app.rb, and app.rb ends with `Ruflet.app { ... }.run`
(or starts Ruflet::Server itself, or calls Ruflet.run on a ruflet_core
without run interceptors). The runner serves that app's own server from
inside the script, so the main fiber serves from inside app.rb's require,
and Ruby holds that require's lock until the server stops. A reload loads
main.rb again on the watcher thread; its `require_relative "app"` waited
on that lock forever while holding the reload mutex. The app reported
ready, but no later save or "r" ever reloaded, and nothing was logged.

When the runner serves the app's own server, it now notes the files whose
require that start is nested in (the top-level frames between the start
and the script; "<main>" too, which is how bootsnap's compile cache labels
them). While a reload of such an app runs, and only then, a module
prepended to Object gives `require` and `require_relative` on the
reloading fiber a way past those locks: a require of one of those files
loads it there instead, once per reload, as `require` would. Every other
require, and every require on another fiber or thread, goes to Kernel's
own methods; outside such a reload the module has no methods at all. Once
the app's own server stops, those requires complete and later reloads
require the files as usual.

Apps that start the server in the script itself, and Ruflet.run apps in
any layout (which reloaded on v0.0.23, because the interceptor returns at
once and the require completes), never use any of this.
The tests that wait for the fallback popped a queue without a limit, so a
fallback that never fired (the previous review removed its timer) showed
only as a hung suite until the 60 s or 120 s guard. They now fail after
15 s, naming the fallback. The fallback fires at once in these tests
(ready_fallback_seconds: 0); the limit is only that guard.

The same for the required-file tests' wait for the ready report: an app
that never reports now fails after 30 s instead of the session guard's
120 s timeout.
The PR says that during a reload of an app served from inside a required
file, only the reloading fiber loads the files in flight, and that every
other require goes to Kernel's own methods. Several planted bugs in that
code passed every test. Each of these now fails one:

- a require of a file in flight on another thread loaded it there: new
  test, where a `require` and a `require_relative` on two other threads
  wait on the require lock, as without the runner;
- require_relative resolving against the caller's path instead of its
  real path: in that test, where the app's directory is reached through a
  symlink (macOS temp directories are), the other thread then loaded
  app.rb a second time, alongside the reload;
- a same-named file elsewhere on $LOAD_PATH loaded instead of required:
  new test;
- a file in flight required in another case (macOS file systems) not
  found, so the reload waited forever: new test, skipped on
  case-sensitive file systems;
- require_relative from eval'd code not raising Kernel's LoadError: new
  test;
- require_relative calling a Kernel#require wrapper (the harness sets
  bootsnap's up before it loads the runner) instead of the interpreter's
  own require: new test in a fresh process;
- loading a file in flight returning false instead of true: the
  once-per-reload test now checks [true, false].

The once-per-reload test also stops the server main.rb starts after
app.rb's own server stops. Before, nothing stopped it, and teardown
waited 10 s for the runner thread.

Test-only: these all pass on the code as it was.
On Ruby 3.3, 3.4.0-3.4.9 and 4.0.0-4.0.3 built with Prism, bootsnap
(1.24.5 and later) works around Ruby bug #22023 by compiling each file from
its source with InstructionSequence.compile. The top-level frames of those
files read "<compiled>", not "<top (required)>" or "<main>", so the
runner recorded no file in flight. main.rb requiring an app.rb that ends
with `Ruflet.app { ... }.run` then still hung on its first reload whenever
bootsnap was installed, which `ruflet run` sets up: the reload's
`require_relative "app"` waited on app.rb's require lock forever, exactly
as before 55bb36a. The PR's own bootsnap test failed on Ruby 4.0.0.

"<compiled>" now counts as a top-level frame too. The script's own top
level is also no longer found by its label: it is the bottom-most frame in
the script, so the boundary holds whatever a compile cache labels it, and
a method the script defines that runs above a file it requires is not
taken for it.

Tests: a compile-cache variant that compiles from source (it gives
"<compiled>" on every Ruby, so the gap fails on any Ruby); a start from a
script method called by a required file; and the bootsnap run_command
test accepts either label of bootsnap's cache.
A script that starts its own server unguarded and calls Ruflet.run once
that server stops:

    $own = Ruflet::Server.new(...) { |page| page.add("own") }
    $own.start
    Ruflet.run { |page| ... }

After the own server stops, the runner serves the Ruflet.run block. A
save re-runs the script from the top, so the reload reached `$own.start`
first, took it for the load's entrypoint, and repainted every client with
the stopped server's block instead of the Ruflet.run UI. On v0.0.23 and
main that reload started the own server again on the watcher thread and
never finished.

On the first load that start returned once its server stopped. On a
reload after the runner has let that server go, a captured start before
the load's entrypoint now returns at once, without starting anything, so
the load goes on to the Ruflet.run, as under plain ruby once that server
has stopped. Nothing else changes: while the app's own server serves, a
reload still ends at its start.
While the app's own server serves, $VERBOSE is the app's again, not the
nil the load sets. Once that server stopped, it stayed that way for the
rest of the script (the code after `.run`), which on v0.0.23 and main
runs inside the load with warnings silenced: a constant redefined after
`.run` printed "already initialized constant" under `ruflet run` only on
this branch. The runner now gives the rest of the script back the
$VERBOSE the load had when the start was captured.

Also: a script that sets $VERBOSE itself while it loads (`$VERBOSE = true`
at the top of main.rb) had that setting replaced, while its server
served, by the one from before the load. It now keeps its own, as on
v0.0.23 and main; only the nil the load set is replaced.
…race

Three planted bugs passed every test:

- READY_FALLBACK_SECONDS = 0. Every fallback test passed its own window,
  so nothing pinned the 5 s that bounds the disclosed case where a queued
  "r" reaches a USR1 handler the app installed itself. A test now pins
  the default `ruflet run` uses, and another checks that the fallback
  fires no sooner than its window after "t" (a lower bound, which holds
  on any machine).
- The TracePoint left enabled after `class Ruflet::Server` opens. The
  fresh-process hook test now checks that the trace is on until then and
  off after.
- "t" reported only after the run-interceptor check, so an app on a
  ruflet_core without run interceptors never reported its trap and never
  got the fallback. The trap-before-load test now has a twin on such a
  core.

Test-only: these all pass on the code as it was.
…stopped

The previous commit lets a reload go past starts that come before its
entrypoint once the app's own server has stopped. A start after that
Ruflet.run must still end the reload, as for any Ruflet.run app whose
script starts a server after it: dropping the entrypoint condition made
the reload run the code after that start. This pins it.
The runner silences warnings while it loads the app script ($VERBOSE = nil)
and, when the app's own server serves from inside that load, restored the
value from before the load whenever it found nil. A script's own
`$VERBOSE = nil` was indistinguishable from the load's and was replaced,
so warnings that v0.0.23 and plain ruby keep silent printed under
`ruflet run`.

The first load now traces assignments to $VERBOSE, $-v and $-w until
the app's own server starts serving, and keeps whatever value the load
left when anything assigned it. The trace is removed before the server
serves and when the load ends.
Without the runner, a USR1 handler the app installs runs on every USR1
(on v0.0.23, `r` ran it too). While the app's own server serves from
inside its script, arming hot reload replaced that handler with the
runner's trap, so `r` and any outside USR1 reloaded the app and the
app's handler never ran again, not even once the server had stopped.

AppTrapChain now traps USR1 while that server serves: USR1 asks for a
reload and then calls the handler the app installed, with the signal
number, as Ruby does. A reload, which traps USR1 again from the top of
the script, is chained the same way, also when it fails. Once the
server stops, the app's latest handler is put back unless the app has
trapped USR1 again since. Command-string handlers (IGNORE, DEFAULT,
EXIT, ...) are not run while serving, only put back. Ruflet.run apps
keep the handling they have on v0.0.23 and main.
The $VERBOSE tests name $VERBOSE, $-v and $-w themselves instead of
reading ScriptLoad::VERBOSE_GLOBALS, and a USR1 handler that never runs
fails its test with a message instead of erroring out of Timeout, so on
a tree without these fixes they fail on the behaviour they pin.
A common idiom chains a USR1 handler to the handler it replaced:
`previous = Signal.trap("USR1") { |s| ...; previous.call(s) }`. While the
app's own server serves, a reload traps USR1 again while AppTrapChain is
installed, so that handler's `previous` is the chain itself, which runs
that same handler. Since 4863c40 the two called each other on the next
USR1 until a SystemStackError ended the app, and once the server stopped
every USR1 did.

The chain now does nothing when it is called while it runs the app's
handler, and runs the app's handler only while it is armed: after
restore the app's latest handler is the trap itself. The runner's own
handler, which the first load's handler calls, asks for no second reload
while the chain runs the app's handler, so each USR1 asks for one
reload. The flag is reset in an ensure, so a handler that raises does
not leave the chain silent.

Tests: the idiom over three USR1s and reloads and once the server
stopped; no second reload when the handler calls the replaced handler
after the reload began; a handler that raises; USR1 once the app's own
server stopped and the runner serves the Ruflet.run after it (the chain
is not armed again: pins `@armed = false` in restore); and `ruflet run`'s
own spawn with `r` three times for the idiom.
…andler

assert_r_reloads_and_runs_the_apps_handler ignored its `presses:`
keyword: the loop was hard-coded to two presses and the assertion to two
USR1s, so the chaining-idiom caller that asks for three pressed `r`
twice. The loop now runs `presses` times and expects one USR1 per press.

AppTrapChain#take_over keeps the app's handler when a reload leaves the
chain itself as the trap (`unless previous.equal?(@handler)`). Since the
re-entry guard of 8296f41 nothing failed without that check, yet an app
that installs its USR1 handler only on the first load
(`$trapped ||= Signal.trap("USR1") { ... }`, or a handler from a gem or a
file that is not reloaded) then never runs it again after the first
reload. New test: three USR1s each run that handler once and reload, and
once the server stops a USR1 runs it once. With the check removed it
fails with "the app's handler on USR1 2 never ran".
@AdamMusa
AdamMusa merged commit dd924eb into main Nov 20, 2026
12 checks passed
AdamMusa added a commit that referenced this pull request Nov 20, 2026
#75 split build_command.rb into cli/build/ modules and turned
prepare_flutter_client's steps into registered contributors. The seam is
carried into that layout:

- build_command.rb requires client_bootstrap and includes ClientBootstrap
  next to FlutterSdk, as before.
- The write_client_bootstrap call, which ran right after the extension
  selection was applied and verified, is now the startup_hooks contributor
  (cli/build/contributors/startup_hooks.rb), registered after :extensions,
  so it still runs at that point, before assets and before Flutter.
- build_contributors_test pins startup_hooks in that place in BUILT_IN and
  checks that the contributor passes the build on and always continues.
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.

1 participant