Skip to content

Studio desktop: leave the startup screen on server-port instead of at the next 500 ms poll - #15673

Merged
danielhanchen merged 3 commits into
mainfrom
perf/desktop-port-wake
Oct 28, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
perf/desktop-port-wake

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

When the desktop app starts its managed backend, the frontend waits for the validated port in waitForManagedServerPort, which checked portRef and then slept a fixed 500 ms. The server-port listener set the port at once, but the startup screen only switched to the app at the next poll tick, 0 to 500 ms later. This PR lets the server-port listener wake that wait, so the switch happens as soon as the port is known.

What changes

  • studio/frontend/src/hooks/managed-server-port.ts (new): the startup loop moves here unchanged, plus createPortWakeSignal(), whose wait(ms) sleeps like before but can be cut short by notify().
  • studio/frontend/src/hooks/use-tauri-backend.ts: the hook passes the signal's wait to the loop and calls notify() in the server-port listener, right after setApiBase(port).
  • The 500 ms poll stays as the fallback, so a missed event still cannot hang startup.
  • The calls and their order are unchanged: setApiBase(port) in the listener, then setApiBase and setRunningStatus in startManagedServer. A notify() with nobody waiting is a no-op, and an aborted start still returns aborted. server-crashed and server-start-timeout are not wired to the wake: their listeners already set the error and clear startingRef, so waking the loop early would change nothing on screen.

Results

Linux x86_64, release desktop build under Xvfb with a real managed backend, interleaved launches. Time from the server-port emit to the UI's running status: 358.9 ms [97..467] on main vs 4.7 ms [3..11] here (n=6 each), and 170.4 ms [10..422] vs 2.7 ms [2..5] in a second interleaved set (n=5 each). Every run of both arms showed the same state sequence (Checking..., Starting Unsloth..., Loading models..., Nearly done..., then the app).

Tests

  • studio/frontend/tests/managed-server-port-wake.test.ts (new, 5 tests): server-port ends the wait well under the poll interval; without a notify the wait still polls; a known port skips the wait and an aborted start reports aborted; a notify with nobody waiting does not cut short a later wait; the hook calls notify from its server-port listener. It fails on main (no module) and passes here.
  • The 10 frontend test files that cover use-tauri-backend.ts (74 tests) pass here; on main the 9 existing files pass (69 tests).
  • node scripts/typecheck.mjs --tests is clean. eslint on use-tauri-backend.ts shows the same 2 errors as main (TauriInvoke unused, set-state-in-effect).

Not covered

  • Windows (WebView2) and macOS (WKWebView) were not run; the change is a JavaScript promise with no platform branch.

@danielhanchen
danielhanchen marked this pull request as ready for review October 28, 2026 09:20
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T09:21:28.936395Z ad8bccb Manual request
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: ad8bccb8ae

ℹ️ 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".

wasimysaid added a commit to wasimysaid/unsloth that referenced this pull request Oct 28, 2026
@danielhanchen
danielhanchen merged commit 31665e7 into main Oct 28, 2026
4 of 39 checks passed
@danielhanchen
danielhanchen deleted the perf/desktop-port-wake branch October 28, 2026 10:56
danielhanchen added a commit that referenced this pull request Oct 28, 2026
#15745)

## Summary

`Repo tests (CPU, rest)` fails on main with three guard failures. It
shows on every staging CI run of recent PRs (#15613, #15615, #15673 and
others), whatever the PR changes:

```
tests/test_rendezvous_port_everywhere.py::test_distributed_tests_take_their_port_from_the_shared_helper
    tests/test_gc_skip_auto_dist_cells.py: _free_port does not use tests._rendezvous_port
    tests/test_gc_skip_auto_dist_gloo.py: _free_port does not use tests._rendezvous_port
tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[5]
    tests/test_mamba_fused_lora_adapters.py:464: read_text()
tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[13]
    tests/test_refresh_moved_layer_devices.py:81: read_text()
```

These tests landed around the same time as the guards that now catch
them:
- 570b9b8 added the two `gc_skip_auto_dist` gloo tests with their own
`bind(("127.0.0.1", 0))` port picker. That is the ephemeral-range race
that #15679 moved every distributed test off, which produces
`EADDRINUSE`.
- 986558e and 7f3c5a1 read checked-in source with the platform
default encoding, which breaks on Windows once the file gains a
non-ASCII byte.

The guards are right in all three cases, so the tests change, not the
guards.

## Change

