Skip to content

Studio: optional launchd agent to keep Studio running on macOS CLI installs - #17879

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
Tofuwang45:17877-studio-macos-launchd-agent
Nov 16, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
Tofuwang45:17877-studio-macos-launchd-agent

Conversation

@Tofuwang45

@Tofuwang45 Tofuwang45 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes #17877

Problem

On macOS, Studio installed with install.sh stops when the terminal closes, so its scheduled tasks stop too. Linux has an opt-in systemd user service (#9308) and the desktop app has "Run at login", but macOS CLI installs had no way to keep Studio running.

Change

Opt-in only: without UNSLOTH_INSTALL_LAUNCHD=1, nothing changes. With it, install.sh writes and loads ~/Library/LaunchAgents/ai.unsloth.studio.server.plist, mirroring the systemd service file for file:

systemd (#9308) launchd (this PR)
studio/systemd/install_user_service.sh studio/launchd/install_user_agent.sh
unsloth-studio.service.in ai.unsloth.studio.server.plist.in
UNSLOTH_INSTALL_SYSTEMD / _HOST / _PORT UNSLOTH_INSTALL_LAUNCHD / _HOST / _PORT
Restart=on-failure KeepAlive → SuccessfulExit = false
_remove_systemd_user_service _remove_launchd_user_agent

Behaviour:

  • Binds 127.0.0.1:8888 by default. UNSLOTH_LAUNCHD_HOST=0.0.0.0 opts into LAN.
  • Restarts only after a crash. unsloth studio stop sends SIGTERM, Studio exits 0, and launchd leaves it stopped.
  • Skipped with a note when the desktop app's ~/Library/LaunchAgents/Unsloth.plist exists, so two servers don't fight over the port.
  • Never overwrites a plist it didn't write (marker comment on line 2).
  • Clear error when there's no GUI session (SSH only, nobody logged in at the Mac).
  • scripts/uninstall.sh boots the agent out before the kill sweep, so launchd can't restart Studio mid-uninstall.
  • launchd's PATH is only /usr/bin:/bin:/usr/sbin:/sbin, so the agent sets one that includes the venv, ~/.local/bin and Homebrew.
  • stdout/stderr go to /dev/null. Studio already tees both streams to logs/server/ (0600, newest 20 kept). A launchd log file would never rotate, would be world-readable by default, and would contain the startup banner's API key. Startup errors from before that tee starts can be seen by running the command in a terminal; the installer prints that command.
  • The helper is bash 3.2 compatible (macOS /bin/bash). XML escaping uses sed, because ${v//&/&} behaves differently in bash 3.2 and 5.2.

Also: packaged via "launchd/*" in pyproject.toml, added to the cross-platform parity CI path filters and test list, and registered in tests/test_installer_interactive_prompts.py so the helper can never add a prompt.

Testing

  • tests/test_installer_launchd.py: 40 tests using stand-in launchctl / uname / id, so they run on the Linux CI runners as well as macOS (they skip on Windows). Installer and related suites locally: 215 passed, 56 skipped (the systemd tests skip on macOS); the launchd tests also pass with sh resolved to dash, as on Ubuntu.
  • pre-commit, bash -n / sh -n, plutil -lint on a generated plist.
  • On a real Mac (macOS, Apple Silicon), UNSLOTH_INSTALL_LAUNCHD=1 UNSLOTH_LAUNCHD_PORT=8899 ./install.sh --local --no-torch:
Check Result
After install state = running, HTTP 200; ~340 MB RSS, same as a manual run
kill -9 restarted by launchd; new PID owns the port, no orphans
unsloth studio stop last exit code = 0, stays stopped
launchctl kickstart back up in 3s
Log out, log back in Studio started by launchd 7s after Finder, no terminal open
Uninstall's agent step unloaded, plist removed, no process left

Known behaviour, same as systemd: if another Studio already holds the port, the agent's copy exits 1 and launchd retries every 10s until the port frees up.

Failure paths (second commit, from review):

  • Uninstall falls back to launchctl remove <label> if bootout fails, so KeepAlive can't respawn Studio after the kill sweep. Checked against real launchd.
  • The install helper waits up to 130s for an old agent to unload, past the plist's 120s ExitTimeOut.
  • If enable or bootstrap fails, the helper removes the new plist, so a failed install can't start a second server at next login.

Out of scope

Windows Task Scheduler and the scheduled-tasks UI redesign from the ideas list.

🤖 Generated with Claude Code

…stalls

On macOS, Studio installed with install.sh stops when the terminal closes,
so its scheduled tasks stop too. Linux has an opt-in systemd user service
(unslothai#9308) and the desktop app has "Run at login"; macOS CLI installs had
nothing.

UNSLOTH_INSTALL_LAUNCHD=1 now writes and loads
~/Library/LaunchAgents/ai.unsloth.studio.server.plist, mirroring the
systemd service:

- Binds 127.0.0.1:8888 by default; UNSLOTH_LAUNCHD_HOST/PORT override.
- KeepAlive SuccessfulExit=false: restarts after a crash, while
  `unsloth studio stop` (exit 0) stays stopped.
- Skipped when the desktop app's Unsloth.plist agent exists.
- Never overwrites a plist it did not write (marker on line 2).
- Clear error when there is no GUI session (SSH only).
- uninstall.sh boots it out before the kill sweep.
- stdout/stderr go to /dev/null: Studio already tees both to
  logs/server (0600, newest 20 kept), and a launchd log would be an
  unrotated, world-readable copy holding the startup API key.

Closes unslothai#17877

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 15, 2026 22:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

- uninstall.sh: when `launchctl bootout` fails, fall back to
  `launchctl remove <label>`. A job left loaded would be respawned by
  KeepAlive after the kill sweep, mid-uninstall.
- install_user_agent.sh: wait up to 130s for the old agent to unload,
  past the plist's ExitTimeOut (120s), so a Studio finishing a slow
  shutdown is not reported as a failed install.
- install_user_agent.sh: if enable or bootstrap fails, remove the new
  plist, so a failed install does not start a second server at next
  login beside the one install.sh falls back to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T09:24:59.658056Z 3b113df 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 added a commit to Imagineer99/unsloth that referenced this pull request Nov 16, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 16, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 16, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@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: efd426edbe

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

_path="$_exe_dir:$HOME/.local/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

# launchd scans the login account's home at login, not a redirected $HOME.
_account_home="$(dscl . -read "/Users/$(id -un)" NFSHomeDirectory 2>/dev/null | awk '{print $2}' || true)"

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 Preserve spaces when reading the account home directory

When HOME is redirected and the login account's home contains spaces, such as /Volumes/External SSD/alice, the new $2 extraction cannot preserve the complete NFSHomeDirectory. It truncates the path or triggers the fallback to the redirected home, placing the plist outside the account's LaunchAgents directory. A successful initial load therefore does not provide the documented --enable behavior (“so it starts at login”). Preserve the complete attribute value here and in the matching installer/uninstaller lookups.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not changing: a home with spaces makes dscl print the value on its own line, so the parsed field is not an absolute path and the helper falls back to $HOME, the pre-redirect behaviour (same parsing install.sh already uses for _default_home). It only matters with a redirected HOME and a spaced account home.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@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. Another round soon, please!

Reviewed commit: 3b113df661

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Studio: optional launchd agent to keep Studio running on macOS CLI installs

3 participants