Skip to content

feat(viewer): closing the Tauri viewer window should terminate the fastled CLI (except headless serve) #167

Closed

Description

@zackees

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:83 spawns the viewer, then blocks in the recompile watch loop (1 s recv_timeout ticks + 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 on tokio::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.

  1. 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 what running_process exposes and extend it if needed).
  2. Normal sketch mode: check viewer.is_alive() on each watch-loop tick (the existing 1 s recv_timeout cadence is fine). On exit: stop the file watcher, print a one-line "viewer closed, shutting down" notice, return ExitCode::SUCCESS.
  3. --serve-dir: replace the bare ctrl_c().await with a tokio::select! over Ctrl+C and a viewer-exit future (or a 1 s polling task). Same clean shutdown.
  4. 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 a ViewerProcess).

Corner cases found while researching

  • Window closed mid-compile: run_native_compile_streaming runs 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 0 either 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-dir in a second terminal (or a future --keep-alive flag, 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_directory at commands.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 fastled with exit code 0 within ~2 s (after any in-flight compile completes).
  • Closing the viewer window in --serve-dir mode terminates fastled with exit code 0.
  • --internal-serve-dir-headless behavior 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 lint and soldr cargo test --workspace pass.

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

Activity

  1. added a commit that references this issue on Jun 12, 2026
    68ee09a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions