-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Studio: Stop every running Unsloth server, not just the last one recorded #7577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danielhanchen
merged 22 commits into
unslothai:main
from
NilayYadav:fix-stop-orphaned-servers
Jul 29, 2026
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e5aec0a
Stop every running Unsloth server, and refuse to start a second on a …
NilayYadav e8e092c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 306915c
Check the fallback range, guard PID reuse, and keep writing studio.pid
NilayYadav a0e4742
Signal each server once when its PID is recorded in more than one file
NilayYadav 2b2861d
Confirm a recorded PID is a Studio server before signalling it
NilayYadav 3fbd60f
Pin PID records to process start time and check every listener on a port
NilayYadav e897c02
Keep every recorded start time per PID and accept in-process Studio s…
NilayYadav 24b53bc
Match the blocking listener address and stop trusting unverifiable PI…
NilayYadav dfcb991
Never delete a PID record that cannot be verified
NilayYadav 279afd1
Detect our own server from our own records instead of a psutil listen…
NilayYadav 7db3ee0
Match a pre-upgrade studio.pid to the blocked port before falling back
NilayYadav bdd0cf3
Never signal PID 0 or 1, and verify a per-port record before trusting it
NilayYadav 07f2f46
Stop unverifiable records instead of skipping them, and record every …
NilayYadav 4d472ed
Drop the command-line guess, fix Windows liveness, and free the PID r…
NilayYadav 5d00689
Merge remote-tracking branch 'origin/main' into pr-7577-merged
shimmyshimmer 02eb535
Studio: harden the per-port PID records against the cases that lose a…
shimmyshimmer a9ad8b7
Merge remote-tracking branch 'origin/main' into pr-7577-merged
shimmyshimmer 52d671f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c854b2d
Studio: let a caller that follows the port keep the fallback, and nev…
shimmyshimmer 287eb0c
Studio: hand over the legacy PID pointer, and fail stop on unreadable…
shimmyshimmer d8f7f2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 79a9785
Merge remote-tracking branch 'origin/main' into r7577
shimmyshimmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0 | ||
|
|
||
| """Per-port PID files, so `unsloth studio stop` can find every server. | ||
|
|
||
| Imports run.py directly, so run under the Unsloth venv. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| _BACKEND = Path(__file__).resolve().parents[1] | ||
| if str(_BACKEND) not in sys.path: | ||
| sys.path.insert(0, str(_BACKEND)) | ||
|
|
||
| import run # noqa: E402 | ||
|
|
||
|
|
||
| @pytest.fixture(autouse = True) | ||
| def isolated_root(tmp_path, monkeypatch): | ||
| monkeypatch.setattr(run, "_studio_root", lambda: tmp_path) | ||
| monkeypatch.setattr(run, "_PID_FILE", tmp_path / "studio.pid") | ||
| monkeypatch.setattr(run, "_OWN_PID_FILE", None) | ||
| yield | ||
|
|
||
|
|
||
| def test_write_pid_file_is_per_port(tmp_path): | ||
| run._write_pid_file(8901) | ||
|
|
||
| path = tmp_path / "studio-8901.pid" | ||
| assert path.read_text(encoding = "utf-8") == str(os.getpid()) | ||
|
|
||
|
|
||
| def test_second_port_does_not_clobber_the_first(tmp_path): | ||
| (tmp_path / "studio-8901.pid").write_text("8550", encoding = "utf-8") | ||
|
|
||
| run._write_pid_file(8902) | ||
|
|
||
| assert (tmp_path / "studio-8901.pid").read_text(encoding = "utf-8") == "8550" | ||
| assert (tmp_path / "studio-8902.pid").read_text(encoding = "utf-8") == str(os.getpid()) | ||
|
|
||
|
|
||
| def test_remove_pid_file_only_removes_our_own(tmp_path): | ||
| run._write_pid_file(8901) | ||
| (tmp_path / "studio-8902.pid").write_text("8600", encoding = "utf-8") | ||
|
|
||
| run._remove_pid_file() | ||
|
|
||
| assert not (tmp_path / "studio-8901.pid").exists() | ||
| assert (tmp_path / "studio-8902.pid").exists() | ||
|
|
||
|
|
||
| def test_remove_pid_file_leaves_a_reused_entry_alone(tmp_path): | ||
| run._write_pid_file(8901) | ||
| (tmp_path / "studio-8901.pid").write_text("999999", encoding = "utf-8") | ||
|
|
||
| run._remove_pid_file() | ||
|
|
||
| assert (tmp_path / "studio-8901.pid").read_text(encoding = "utf-8") == "999999" | ||
|
|
||
|
|
||
| def test_recorded_studio_pids_reads_per_port_and_legacy_files(tmp_path): | ||
| (tmp_path / "studio-8901.pid").write_text("8550", encoding = "utf-8") | ||
| (tmp_path / "studio-8902.pid").write_text("8600", encoding = "utf-8") | ||
| (tmp_path / "studio.pid").write_text("4242", encoding = "utf-8") | ||
|
|
||
| assert run._recorded_studio_pids() == {8550, 8600, 4242} | ||
|
|
||
|
|
||
| def test_recorded_studio_pids_ignores_corrupt_files(tmp_path): | ||
| (tmp_path / "studio-8901.pid").write_text("not-a-pid", encoding = "utf-8") | ||
|
|
||
| assert run._recorded_studio_pids() == set() | ||
|
|
||
|
|
||
| def test_own_studio_blocking_the_port_is_recognised(tmp_path): | ||
| (tmp_path / "studio-8901.pid").write_text("8550", encoding = "utf-8") | ||
|
|
||
| assert run._blocker_is_own_studio((8550, "python")) is True | ||
|
|
||
|
|
||
| def test_a_foreign_blocker_still_falls_back(tmp_path): | ||
| # jupyter-lab on 8888 must keep the fallback, not abort the launch. | ||
| (tmp_path / "studio-8901.pid").write_text("8550", encoding = "utf-8") | ||
|
|
||
| assert run._blocker_is_own_studio((117, "jupyter-lab")) is False | ||
| assert run._blocker_is_own_studio(None) is False |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.