diff --git a/CHANGELOG.md b/CHANGELOG.md index 603d3b1..abb966d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,17 +19,17 @@ What changed in moving it: of, each other's server. - **POSIX stops the whole process tree.** The server starts in its own session and the group is killed. Before, only Windows killed children (`taskkill /T`); - POSIX terminated the one pid, never noticed because the launcher had only run - on Windows. + POSIX terminated the one pid, which went unnoticed because the launcher had + only ever run on Windows. - **A busy dashboard is reused, not duplicated.** The reuse health check waited 1 s; economicspace's dashboard took 4.4 s to answer during a page - load, so a second double-click then booted a second server on the next port - (found verifying this release; the same in launch_ui.py). It waits up to - 15 s once the marker and a live pid agree. + load, so a second double-click then booted a second server on the next port. + Found while verifying this release; launch_ui.py has the same bug. It now + waits up to 15 s once the marker and a live pid agree. - **A failed install shows pip's last lines**, not only the log's path, and a missing app or Streamlit is named in the window. -- **Usage errors under `pythonw` raise a message box**, as argparse's stderr is - None there. +- **Usage errors under `pythonw` show a message box**, since argparse's stderr + is None there. Tests: the boot sequence, port choice, marker rules and the no-tkinter fallback, against a fake server and stand-in widgets; each historical Windows trap is diff --git a/README.md b/README.md index 702d78d..dbe1b28 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![tests](https://github.com/loggger101/quietlaunch/actions/workflows/tests.yml/badge.svg)](https://github.com/loggger101/quietlaunch/actions/workflows/tests.yml) -Start a Streamlit app with **no console window**, and give it something to close. +Start a Streamlit app with no console window, and give it something to close. `streamlit run app.py` in a terminal makes the terminal the app: close it and the server stops, leave it and it sits in the way. Hiding it with `pythonw` is worse: @@ -15,12 +15,12 @@ console with a small control window that - opens the browser once the port actually answers, not when the process starts; - reuses the dashboard it started earlier instead of booting a second copy, and never adopts somebody else's Streamlit that happens to hold the port; -- stops the server **and its children** when it is closed; +- stops the server and its children when it is closed; - installs Streamlit on first use if the interpreter does not have it. -Python 3.9 or later, standard library only. Windows is the reason it exists; -Linux and macOS work too, and CI runs all three (tkinter required for the -window; without it there is a message-box fallback). +Python 3.9 or later, standard library only. Windows is the reason it exists, +but Linux and macOS work too, and CI runs all three. The window needs tkinter; +without it, quietlaunch falls back to a message box. Extracted from [economicspace](https://github.com/loggger101/economicspace)'s dashboard launcher, whose history this repository carries. @@ -84,16 +84,16 @@ and app of the server it started; deleted when that server stops). ## Why it looks the way it does -Each of these was hit for real, and each looks fine from a console. The tests -plant every one and fail on it. +Each of these was hit for real, and each looks fine from a console. Each has a +test that fails when the bug is planted back in. - **`SO_REUSEADDR` is inverted on Windows.** On Unix it means "reuse a port in TIME_WAIT"; on Windows it means "bind even though someone holds it", so a free-port probe that sets it calls a live server's port free. The probe does a - bare `bind`, and asks "does anything answer here?" **first**, since on Windows + bare `bind`, and asks "does anything answer here?" first, since on Windows a server on `0.0.0.0` does not even stop a plain bind on `127.0.0.1`. - **A health check identifies Streamlit, not your Streamlit.** Reuse requires - the marker, a live pid **and** a healthy port: a crashed launcher, a recycled + the marker, a live pid and a healthy port: a crashed launcher, a recycled pid and a stranger's app each fail a different one. The marker is per app, so two apps in one folder do not adopt, or delete the note of, each other's server. - **A busy dashboard answers its health check late.** The app's script runs in diff --git a/quietlaunch/launcher.py b/quietlaunch/launcher.py index 164610a..c9568d0 100644 --- a/quietlaunch/launcher.py +++ b/quietlaunch/launcher.py @@ -13,13 +13,12 @@ the app: closing it stops the server, and it has to stay open and in the way for as long as the dashboard is up. That is the terminal this removes. -Removing it costs more than hiding it, which is the whole design here. A -`pythonw` process with no console has no window to close and no output to read, -so a failed start is a program that silently does not appear, and a successful -one is a server nobody can stop without Task Manager. So this launcher puts up -a small control window instead: it says what is happening while Streamlit boots, -opens the browser once the port actually answers, and stops the server when it -is closed. The window replaces the console rather than merely suppressing it. +Removing the console takes more than hiding it. A `pythonw` process with no +console has no window to close and no output to read, so a failed start is a +program that silently does not appear, and a successful one is a server nobody +can stop without Task Manager. So this launcher puts up a small control window +instead: it says what is happening while Streamlit boots, opens the browser +once the port actually answers, and stops the server when it is closed. IT NEVER IMPORTS THE APP. It spawns `streamlit run ` as a child and watches a socket. That is deliberate: this process is the one thing that must @@ -28,8 +27,7 @@ this process a worker's `__main__`. Standard library only, for the same reason. ASCII ONLY. Nothing here is printed to a redirected stdout, but Windows picks -cp1252 for one, the rule is cheap to keep, and the log file this writes is read -by the same eyes. +cp1252 for one, and the rule is cheap to keep. Extracted from economicspace's `launch_ui.py`, whose history this repository carries; each Windows trap noted below was hit there first. @@ -323,10 +321,10 @@ def _pid_alive(pid): POSIX gets `kill(pid, 0)`. Windows has no such call, so this opens the process with QUERY_LIMITED_INFORMATION and reads its exit code: - STILL_ACTIVE (259) means alive. That constant is genuinely ambiguous in - Win32, since a process may exit WITH code 259, but nothing here exits with - a code at all, and the alternative, a full snapshot walk, costs far more - for the same answer. + STILL_ACTIVE (259) means alive. That constant is ambiguous in Win32, since + a process may exit WITH code 259, but nothing here exits with a code at + all, and the alternative, a full snapshot walk, costs far more for the same + answer. """ if os.name != "nt": try: @@ -384,9 +382,8 @@ def _choose_port(cfg): def _tail(path, lines=14): """Last few lines of a log file for the failure message, or "" if unreadable. - Under `pythonw` there is no console, so this text IS the error report; a - boot failure that could not say why is the whole reason the launcher grew a - window rather than merely hiding the console. + Under `pythonw` there is no console, so this text IS the error report. The + launcher has a window at all so that a failed boot can say why. """ try: with open(path, "r", encoding="utf-8", errors="replace") as f: @@ -691,9 +688,9 @@ def quit(self): `(proc, logfile)` atomic against the hand-over in `_boot_inner`, because closing the window mid-spawn otherwise found `proc` still None, killed nothing, and left a server that nothing on screen could stop. And the - pending `after` is CANCELLED rather than merely left unscheduled: - setting `stopping` stops the pump rescheduling, but the one already in - flight still fires, into a destroyed interpreter. + pending `after` is CANCELLED: setting `stopping` stops the pump + rescheduling, but the one already queued still fires, into a destroyed + interpreter. """ with self.lock: if self.stopping: @@ -768,7 +765,7 @@ def finish(self): def _boot(self): """Worker-thread entry point: run the boot sequence, report any exception. - The bare `except` is the point. This runs on a daemon thread under + It catches every Exception on purpose. This runs on a daemon thread under `pythonw`, where an unhandled traceback goes nowhere at all, so every failure has to come back through `_done_booting` and onto the window. """ @@ -832,7 +829,7 @@ def _boot_inner(self): # And then this process GOES AWAY. It supervises nothing -- proc is # None -- so leaving its window up would put a second "Stop and # quit" button on screen that cannot stop anything, next to the one - # that can. One dashboard, one control window. + # that can. self._done_booting(True, "Already running -- reopening it in your" " browser.", self.url) self._post(self.open_browser) @@ -920,7 +917,7 @@ def launch(cfg): # 2026-10-18 it opened the browser and said "The dashboard is at ..." # whether the server had answered, died at import, or timed out -- # under pythonw, the one message anybody would see, and it was wrong - # exactly when it mattered. + # whenever the boot failed. port, running = _choose_port(cfg) url = "http://localhost:%d" % port if not running: