Skip to content

Keep the Mac microphone mapping current after upgrades and at login - #34

Merged
maralcbr merged 7 commits into
mainfrom
mac-mic-reload
Oct 12, 2026
Merged

maralcbr merged 7 commits into
mainfrom
mac-mic-reload

Conversation

@maralcbr

Copy link
Copy Markdown
Member

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.

 omarchy-audio-asahi-mic-map --watch
+  write $XDG_RUNTIME_DIR/omarchy-asahi-mic.reloads = PID
+  subscribe (pactl subscribe)          # before the first pass, not after it
   loop
     reconcile                           # map, retire 0.1.2 source, select default
     wait for events
+      every 2 s: own file replaced, unchanged since the last look, no pacman db.lck?
+        -> stop subscriber, execv(new file)   # same PID; first pass reconciles live state
+    after a lost subscription: resubscribe, then a full pass at once

+pacman PostTransaction (Upgrade of usr/bin/omarchy-audio-asahi-mic-map)
+  asahi-mic-restart: for each running user manager
+    MainPID != recorded PID  -> systemctl --user -M user@ --no-block try-restart
  • Self-reload is the main mechanism. pacman never restarts user services, so the watcher checks its own file (dev, inode, size, mtime). Once an upgrade has replaced the file and the transaction has finished, it execs the new code under the same PID. This works in every session, for plain pacman and for omarchy update alike, and root never reaches into a session. While db.lck is 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.
  • Bootstrap hook. Mappers from 0.1.3 and earlier can't reload themselves, which leaves the first upgrade uncovered. A 0.1.2 mapper would otherwise keep its virtual source until the next login. The hook restarts, through the user's own manager, only the mappers that haven't recorded their PID. Once every session runs a self-reloading watcher it restarts nothing. It does nothing off Apple Silicon, in a chroot or in an unbooted root, and it never fails the transaction. Reviewers: this is root talking to user managers. Say if it needs a maintainer opt-in.
  • First-login gap. The watcher used to run its first pass before it subscribed to events, and after losing pipewire-pulse it repaired before resubscribing. If asahi-audio's DSP source appeared in that window, as it does a moment after login, nothing heard the event, and the mapping waited up to the 600 s backstop. The watcher now subscribes first, and every resubscription is followed by a full pass straight away. Unit ordering (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.
  • omarchy-mac 0.1.4.

Evidence

M2 Max, live desktop session, nothing restarted by hand (wpctl inspect @DEFAULT_AUDIO_SOURCE@ plus a 2 s parecord that names no device):

Case Before After
0.1.2 mapper running → pacman -U 0.1.4 Audio/Source/Virtual until systemctl --user restart (seen on the 0.1.2 → 0.1.3 upgrade) hook restarted it at the end of the transaction; typed Audio/Source [FL, FR] was the default 1.1 s later; one "MacBook Microphone"; 55 % gain kept; recording from omarchy_asahi_mic, non-zero
0.1.3 running → 0.1.4 old code kept hook restarted it; default stayed typed; 70 % gain kept
0.1.4 running → 0.1.4-2 rebuild — Running the updated microphone mapper 1.0 s after pacman released its lock, same PID, hook skipped it; default, gain and recording unchanged
Mapper starts, DSP appears 5.7 s later (WirePlumber held back) — mapped and made the default 0.6 s after the DSP appeared
Restart of stack + mapper, mapper before stack, mapper after stack, mapper alone — typed default within 0.6–2.8 s every time; shell survived

Tests: omarchy-mac/test/all (92 ok) and test/integration/all pass in an Arch Linux ARM container with the runtime at e1b0e5e9b. New tests cover:

  • subscribe-before-pass ordering
  • a pass straight after any (re)subscription
  • Program.replaced: unchanged, changing, pacman lock, stale lock, missing, non-executable and failed-exec cases
  • RELOAD noticed during a steady event stream
  • supervise on RELOAD
  • end to end: a running --watch whose file is replaced execs the new file under the same PID with --watch and stops its old pactl subscribe
  • the hook script: only mappers without a matching record restart, a symlinked record is ignored, nothing happens in a chroot, an unbooted root or off Apple Silicon, and a failing systemctl still exits 0

Merge 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

  • omarchy-mac suite and integration suite in an Arch Linux ARM container
  • M2: 0.1.3 → 0.1.4, 0.1.2 state → 0.1.4, 0.1.4 → rebuild (self-reload), all with no manual restart
  • M2: session-start simulations in five orders, including a late DSP
  • Real reboot / first login on the M2. The encrypted disk needs the owner, and SDDM Relogin=false ruled out loginctl terminate-user

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.
@maralcbr
maralcbr requested a review from scottjones as a code owner October 12, 2026 09:32
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.
@maralcbr

Copy link
Copy Markdown
Member Author

Review: first pass found three must-fixes, all fixed in 59c7bc9 and 8db7207.

  • The reload poll reset on every wait, so frequent events or a dying subscriber postponed the reload. The poll is now kept across waits.
  • A failed exec disarmed the reload for good. It now retries with a back-off.
  • The e2e test read the host's pacman lock. It now uses a fixture.

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: 90-omarchy-mac-asahi-mic.hook is the first root PostTransaction hook here that acts on users' systemd --user managers (systemctl --user --machine=user@ --no-block try-restart omarchy-asahi-mic.service). It only restarts mappers that predate self-reload, so it covers the upgrade away from 0.1.2 or 0.1.3. Say yes to keep it, or I drop that commit (f6494a3 plus the marker lines). Without it, a 0.1.2 mapper keeps its virtual source until the next login after this upgrade, and only after this one.

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.
@maralcbr

Copy link
Copy Markdown
Member Author

Owner decision: drop the upgrade hook. Reverted in e01ca10 (f6494a3 + 8db7207): no pacman hook and no PID record; 0.1.3 mappers switch at the next login, every later upgrade reloads itself. omarchy-mac suite 92 ok.

@maralcbr
maralcbr merged commit 222f7a8 into main Oct 12, 2026
6 checks passed
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