Repository navigation
Keep the Mac microphone mapping current after upgrades and at login - #34
Conversation
The watcher ran its first pass and only then started pactl subscribe, and after a lost subscription it repaired before resubscribing. A DSP microphone that appeared in between, as asahi-audio's does a moment after login, raised an event nobody heard, and the mapping waited for the ten-minute backstop. The supervisor now subscribes before its first pass, and every resubscription is followed by a full pass at once.
pacman restarts no user service, so after an upgrade the running watcher kept the old code until the next login: 0.1.2's virtual source stayed the default input after 0.1.3 was installed. The watcher now looks at its own file every two seconds; once a different file has stayed unchanged for a look and no pacman transaction holds the database (the interpreter may be upgraded in the same transaction; a lock older than a minute is ignored), it stops its subscriber and execs the new file under the same PID, whose first pass reconciles the live session, including a 0.1.2 source. A file that is missing, not executable or fails to exec leaves the old code running.
…rade A mapper from 0.1.3 or earlier has no self-reload, so the upgrade that brings it would still leave it running old code until the next login; a 0.1.2 mapper kept its virtual source. A PostTransaction hook on the mapper's file now restarts, through each logged-in user's manager (systemctl --user --machine=user@ try-restart, without blocking pacman), every running mapper whose PID is not the one recorded in the user's runtime directory, which only self-reloading watchers write. Once every session runs a self-reloading watcher the hook restarts nothing. It does nothing off Apple Silicon, in a chroot or an unbooted root, and never fails the transaction.
Review found three holes. The two-second look at the mapper's own file restarted with every wait, so watched events more often than that, or a subscriber that kept dying, postponed the reload indefinitely; the next look is now kept on the subscription and consulted on every return path. A failed exec (the interpreter mid-replacement) disarmed the reload for good, while the upgrade hook skips a mapper that recorded its PID; it now retries with a growing back-off. And the lock was bypassed after a minute even with pacman still running its hooks; a lock is now ignored only when no pacman process exists. Popen returning does not mean pulse has registered the subscription, so a pass now also follows a second after subscribing. The end-to-end reload test points the pacman lock at its fixture and drops PULSE_SERVER and PIPEWIRE_REMOTE, so it passes the same on any host.
The record lives in the user's runtime directory, so root reads at most a few bytes of it under a timeout, and the user lookup is bounded too.
|
Review: first pass found three must-fixes, all fixed in 59c7bc9 and 8db7207.
Also changed: a stale lock is ignored only when no pacman process is running, there's an extra pass 1 s after subscribing, and the hook's marker read is bounded. Second pass: MUST FIX: none. Re-verified on the M2 with the final code. The mapper reloaded itself 1.2–1.7 s after pacman finished, including under a flood of gain changes every 0.4 s. Same PID, the hook skipped it, and the default, gain and recording were unchanged. Needs maintainer opt-in before merge: Not blocking: after a pipewire-pulse restart, the 1 s confirm pass is cancelled by the immediate pass, which is the same as before this PR. |
Only lab Macs run 0.1.3, and their mapper switches to the self-reloading one at the next login. Keeping a root hook that restarts users' services for one transition isn't worth the precedent.
Summary
The Asahi microphone mapping is now right straight after an upgrade in a running session, and right from the first pass of a new session. Nobody has to restart anything.
pacmanand foromarchy updatealike, and root never reaches into a session. Whiledb.lckis held the exec waits, because the interpreter may be upgraded in the same transaction. A lock older than a minute is ignored. If the new file is missing, not executable or fails to exec, the old code keeps running.After=/Wants=on pipewire, pipewire-pulse and wireplumber) was already right. The existing migration from Meter the Mac microphone like a stock one (omarchy-mac 0.1.3) #28 handles 0.1.2 state and keeps gain and selection.Evidence
M2 Max, live desktop session, nothing restarted by hand (
wpctl inspect @DEFAULT_AUDIO_SOURCE@plus a 2 sparecordthat names no device):pacman -U0.1.4Audio/Source/Virtualuntilsystemctl --user restart(seen on the 0.1.2 → 0.1.3 upgrade)Audio/Source [FL, FR]was the default 1.1 s later; one "MacBook Microphone"; 55 % gain kept; recording fromomarchy_asahi_mic, non-zeroRunning the updated microphone mapper1.0 s after pacman released its lock, same PID, hook skipped it; default, gain and recording unchangedTests:
omarchy-mac/test/all(92 ok) andtest/integration/allpass in an Arch Linux ARM container with the runtime at e1b0e5e9b. New tests cover:Program.replaced: unchanged, changing, pacman lock, stale lock, missing, non-executable and failed-exec cases--watchwhose file is replaced execs the new file under the same PID with--watchand stops its oldpactl subscribesystemctlstill exits 0Merge Danger
Door: two-way. Reverting removes the reload, the hook and the marker file. Nothing persistent is written except a PID file in the runtime directory.
Blast Radius: microphone. The worst cases: a mapper that reloads into a broken file exits, and systemd restarts it (
Restart=on-failure). A hook that can't reach a user manager times out after 10 s per user and leaves that mapper as it was before.Test plan
Relogin=falseruled outloginctl terminate-user