happier icon indicating copy to clipboard operation
happier copied to clipboard

macOS system-mode relay install always fails and leaves an unbootable daemon: payload symlinked from /tmp + deleted by cleanup, one-shot health probe, AUTO_MIGRATE=0 on darwin

Open lowtek7 opened this issue 5 months ago • 2 comments

Summary

relay host install --mode system on macOS (arm64) always fails and leaves a permanently broken daemon. Three compounding issues:

  1. The installed payload is a symlink into the installer's /tmp scratch dir, which the installer deletes on exit
  2. The post-install health probe is a single-shot TCP connect with no retry, so a cold-starting server always loses the race
  3. HAPPIER_SQLITE_AUTO_MIGRATE=0 is rendered on darwin+Bun, so even a running server serves an unmigrated database

Repro'd on macOS 15+ (Apple Silicon, Mac mini), hprev 0.2.2-preview.1775586717.26498, server payload v0.2.1, with:

sudo hprev relay host install --mode system --channel preview

→ Error: [relay-runtime] relay runtime did not become healthy (http://127.0.0.1:3005/v1/version) every time, and afterwards the daemon can never start again.

Bug 1 — payload lives in /tmp and is deleted by installer cleanup

prepareFirstPartyComponentPayloadFromGitHubRelease downloads and extracts the release into mkdtemp(/tmp/happier-first-party-happier-server-XXXXXX) and returns a cleanup() that rm -rfs it.

installOrUpdateRelayRuntimeLocal then "installs" by:

  • installBinaryShim → symlinks <installRoot>/bin/happier-server to the binary inside the /tmp scratch dir (symlink is attempted first; the copyFile fallback only runs if symlinking fails, which it doesn't on darwin)
  • rendering NODE_PATH and PRISMA_QUERY_ENGINE_LIBRARY via serverBinDir: dirname(params.serverBinaryPath) — i.e. also pointing into the /tmp scratch dir — into both the launchd plist and /etc/happier-preview/server.env

The CLI's install command wraps everything in try { ... } finally { await prepared.cleanup(); }, so the scratch dir is deleted when install finishes (success or failure). The permanent install root ends up containing only a dangling symlink and self-host-state.json — the only copy of the server payload is gone.

Observed end state:

/opt/happier-preview/bin/happier-server -> /tmp/happier-first-party-happier-server-RFyOc8/.../happier-server  (dangling)

and the daemon crash-loop in server.err.log:

PrismaClientInitializationError:
Unable to require(`/tmp/happier-first-party-happier-server-RFyOc8/extract/happier-server-v0.2.1-darwin-arm64/node_modules/.prisma/client/libquery_engine-darwin-arm64.dylib.node`)
Details: dlopen(...): no such file

After the first crash, launchd cannot even respawn the job because the executable symlink is dangling — launchctl print system/happier-server-preview shows state = spawn scheduled, active count = 0, forever. (The launchd-spawned process initially keeps running off the unlinked inode, which is why the install briefly shows a live PID — but dlopen needs a real path and dies.)

Bug 2 — health check is a one-shot probe, so install always reports unhealthy

checkRelayRuntimeHealth → probePortOpen issues a single createConnection attempt. The 30 000 ms value is only a socket timeout; on ECONNREFUSED the error event fires within milliseconds and resolves false — there is no retry/poll loop. The freshly bootstrapped server takes ~1s to bind the port (Bun bundle + Prisma init), so the probe virtually always fails first, install throws "did not become healthy", and the finally cleanup from Bug 1 deletes the payload out from under the just-spawned daemon.

Timeline from my machine: plist written 09:25:36 → installer probe fails + cleanup deletes /tmp → server logs the dlopen failure at 09:25:37.

Bug 3 — HAPPIER_SQLITE_AUTO_MIGRATE=0 on darwin+Bun leaves the DB unmigrated

renderSelfHostServerEnvText:

const autoMigrateSqlite = platform === 'darwin' && hasBunRuntime ? '0' : '1';

hasBunRuntime reflects the CLI process (always Bun for the shipped binary), not the server, so macOS installs always get 0 while Linux gets 1. Nothing else applies migrations (HAPPIER_SQLITE_MIGRATIONS_DIR points at <dataDir>/migrations/sqlite, which the installer never creates either). After I manually repaired bugs 1/2, the server came up "healthy" on /v1/version but every DB-touching code path failed with The table main.Session does not exist in the current database — 316 warnings accumulated in minutes. Setting HAPPIER_SQLITE_AUTO_MIGRATE=1 (after copying prisma/sqlite/migrations from the payload into the data dir) made the server apply all 26 migrations cleanly on next start.

Suggested fixes

  1. Copy the payload to a permanent location under installRoot (the installVersionedPayload / promoteVersionedPayload machinery already exists in firstPartyRuntime/) and render NODE_PATH / PRISMA_QUERY_ENGINE_LIBRARY / the bin shim against that location — never against the scratch dir.
  2. Poll the health endpoint with retries for the full timeout window (e.g. retry every 500 ms–1 s up to 30 s) instead of a one-shot connect.
  3. Revisit the darwin+Bun AUTO_MIGRATE=0 special case (or ship/copy the migrations dir and document who is responsible for applying migrations on macOS).

Manual recovery that worked (for anyone else hitting this)

  1. Download happier-server-vX.Y.Z-darwin-arm64.tar.gz from the server-preview release, verify sha256 against darwin-arm64.json, extract to /opt/happier-preview/payload/<payload-root>/
  2. Re-point <installRoot>/bin/happier-server (and the /usr/local/bin shim) at the permanent payload binary
  3. Patch NODE_PATH / PRISMA_QUERY_ENGINE_LIBRARY in the launchd plist and server.env to the permanent paths
  4. Copy prisma/sqlite/migrations from the payload to <dataDir>/migrations/sqlite and set HAPPIER_SQLITE_AUTO_MIGRATE=1
  5. launchctl bootout + bootstrap + kickstart — server healthy, 26 migrations applied, survives respawn with no /tmp references

Environment

  • macOS Darwin 25.5.0, Apple Silicon (Mac mini)
  • hprev 0.2.2-preview.1775586717.26498
  • happier-server payload v0.2.1 (server-preview release, build commit 4913c1e)
  • Install mode: --mode system --channel preview

lowtek7 avatar Jun 13 '26 00:06 lowtek7

Hi @lowtek7 , Thank you very much for the detailed report!

It should actually be solved on dev version/channel already, but I've not pushed a new preview/stable release yet since then.

I'm finalizing a few things before pushing a new dev version out, and once everything seems stable-ish will push a big new preview version out with everything that has been fixed since then :) quite a lot of new features, fixes and improvements!

Keeping this issue opened in case anyone encounter the issue with preview in the meantime. Thanks again!

leeroybrun avatar Jun 15 '26 16:06 leeroybrun

Thanks for the macOS installation and cleanup reproduction. CLI and server 0.2.16 keep the relay runtime in its permanent installation and correct service startup and migration handling on macOS.

Please reply or reopen with your component versions if this still happens after updating.

Posted on behalf of @leeroybrun.

happier-bot avatar Oct 11 '26 12:10 happier-bot