From 03604c83676a9502ef321643194140f1e6130c7a Mon Sep 17 00:00:00 2001 From: neil Date: Wed, 4 Nov 2026 20:18:43 -0800 Subject: [PATCH] macOS: wait for the old launchd job before reloading the daemon Re-running the installer on a Mac failed with "Bootstrap failed: 5: Input/output error". bootout returns while the old job is still stopping, and the bootstrap right after it fails. setup.sh now lints the plist, waits until launchctl print no longer finds the job, re-enables it (a disabled job also fails with error 5), retries the bootstrap once, and names the commands to check if it still fails. Co-Authored-By: Claude Opus 5.5 --- native/setup.sh | 12 +++++++++++- tests/test_installers.py | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/native/setup.sh b/native/setup.sh index 5d890ae..bd1630e 100755 --- a/native/setup.sh +++ b/native/setup.sh @@ -144,9 +144,19 @@ else PLIST as_root install -m 644 -o root -g wheel "$plist" "/Library/LaunchDaemons/$SERVICE.plist" rm -f "$plist" + plutil -lint "/Library/LaunchDaemons/$SERVICE.plist" >/dev/null || native_die "the generated plist is invalid: plutil -lint /Library/LaunchDaemons/$SERVICE.plist" + # Unload the old job and wait until it is really gone: bootout returns while the job is + # still stopping, and bootstrapping then fails with "5: Input/output error". as_root launchctl bootout "system/$SERVICE" >/dev/null 2>&1 || true - as_root launchctl bootstrap system "/Library/LaunchDaemons/$SERVICE.plist" + i=0 + while as_root launchctl print "system/$SERVICE" >/dev/null 2>&1 && [ "$i" -lt 30 ]; do sleep 0.5; i=$((i + 1)); done + # A job marked disabled (launchctl disable) can't be bootstrapped either. as_root launchctl enable "system/$SERVICE" >/dev/null 2>&1 || true + if ! as_root launchctl bootstrap system "/Library/LaunchDaemons/$SERVICE.plist"; then + sleep 2 # one retry, for a job that took longer to stop + as_root launchctl bootstrap system "/Library/LaunchDaemons/$SERVICE.plist" \ + || native_die "launchctl bootstrap failed. Check: sudo launchctl print system/$SERVICE; sudo launchctl print-disabled system | grep $NAME" + fi fi if [ "$PLATFORM" = systemd ] && [ "$AVAHI" = 1 ]; then native_log "avahi: announcing $(hostname -s).local" diff --git a/tests/test_installers.py b/tests/test_installers.py index b050bfc..c6542fa 100644 --- a/tests/test_installers.py +++ b/tests/test_installers.py @@ -158,3 +158,11 @@ def test_macos_daemon_runs_as_root(): setup = (ROOT / "native" / "setup.sh").read_text() plist = setup[setup.index("")] assert "UserName" not in plist + + +def test_macos_reload_waits_for_the_old_job(): + """bootstrap right after bootout fails with "5: Input/output error" while the old job stops.""" + setup = (ROOT / "native" / "setup.sh").read_text() + reload = setup[setup.index('launchctl bootout "system/$SERVICE"'):] + assert reload.index('launchctl print "system/$SERVICE"') < reload.index("launchctl bootstrap system") + assert reload.index('launchctl enable "system/$SERVICE"') < reload.index("launchctl bootstrap system")