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")