Skip to content

fix(app): wait for the stop before the Mac app's restart, start and remove - #1877

Merged
devswha merged 1 commit into
mainfrom
fix/mac-app-restart-race
Oct 24, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/mac-app-restart-race

Conversation

@devswha

@devswha devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Restart in Ovis for Mac stops the Ovis server and leaves it stopped.

Cause. restart() ran herdr plugin action invoke <id>.stop and then <id>.start. That command only queues the action, so the start ran while the old server was still up and failed with "the server stopped (exit 1) before it answered /api/health", and then the stop finished. Reproduced on the test MacBook Air (eamac) with the app from #1832: herdr's plugin log showed the start failing before the stop finished, and /api/access stayed unanswered. Start, from "Not running", goes through the same path.

Fix.

  • Restart and Start run bun <plugin root>/scripts/plugin.ts stop and then start, waiting for each (60 s each, with LC_CTYPE=UTF-8 as the login item gives herdr), instead of queueing herdr actions.
  • Remove waits for the same stop before herdr plugin uninstall removes the plugin's files.

Checked on eamac (macOS 27.0.1), real home, plugin under the old id, herdr running:

  • Restart: the server pid changed (46160 → 69037), /api/access answered 200 within 3 s, and the window stayed on "Running on this Mac" with the QR code.
  • Start from "Not running": running with the QR code in under 7 s.
  • Remove (then confirm): the server stopped, the plugin and tailscale serve were removed, the login item was deleted, and herdr kept running. Install afterwards brought it back, with the QR code in 21 s.

Reviewed by omo-native-code-reviewer (APPROVE; its LOW points on the locale and the comment are applied, and the same race in Remove is fixed here). The app has no Swift tests; swift build (arm64 and x86_64) passes on eamac and bun run check fast passes.

@coderabbitai

coderabbitai Bot commented Oct 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 31 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 88267d2 and 7282551.

Files selected for processing: 2
  • app/mac/Sources/OvisMac/Model.swift
  • changelog.d/mac-app-restart.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Local bun run check fast at 7282551: workflow syntax, generated files, typecheck and build pass; unit tests passed at the same Swift change before the rebase (ea769382f), and after it only server/shell-agent.test.ts failed with EMFILE: too many open files, watch (this PC was at 126 of 128 inotify instances with other lanes running). This PR changes only app/mac/ Swift and a changelog fragment, so that file runs main's code; CI's Fast checks are the verdict.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass review-work report. Overall: PASSED. This change is Mac app code, not server/src/shared. Under the maintainer's token rule it merges on the code review, the merge pass's own check and CI, without the gate reviewer.

Lane Verdict
Code review (omo-native-code-reviewer, programming + remove-ai-slops) PASS (CLEAR)
Merge pass's own check swift build -c release of app/mac on eamac, in a fresh clone at 7282551 that was deleted afterwards: Build complete. git merge-tree is clean and no other open PR touches these files

What was checked

  • Bounded waits. Restart and Start now wait for plugin.ts stop before start. The shared run helper sends SIGTERM after 60 s and SIGKILL 5 s later. It suspends rather than blocking the main thread. The script's worst cases are about 24 s for stop and 46 s for start.
  • Finding the plugin and bun. The plugin root is found under devswha.ovis and under the pre-rename devswha.herdr-web-ui. bun is looked up in the app's fixed list of folders, so the login item's minimal PATH doesn't matter.
  • herdr itself. It is never stopped. plugin.ts stop kills only the server's recorded process group, and there is no --takeover.
  • Failed stops. A failed or timed-out stop shows an error, and the start doesn't run. Remove still uninstalls, as before.
  • Changelog. The fragment follows changelog.d/README.md. changelog-fold.test.ts passes 12/12.

Follow-ups (LOW)

  • When bun is missing, the stop error comes with an empty log. This predates the PR.
  • Remove doesn't tell the user when the stop failed.
  • The bun plugin.ts call is repeated three times.

All required checks are green and no review thread is open.

CodeRabbit did not review this PR (rate-limited or skipped). The reviews above stand in for it.

@devswha
devswha merged commit 97b1c6d into main Oct 24, 2026
12 of 16 checks passed
@devswha
devswha deleted the fix/mac-app-restart-race branch October 24, 2026 14:28
devswha added a commit that referenced this pull request Oct 28, 2026
…ne step come from, and finish #1455's follow-ups (#2116)

Fixes #1455: the Ovis for Mac follow-ups from #1451's review that were
still on main. The device sweep checked each item against main
([comment](#1455 (comment))).
M1, M2 and part of M4 were already fixed (#1877 and the `work` counter).

**This ships with the next Ovis for Mac build.** Ovis.dmg is built at
release (`mac-dmg.yml`), so installed apps keep the old behaviour until
then. Nothing here touches `server/`, `src/` or `shared/`, and the
remote bundle is unchanged.

## What changes (app/mac)

**Security-ish**
- **M6, the installer download:** it follows only HTTPS redirects to
`ovis.sh` or `herdrweb.dev`, and checks the final URL before running the
script. Today `ovis.sh/install.sh` returns a 302 to
`herdrweb.dev/install.sh`. Before, any redirect was followed and only
the final 200 was checked.
- **M5, the phone address:**
- `/api/access` is read only from a loopback origin, with no redirect
followed.
- `serving_url` becomes the QR code only as `https://<name>.ts.net`, the
only address Tailscale's certificate covers. No user info, query,
fragment or path is allowed.
- The server origin from `plugin.ts status` must be http(s), and Open in
Browser opens only http(s).

**Behaviour**
- **M3, the phone step:** it now runs `scripts/plugin.ts phone` from the
code the server runs now, not the first-installed checkout. Settings →
Updates runs newer releases from the state dir while herdr's checkout
stays put, so the app reads that code from the running processes instead
of copying the plugin's lookup:
- `plugin.ts status`, read when the button is clicked, names the
launcher's pid.
  - The launcher must be this checkout's `server/managed.ts`.
- Its supervisor runs the bridge (`bun server/index.ts`) in the active
release's dir, and the app reads that directory from the bridge process.
This holds even when the launcher fell back to an older supervisor.
- The dir counts only when it is the checkout or a `release-*` dir whose
`scripts/plugin.ts` is its own. Otherwise the checkout's script runs.

The step also checks that the script has the `phone` command, waits up
to 240 s (plugin.ts gives `tailscale serve` 180 s), and checks the exit
status: a failure shows the output instead of a silent refresh.
- **Remove:** it stops and says why when `herdr plugin list`, `stop` or
`plugin uninstall` fails. Try again then goes on from that step. It
turns off `tailscale serve` for the app's port even when the server is
down: it reads `tailscale serve status --json` the way
`server/tailscale.ts` does, and fails when that read fails.
- **Start without a LaunchAgent** (herdr set up from a terminal): herdr
now runs for this login only. The job is bootstrapped from a temp plist
that is deleted right after, so no login item is created. Only Install
creates one.
- **Logs:** `herdr-server.log` moves to `herdr-server.log.1` past 10 MB,
when the app opens or starts herdr. It is copied and emptied in place,
because launchd keeps the file open with O_APPEND. A symlinked log is
rotated where it points.
- **Try again** reruns the action that failed (install, restart, phone
or remove).
- The refresh button gets an accessibility label.
- **`build.sh`:** it refuses a version or build that is not numbers and
dots, which broke its `sed` before. The keychain now reaches `codesign`
as one argument.
- **Tests:** `app/mac/Tests` (`swift test`) adds 14 XCTest cases: the
pure checks, plus a real launcher, supervisor and bridge process tree.
No CI job runs them; the README says how.

## Verification
All runs were on the test MacBook Air (`eamac`, macOS 27.0.1, Xcode 27),
in fresh clones under /tmp that were deleted afterwards. The installed
Ovis.app, herdr and the `sh.ovis.*` LaunchAgents were not touched.

- **Build and tests:** `swift build` OK, `swift test` 14/14. `build.sh`
builds an ad hoc signed app and DMG.
- One test builds a real tree: the checkout's launcher runs a supervisor
that has fallen back, and that supervisor runs the release's bridge. The
app picks the release, and picks nothing for another checkout. Reading
process arguments works even for `/bin/sh` on macOS 27.
- Real trees match it: eamac (an install under the old plugin id) and
this PC both show `bun <root>/server/managed.ts`, then its child
`server/supervisor.ts`, then `bun server/index.ts`. I only read these
processes.
- **The tests can fail:** on main, the new tests do not compile (the
checks are missing). On this branch I broke each check in turn, and each
made its test fail:
  - the installer host, an http phone URL, and a non-ts.net host;
- an HTTP serve port, a failed lookup read as absent, and log rotation
(rename instead of truncate, and a symlinked log);
- the bridge's dir replaced by the supervisor's, any launcher accepted,
any dir accepted, a `plugin.ts` linked out of the release, no pid read,
and an unterminated argument.
- **`build.sh`:**
- `OVIS_MAC_VERSION=1.0/0` and `OVIS_MAC_BUILD=1&2` exit 1 with a
message.
- A `codesign` shim shows `OVIS_KEYCHAIN="/tmp/key chain/k.keychain-db"`
arriving as one argument. main's build.sh split it in two.
- An empty `"$@"` under `set -u` is fine in macOS `/bin/sh` (bash 3.2).
- **launchd probe** with a throwaway label (`dev.fix1455.probe.*`):
- Bootstrap from a temp plist, then delete the plist: the job stays
loaded and running, and nothing is left in `~/Library/LaunchAgents`.
- Copy and truncate the log while the job writes: the next lines start
at offset 0 with no NUL gap.
- `bootout` removed the probe, and `sh.ovis.herdr-server` was in the
same state before and after.
- **`bun run check fast`:** typecheck, build and all other checks pass.
On this busy PC (load 80 to 90), each full run had one timing-bound test
fail in a file this PR does not touch, and a different one each time:
  - `server/codex-processes.test.ts`, the #896 case;
  - `scripts/ci-browser.test.ts`, three cases killed at their 5 s limit;
  - `server/codex.test.ts`, the bounded-time case.

The diff is only `app/mac/` and one changelog fragment. Run on their
own, the failing files pass (codex-processes 3/3; ci-browser 2/2 on this
branch and 2/2 on main), and a full run on main passed. CI runs the full
suite.

## Review
- **omo-native-code-reviewer** on the first round: PASS. Its MEDIUM was
the 120 s phone limit; it is now 240 s. Fixed LOWs: the deleted-release
fallback, a failed lookup in Remove, and the changelog wording.
- **Codex Sol 6.1 xhigh** reviewed four rounds. The final round
(`b7a13dee`) is a **PASS** with no CRITICAL, HIGH or MEDIUM. Earlier
rounds found MEDIUMs, all fixed:
- a failed lookup in Remove, and a failed `serve status` counted as
success;
- the phone step's release choice: copying the plugin's state dir,
settings and hash in Swift kept drifting (`/private` normalization, both
state dir names, `HERDR_PLUGIN_CONFIG_DIR`), so the app now reads the
running bridge instead;
  - a fallback supervisor, and a stale or reused pid.
- CodeRabbit: none requested yet.

Left as known limits (LOW):
- Lines written during the copy and truncate can be lost.
- On a non-Tailscale-hosted control server (Headscale), "Make the phone
address" can repeat with no new feedback.
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