- `tests/test_gc_skip_auto_dist_cells.py`,
`tests/test_gc_skip_auto_dist_gloo.py`: `_free_port()` delegates to
`tests._rendezvous_port.free_port()`, the same three lines every other
distributed test uses. The now-unused `import socket` is dropped.
- `tests/test_mamba_fused_lora_adapters.py:464`,
`tests/test_refresh_moved_layer_devices.py:81`: `read_text(encoding =
"utf-8")`.

## Testing

- `tests/test_source_read_encoding.py` +
`tests/test_rendezvous_port_everywhere.py`: 3 failed on main, 22 passed
with this change.
- The four touched test files under `-n 4`: 43 passed, 4 skipped.
danielhanchen added a commit that referenced this pull request Oct 28, 2026
…ashing it (#15763)

## Summary

The macOS `Chat UI, API, Update and GGUF inference` job fails on
unrelated PRs (seen on #15696 and #15673 today) after 50 to 75 minutes
in Studio setup:

```
[node-prebuilt] downloading https://nodejs.org/dist/v24.22.1/node-v24.22.1-darwin-arm64.tar.gz
Downloading node-v24.22.1-darwin-arm64.tar.gz:  12.1% (6.1 MiB/50.6 MiB) at 1.6 KiB/s
[node-prebuilt] node-v24.22.1-darwin-arm64.tar.gz checksum mismatch 1/2: expected=89c6... actual=8014...
...
[node-prebuilt] prebuilt unavailable: node-v24.22.1-darwin-arm64.tar.gz checksum mismatch after retry
error          studio setup failed (exit code 1)
```

The archive was never tampered with: it was 6.1 MiB of a 50.6 MiB body.
Two installer bugs turn a flaky connection into that:

1. **Truncated bodies are accepted.**
`http.client.HTTPResponse.read(amt)` returns `b""` when the server
closes before `Content-Length` bytes arrive, without raising
`IncompleteRead` (CPython leaves this as a known compatibility
trade-off). The copy loops in `install_node_prebuilt.download_file`,
`prebuilt_core.download_file` and `prebuilt_core.download_bytes` stop at
the first empty read, so a short file is written out and hashed. For
Node that surfaces as a misleading checksum mismatch; for a llama.cpp
asset with no published digest the short archive goes straight to the
extractor.
2. **A trickle never times out.** The 120 s socket timeout is per read,
so a connection that keeps delivering a few bytes a second holds an
attempt open indefinitely (1.6 KiB/s above, over 30 minutes per
attempt).

## Change

- `prebuilt_core.copy_response_body`: one body-copy helper used by all
three loops.
- Raises `TruncatedDownload` (a subclass of
`http.client.IncompleteRead`) when the body ends short of
`Content-Length`.
- Raises `StalledDownload` (a `TimeoutError`) when a 60 s window brings
under 256 KiB, about 4 KiB/s, and the body is not yet complete.
- Reads a real `http.client.HTTPResponse` with `read1`, which returns
what one receive brings. `read(n)` waits for all n bytes, so on a
trickle it would block for hours and the window check would never run.
- Both exceptions are already classified as retryable by
`is_retryable_url_error` in both modules, so the existing retry loops
reconnect instead of hashing a partial file or waiting on a dead
connection. Bodies without `Content-Length` (chunked) keep their current
behaviour; http.client already raises on a cut chunked body.

## Testing

- New `tests/studio/install/test_download_truncation_and_stall.py` (11
tests) runs against a real local HTTP server, because the `read(amt)`
EOF behaviour is what is under test and a `BytesIO` double cannot
reproduce it:
  - a body closed at 25% raises a retryable `TruncatedDownload`;
- `download_file` (Node and llama.cpp paths) and `download_bytes`
reconnect and finish with the full payload;
- a server that truncates every attempt is refused with no partial file
left behind;
- a trickling body is abandoned, a slow but moving one is not, and a
window closing on the final byte is not a stall.
- a real server sending the full `Content-Length` then one byte every 50
ms is abandoned within the (shortened) window, and `download_file`
reconnects past it on both paths.
- Sabotage: with only the truncation check removed, 5 tests fail; with
only the stall check removed, the trickle test fails; with plain `read`
instead of `read1`, the three real-trickle tests fail (the call blocks
until the server gives up).
- `tests/studio/install` base vs head on this host: identical failing
test IDs (21, all pre-existing on main here), 10274 passed at base and
10282 with this change (the 8 new tests).
- Real download through the new path: `node-v24.22.1-linux-x64.tar.gz`
(55.6 MiB) from nodejs.org verified against the pinned sha256 at 378
MiB/s, so the smaller reads cost nothing.
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