Skip to content

Studio: let the desktop app stop a server it attached to - #16581

Closed
NilayYadav wants to merge 6 commits into
unslothai:mainfrom
NilayYadav:desktop-stop-external-server
Closed

NilayYadav wants to merge 6 commits into
unslothai:mainfrom
NilayYadav:desktop-stop-external-server

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

When the desktop app finds an Unsloth server already running for the same install (for example one started with unsloth studio in a terminal), it attaches to it and disables Update with a note to stop that server first. But Stop server from the menu bar only detached: the server kept running, Update stayed disabled, and quitting and reopening attached to the same server again. There was no way out from the app.

Stop server now shuts down the attached server through the same authenticated /api/shutdown request already used for desktop-owned servers, then waits for the port to close. Only a server that accepts this install's desktop login can be stopped this way, and nothing is killed by PID. After the stop the app leaves attached mode, so Start server launches its own server and Update is enabled again. If the server does not stop, the app stays attached and returns an error.

Tested on macOS with the real desktop app:

  • Before (v0.1.913-beta release): with a terminal server on 8888, Update was greyed out; Stop server left the server running (/api/health 200); reopening attached to it again.
  • After (this branch, version set to 0.1.913-beta): Stop server shut the terminal server down cleanly, Start server spawned an app-owned server, and the 0.1.914-beta banner showed Update enabled.

Fixes #16561

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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-11-04T07:30:32.380265Z 0e50357 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 044d94da1f

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

markServerStopIntent();
try {
await invoke("stop_server");
await invoke("stop_server", { port: portRef.current });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Display errors from attached-server shutdowns

If an attached server refuses the authenticated shutdown or takes longer than the shutdown timeout, this invocation rejects, but runStopServer() only clears the stop marker and rethrows. The tray-toggle-server listener discards the stopServer() promise, so the error from stop_attached_backend() is never displayed. The app remains running and attached with Update disabled, making Stop server appear to do nothing and hiding the instruction to stop the server from its terminal. Catch and display the failure, such as through a toast, while retaining attached mode.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ed5cfe6: the tray stop now shows a toast with the reason when the stop fails, and stays attached.

@NilayYadav NilayYadav self-assigned this Nov 3, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex security 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: ed5cfe6da4

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

Copy link
Copy Markdown
Member

The two frontend reds here (Frontend unit tests (Windows) and Frontend build + bundle sanity) are from tests/tray-server-status.test.ts, not from this change. That test cut the tray listener at the first });, which is now the end of the new .catch, so it stopped reading the stopped / error branch. #16600 makes the test end the slice at the register call's own close. Once that lands, rebasing or rerunning these two jobs should turn them green with no change needed here.

danielhanchen added a commit that referenced this pull request Nov 3, 2026
## Summary

`studio/frontend/tests/tray-server-status.test.ts` misreads the tray
listener whenever the listener contains a nested `});`. It fails #16581,
which only adds a `.catch` to the tray stop. That failure breaks both
`Frontend unit tests (Windows)` and `Frontend build + bundle sanity`:

```
not ok - the tray offers a click exactly when the listener would act on it
  a tray toggle is clickable for a status the renderer silently drops, or greyed for one it would have handled
  actual (main.rs enables):        ['error', 'running', 'stopped']
  expected (listener acts on):     ['running']
```

The listener in #16581 still handles `running`, `stopped` and `error`,
exactly as on main:

```ts
register<void>("tray-toggle-server", () => {
  if (statusRef.current === "running") {
    stopServer().catch((e) => {
      toast.error("Could not stop the server", { description: String(e) });
    });
  } else if (
    statusRef.current === "stopped" ||
    statusRef.current === "error"
  ) {
```

`actionableStatuses` cut the listener at the first `});` after
`register<void>("tray-toggle-server"`. With the `.catch` in place, that
is the end of the `.catch` call, so the `stopped` / `error` branch was
never read. The guard itself is useful: the tray item in `main.rs` and
the renderer's listener must agree on the statuses. Only the slice was
fragile, and the PR is fine.

## Change

`studio/frontend/tests/tray-server-status.test.ts`, `actionableStatuses`
only: the slice now ends at the `});` on the `register` call's own
indentation, which is the same indent-matching approach `hookFunction`
in this file already uses. If the call stops starting its own line, the
test throws a clear error instead of silently reading too little.

## Testing

`node --test tests/tray-server-status.test.ts`:

| hook | old test | this branch |
|---|---|---|
| main's `use-tauri-backend.ts` | 6 pass | 6 pass |
| #16581's `use-tauri-backend.ts` | 5 pass, 1 fail (above) | 6 pass |
| #16581's hook with its `error` check pointed at a status that does not
exist | n/a | 5 pass, 1 fail |

The last row shows the guard still catches a listener that drops a
status the tray enables.
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 8276121058

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

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 0e50357190

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

Lyxot pushed a commit that referenced this pull request Nov 4, 2026
…s port (#16595)

Fixes #16561. Includes NilayYadav's attached-server stop from #16581.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 4, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member

Thank you for the PR! This change landed on main through #16595 (commit f10c0ca), which includes this attached-server stop as-is: stop_attached_backend, the port argument to stop_server, and the tray error toast. It also waits up to 130 s for an accepted shutdown so a training run can finish saving. With main merged in, this branch has no remaining diff, so I am closing it as superseded. Fixes #16561 via #16595.

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.

[Bug] Unable to update to latest beta

3 participants