C.W.K.
Stream
Lesson 01 of 04 · published

One Process Owns the Sound

~9 min · launchd, one-process, lifecycle

Level 0Silent
0 XP0/36 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"The single launchd-owned process is the whole product body. UI actions call into it; they never spawn a second daemon."

A Speaker Is a Single Resource

The audio output is one physical thing. Two processes both trying to own it is a recipe for stutter, races, and audio that overlaps or drops. So Bellows runs as exactly one production process, managed by launchd. That process is the entire product body: it serves the API, serves the built UI, and owns the audio worker inside its own lifespan. There is no separate audio daemon and no side socket — the shape the retired predecessor had, deliberately folded into one.

launchd Keeps It Alive, the App Keeps It Single

The launchd job is set to start at load and stay alive — if the process dies, launchd brings it back. That handles availability. The other half is a rule the app enforces on itself: UI actions never fork a shadow daemon to "help." Every request drives the one existing worker. Availability is launchd's job; singularity is the app's discipline. Together they mean there is always exactly one owner of the speaker, and it's always the same one.

Why Shadow Daemons Are the Enemy

The tempting bug is a background helper spun up for a specific task — a quick second process to do playback, or a worker forked per request. Each one is a new claimant on the single audio device, and now "who is playing right now" has more than one answer. Keeping the process count at exactly one, enforced from both sides, is what makes the audio behave like it has a single, sober owner.

Code

One managed process, restarted but never duplicated·bash
# launchd owns exactly ONE production process. No app code spawns another.
$ launchctl print gui/$(id -u)/com.cwk.bellows-serve | grep -E 'state|pid'
    state = running
    pid = 4123

# The plist sets RunAtLoad + KeepAlive: the process starts at login and is
# restarted if it dies. That single process owns the audio worker in-lifespan.
# UI actions call INTO it -- they never fork a second daemon or open a socket.

$ launchctl kickstart -k gui/$(id -u)/com.cwk.bellows-serve   # clean restart

External links

Exercise

Find a single physical or logical resource in a system you know that really should have one owning process — a printer, a GPU, a serial device, a lock file, a hardware token. Describe what breaks if two processes both try to own it. Then split the guarantee in two: what provides availability (restart), and what enforces singularity (never a second)?
Hint
Availability and singularity are different promises made by different parts. A supervisor restarts a dead process; only the app's own discipline stops it from spawning a rival. Missing either one leaves the resource contended or unavailable.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.