Skip to content

Studio: skip the vLLM / SGLang port ownership scan until the engine is listening - #19242

Merged
danielhanchen merged 5 commits into
mainfrom
perf/r40-managed-engine-poll
Nov 24, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
perf/r40-managed-engine-poll

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

While a vLLM or SGLang model loads, the Studio readiness loop in ManagedEngine._start_once polls every 0.25 s. Since #19199 every poll first runs LlamaCppBackend._health_listener_is_ours, which walks the child process tree with psutil, reads each child's sockets and then scans every inet socket on the host. Until the engine binds its port nothing is listening, so each of those scans is wasted, and on Linux and macOS each one also logs

llama-server port N answered /health but our child PID is not listening on it; refusing to use it

at error level, for the whole 1 to 5 minute load. Every other caller of the check runs it once, after /health has already answered.

Change

Before the ownership check, the poll now makes a plain TCP connection to the same host and port the /health probe uses (parsed from base_url, so an IPv6 [::1] URL connects to ::1) and sends nothing.

  • Refused or unreachable: wait 0.25 s and loop, as before. The old path also waited here: the check returned False on Linux and macOS, and on Windows it returned True and the probe then failed to connect to the same address.
  • Accepted: the unchanged ownership check runs, then the unchanged keyed /health probe. Both still run on every iteration, and nothing is cached.

Timeouts, stall detection, cancellation, the exited-child path and the Windows ownership rules are untouched. The connect timeout is 2 s, the same as the probe client's.

Results

A fake engine is launched through the real ManagedEngine.start path, with the same Popen flags and stdin pipe. It spawns 4 idle workers, prints progress lines for 30 s, then binds 127.0.0.1 and serves /health. There were 3 interleaved runs per arm on the same Linux host. The spread across each arm's 3 runs is the A/A noise.

Metric (median of 3, range) Base Head
Ownership checks per load 38 (36 to 39) 1 (1 to 1)
False "not listening" error lines 37 (35 to 38) 0
CPU in the ownership check 20.09 s (20.08 to 20.35) 0.38 s (0.34 to 0.42)
Studio process CPU during the load 20.18 s (20.17 to 20.43) 0.49 s (0.47 to 0.55)
Keyed /health probes 1 1
Ready after engine bind 0.42 s (0.40 to 0.50) 0.45 s (0.41 to 0.48)
Total load time 30.53 s 30.56 s

This host has many sockets, so one scan costs about 0.5 s of CPU, which is more than a typical desktop. The call count and the error lines do not depend on the host.

Tests

New studio/backend/tests/test_managed_engine_readiness_poll.py:

  • With nothing listening there is no ownership scan, no error and no probe, and the load still times out the same way (fails on main: one scan plus one false error inside the window).
  • Once something listens, the ownership check runs on every poll before the keyed probe: a refused first answer is asked again and the key goes out only after a yes. Repeated non-200 probes are each preceded by a fresh check, and the load still times out.
  • A listener that is not owned by our child is still refused with the same error.
  • With a listener whose owner psutil cannot name, mocked Windows still probes it and Linux/macOS still refuse.
  • _port_accepts sends no bytes, returns False once the listener is gone, and follows an IPv6 [::1] URL.

The 5 behaviour tests pass on main and on this branch. The other 3 fail on main as expected.

The related suites (managed engine, engine ports, API key transport, llama-server port owner, sidecar port owner, health wait, STT sidecars, IPv6 loopback hold, WSL host, NPU failure output) were run on main and on this branch, and the failing test IDs were compared. No test fails only on this branch:

  • In the parallel run, two timing tests failed only here: test_a_load_that_stalls_times_out_one_timeout_after_its_last_work and test_runner_stops_the_engine_group_when_studio_closes_the_pipe. Both pass on a serial rerun here, and the WSL runner test then failed on main.
  • test_unload_waits_for_inflight_transcription is timing sensitive on both arms.
  • Four ROCm and support-probe tests fail on main and here alike.

The vLLM / SGLang readiness poll ran the full psutil ownership scan on every
0.25 s tick before the engine had bound its port, costing CPU and logging a
false 'not listening' error each time. Connect first (sending nothing); only
once something accepts does the unchanged ownership check and keyed /health
probe run.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-24T15:08:08.481659Z
We couldn’t complete this review. Comment @codex review to try again.
4dd20a0 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: b6d325a815

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 24, 2026
@danielhanchen danielhanchen self-assigned this Nov 24, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 51d9c8e881

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen marked this pull request as ready for review November 24, 2026 15:07
@danielhanchen
danielhanchen merged commit 2400b2e into main Nov 24, 2026
10 of 51 checks passed
@danielhanchen
danielhanchen deleted the perf/r40-managed-engine-poll branch November 24, 2026 15:08
danielhanchen added a commit that referenced this pull request Nov 24, 2026
#19296)

## Problem

`studio/backend/tests/test_managed_engine_readiness_poll.py` fails on
main (6 of 9 tests) and on every PR based on it, for example #19284 and
#19285, which do not touch Studio:

```
>   child_env["WSL_UTF8"] = "1"
E   TypeError: 'NoneType' object does not support item assignment
```

## Cause

Two PRs merged within an hour of each other, each green on its own base:

- #19167 added `vllm` to `MANAGED_ENGINE_LOG_FAMILIES`, so a vLLM start
now captures logs and, on the WSL path, sets `child_env["WSL_UTF8"] =
"1"` on the environment `_wsl_command` returns (the SGLang half of this
came in #19166).
- #19242 added this test file. Its `_wsl_command` stub returned
`(command, None)`, and its base predated #19167, so vLLM did not capture
logs there and the `None` was never touched.

The code is right: the real `_wsl_command` always returns
`wsl_host.guest_command(...)`, whose second element is the `wsl.exe`
environment dict, and `test_managed_engine_logs.py` already stubs it
with `dict(os.environ)`. The stub was the part that did not match the
contract.

## Fix

- The stub returns `dict(os.environ)`, the same shape the real launcher
hands back.
- The fixture sets `UNSLOTH_STUDIO_HOME` to `tmp_path`, since the vLLM
start now opens a per-attempt log under the Studio home and the tests
should not write into the real one.

No product code changes.

## Verification

- `tests/test_managed_engine_readiness_poll.py`: 9 passed (6 failed on
main before the change).
- Reverting only the stub's return value back to `None` brings back the
same 6 failures.
- `test_managed_engine_logs.py`, `test_engine_ports.py` and this file
together: 50 passed; this file under `-n 4`, 3 repeats: 27 passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant