Repository navigation
Closed
Description
Context
Today the process linkage is one-directional: the CLI owns the viewer (ViewerProcess job object on Windows / ContainedProcessGroup on POSIX, crates/fastled-cli/src/viewer.rs:128-151), so killing the CLI tears down the viewer — but closing the viewer window leaves the CLI running forever. After the user closes the window, the watch loop (crates/fastled-cli/src/commands.rs:117-143) and HTTP server keep running until Ctrl+C, which is surprising: the window is the app from the user's perspective.
Where viewers are launched today:
- Normal sketch mode —
commands.rs:83spawns the viewer, then blocks in the recompile watch loop (1 srecv_timeoutticks + space-bar polling). Nothing ever checks whether the viewer process is still alive. --serve-dir <dir>—serve_directory(dir, launch_viewer=true)(commands.rs:323-375) spawns the viewer then waits solely ontokio::signal::ctrl_c()(commands.rs:371).--internal-serve-dir-headless <dir>—serve_directory(dir, false)(lib.rs:67-68): no viewer is spawned; this is the no-UI serve path and must keep its current Ctrl+C-only lifetime.--just-compile— no server, no viewer; unaffected.
Proposal
When the viewer process exits (window closed, or viewer crashed), the CLI should shut down cleanly and exit 0.
- Add a non-blocking liveness probe to
ViewerProcess(e.g.fn is_alive(&self) -> bool):- Windows:
WaitForSingleObject(self.process, 0) == WAIT_TIMEOUT(handle already stored,viewer.rs:128-133). - POSIX: non-blocking wait on the
SpawnedChild(try_wait-style; check whatrunning_processexposes and extend it if needed).
- Windows:
- Normal sketch mode: check
viewer.is_alive()on each watch-loop tick (the existing 1 srecv_timeoutcadence is fine). On exit: stop the file watcher, print a one-line "viewer closed, shutting down" notice, returnExitCode::SUCCESS. --serve-dir: replace the barectrl_c().awaitwith atokio::select!over Ctrl+C and a viewer-exit future (or a 1 s polling task). Same clean shutdown.- Headless serve (
--internal-serve-dir-headless): no change — there is no viewer process to watch (suppression falls out naturally; assert it with a test that the headless path never constructs aViewerProcess).
Corner cases found while researching
- Window closed mid-compile:
run_native_compile_streamingruns synchronously inside the loop, so viewer-exit is only observed between ticks. Do not kill an in-flight compile (risk of corrupting build caches); exit after the compile step returns. Acceptable latency. - Viewer crash vs. user close: indistinguishable at the process level (both are child exit). Treat identically; exit
0either way — the CLI did nothing wrong, and propagating the viewer's exit code would make scripted callers think the build failed. - Other browser tabs attached to the HTTP server: closing the viewer kills the server, breaking any plain-browser tabs the user opened at the printed URL. Accepted: the viewer is the primary UI. If someone wants a durable server,
--serve-dirin a second terminal (or a future--keep-aliveflag, out of scope here) is the answer. - Space-bar rebuild & Ctrl+C must keep working unchanged while the viewer is open; viewer-exit is an additional loop-exit condition, not a replacement.
- Viewer fails to launch: already a hard failure today (
commands.rs:83-89,serve_directoryatcommands.rs:358-365) — no interaction with this change. - Reload/navigation inside the viewer does not exit the viewer process — no false positives expected.
- POSIX
ContainedProcessGroup: ensure the liveness probe doesn't reap the child in a way that breaks the group's kill-on-drop cleanup semantics.
Acceptance criteria
- Closing the viewer window in normal sketch mode terminates
fastledwith exit code 0 within ~2 s (after any in-flight compile completes). - Closing the viewer window in
--serve-dirmode terminatesfastledwith exit code 0. -
--internal-serve-dir-headlessbehavior unchanged (runs until Ctrl+C; no viewer involvement) — covered by a test. - Ctrl+C and space-bar-rebuild still work while the viewer is open.
- Unit tests for the
is_alive()probe (spawn a short-lived dummy process; assert alive→dead transition) on both Windows and POSIX cfgs. -
bash lintandsoldr cargo test --workspacepass.
Decisions
- Priority: P2 — UX polish, no data loss; the current workaround is Ctrl+C in the terminal.
- Exit code on viewer close: 0 (including viewer crash) — see corner cases.
- Mid-compile close: finish the compile, then exit; never kill the build subprocess.
- Detection mechanism: poll
is_alive()at the existing 1 s loop cadence rather than a blocking wait thread — simplest, no new threads, latency is imperceptible. - Implementing agent: fable-5 with xhigh reasoning effort (per maintainer request).
Related issues
- fix: instant launch broken — Tauri viewer loads fastled:// directory protocol, never the HTTP server, shows 'Not Found' forever #151 / fix: Tauri viewer loads the HTTP server URL instead of serving files from disk #157 — prior viewer-wiring work; this issue extends the viewer↔CLI lifecycle in the opposite direction (viewer death → CLI death).
Metadata
Metadata
Assignees
Labels
No labels
Activity
feat(viewer): closing the viewer window terminates the CLI (#168)