Skip to content

fix: queue a hot reload requested while the app is still booting - #57

Merged
AdamMusa merged 1 commit into
mainfrom
fix/run-reload-during-boot
Nov 2, 2026
Merged

AdamMusa merged 1 commit into
mainfrom
fix/run-reload-during-boot

Conversation

@AdamMusa

Copy link
Copy Markdown
Owner

Bug

Pressing "r" (hot reload) in ruflet run while the app is still booting kills the app and ends the whole session.

  • The CLI sends USR1 to the app child immediately.
  • The hot-reload harness traps USR1 only in Ruflet::HotReload::Runner#run, after bundler/setup, ruflet_core and the app script have loaded.
  • Before that, USR1 meets Ruby's default action, and the app dies by SIGUSR1. With restart false, the run loop then returns the child's status, and the session ends.

Reproduced on unmodified main with the real harness and an app held mid-boot on a FIFO ($SP/team/qa-logs/dev-wave3/old_reload_boot_repro.rb):

boot progress: "L1"
handle_reload_command('r') => true
after boot: nil (expected "L2")
app exit: signaled=true termsig=30 (USR1) exitstatus=nil
=> run loop returns 1 (restart flag false): the session ends

Fix (option 1: the CLI queues the reload until the app reports ready)

This matches the CLI's design: the CLI owns the dev loop, and the child is the CLI's own harness. Option 2 (trap earlier in the child) cannot close the window, because Ruby's own boot and -rbundler/setup run before any harness code.

  • Ruflet::HotReload::Runner#signal_ready (hot_reload.rb): right after install_manual_reload_trap, the runner writes one byte to the fd named by RUFLET_READY_FD, closes it, and removes the variable. If the variable is absent it does nothing, so other launchers and the existing behaviour are unchanged.
  • CLI (run_command.rb): when manual reload is active (reload on, a TTY, and USR1 exists), each app child is spawned through spawn_app_child. This creates a pipe mapped to fd 3 in the child, and a small thread waits for the ready byte. "r" before ready is queued, and the CLI prints "Reload queued: the app is still starting and will reload once it is up". On the ready byte, a queued reload is delivered as USR1. A mutex covers the check-and-queue and the ready-and-deliver steps, so no request is lost. A respawn ("R") resets readiness and drops a queued reload, because the fresh process loads the current code anyway. Without manual reload (including Windows, which has no USR1) nothing changes.

Tests

  • run_command_test.rb#test_reload_pressed_during_boot_is_applied_once_the_app_is_up: an end-to-end test with the real harness and the CLI's spawn helper. The app script holds its boot on a FIFO gate that the test owns, so "r" always lands mid-boot. Synchronisation is pipe/FIFO-readiness only, with no sleeps: the test waits for L1, presses "r" (asserting it is queued), opens the gate, then expects L2, which is the queued reload actually running, and the app still alive.
    • It fails without the fix. With the old CLI the app dies by USR1 (repro above). With the new CLI but the old harness, the reload never arrives, and the test's 120 s guard fires; the guard covers failures only.
  • hot_reload_test.rb: the runner reports ready on the given fd once the watcher and trap are armed, closes the fd, and clears the variable. Without the variable it does nothing. The first test failed against the old hot_reload.rb.

Proof

  • 30 consecutive passes of the new test at this head (5f38b63), while an engine flutter test loop and the ruflet_core suite loop ran alongside (load average up to 151). Log: reload_boot_proof.log.
  • hot_reload_test.rb under the same load: 12 runs, 0 failures.
  • verify_pr.sh with RUBY_PKGS="ruflet_core ruflet ruflet_server": ruflet_core 145/145 files, ruflet 13/13, ruflet_server 11/11. PASS.

Note: one earlier verify run on this branch failed only on test_handle_reload_command_big_r_requests_full_restart, the known flake that #56 fixes; this branch is based on main, which doesn't have it. With #56 merged in a scratch tree, the whole run_command_test.rb passed 15/15 under the same load. The two PRs merge cleanly.

@AdamMusa
AdamMusa merged commit 2523c47 into main Nov 2, 2026
12 checks passed
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