Reliable shutdown on Windows + lelab --stop#46
Merged
nicolas-rabault merged 3 commits intoJul 6, 2026
Conversation
On Windows, stopping LeLab (Ctrl-C / closing the terminal) often leaves the
uvicorn and Vite child processes alive, which keeps port :8000 — and any open
camera handles — held. The next `lelab` then fails to bind, or a camera won't
open because the previous run never released it.
- `lelab --stop`: find and terminate a running LeLab and its child process tree,
freeing the port.
- Clean process-tree teardown on exit (psutil, with a Windows `taskkill /T`
fallback) so the uvicorn/Vite children and their handles are actually released.
- Pre-flight port checks with an actionable message ("…run `lelab --stop` to free
it") instead of an opaque bind error, plus a readiness wait before opening the
browser.
Tests in tests/test_scripts_lelab.py cover the stop / teardown / port-check paths
with mocked psutil + subprocess (no real processes spawned).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
952e688 to
5131314
Compare
nicolas-rabault
requested changes
Jun 30, 2026
nicolas-rabault
left a comment
Member
There was a problem hiding this comment.
Thank you @nobullryder for this contribution.
Here are some changes requests that could improve it.
- The install_signal_handlers override + os._exit(0) path now only
applies on Windows (os.name == "nt"); macOS/Linux keep uvicorn's
native handlers, and timeout_graceful_shutdown=2 is restored so the
prod server gracefully drains as it did before this PR.
- _monitor_processes no longer coerces a clean child exit (0) to exit
code 1; since it's inside the `returncode is not None` branch it can
raise SystemExit(returncode) directly.
- _fail is annotated -> NoReturn, dropping the two unreachable
`raise AssertionError("unreachable")` lines.
- The dev-mode SIGINT/SIGTERM handler just raises SystemExit(0);
teardown is owned solely by _run_dev's `except BaseException`
(also removed the same double pass from _monitor_processes).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
nicolas-rabault
approved these changes
Jul 6, 2026
nicolas-rabault
left a comment
Member
There was a problem hiding this comment.
Very nice, thank you 🤗
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows, stopping
lelab(Ctrl-C / closing the terminal) often leaves the uvicorn and Vite child processes alive, which keeps port:8000— and any open camera handles — held. The nextlelabthen fails to bind, or a camera won't open because the previous run never released it.What this does
lelab --stop— finds and terminates a running LeLab and its child process tree, freeing the port.taskkill /Tfallback) so the uvicorn/Vite children and their handles are actually released.lelab --stopto free it") instead of an opaque bind error, plus a readiness wait before opening the browser.Tests
tests/test_scripts_lelab.pycovers the stop / teardown / port-check paths with mockedpsutil+subprocess(no real processes spawned).🤖 Generated with Claude Code