Most apps can crash and come back, and the user loses a few seconds. A DJ app can't. If it crashes in the middle of a set, the room goes silent, everyone turns to look at the booth, and the DJ is restarting software in front of a few hundred people
I build Blendline, an app that analyzes your tracks, lays them out on a timeline as a set that's already mixed, and plays it live while you keep editing it. A lot of code runs in that app: SwiftUI, an AppKit timeline, decoding and analyzing hundreds of audio files in parallel, MIDI, a remote control over WebRTC. Any of it can have a bug. So I asked a simple question: where should the sound live so that most of those bugs can't stop it?
The answer has three layers. Each one covers what the one before can't
Layer 1: the sound lives in its own process
The window you see is a remote control. Playback, MIDI clock and the headphone monitor run in BlendlineEngine, a small executable that sits next to the app's own in Contents/MacOS. The app starts it with Process the first time it needs a player, and talks to it over a unix domain socket
When the parent of a process dies on macOS, the child keeps running, nothing to configure. If the timeline, the analysis or anything else in the app crashes, the engine doesn't notice, and the set plays on without a gap
The engine has no window, which is exactly the kind of process macOS likes to slow down. One call at startup keeps it at full speed while it plays:
let activity = ProcessInfo.processInfo.beginActivity(
options: [.userInitiated, .latencyCritical],
reason: "Plays a set"
)
The protocol
The app sends requests and the engine answers each one with its state as it is after the request. Both sides are Swift, so the protocol is a pair of Codable enums, framed on the socket as a 4-byte big-endian length and a JSON payload:
public enum EngineRequest: Codable, Sendable, Equatable {
case hello
case createMain(set: URL)
case createMonitor
case dropMonitor
case reset
case player(EnginePlayerID, PlayerCommand)
case quit
// ...
}
public enum PlayerCommand: Codable, Sendable, Equatable {
case load(SetLayout)
case seek(TimeInterval)
case play
case pause
case moveTo(TimeInterval)
case setOutput(device: String?, firstChannel: Int)
// ...
}
JSON sounds slow for audio, but nothing audio-rate crosses this socket. A request says "load this layout" or "jump to bar 96", and the engine renders the audio itself, from a producer thread into a lock-free ring buffer that its output unit reads. Messages are small and rare, and readable json paid for itself the first time I had to debug the protocol :)
A detail that bit me: setOutput carries the device's UID string, not an AudioDeviceID. An AudioDeviceID is only meaningful inside the process that read it. Once the audio moved to another process, every ID the app sent was a number that happened to point at something else
State, and time between states
While the app is connected, the engine also pushes its state every 20 ms. The app never asks "where is the playhead?" and waits. It reads the latest state and runs the clock forward from when that state was read:
public func bar(at now: Double, readAt: Double) -> Double? {
guard let bpm else { return nil }
return (currentTime + (isSounding ? now - readAt : 0)) / SetLayout.barDuration(bpm: bpm)
}
That keeps the timeline's playhead smooth at the display's frame rate without a round trip per frame, and it's the same formula the app uses after a crash, as you'll see below
Taking the set back after a crash
When the app opens, before loading anything, it connects to the socket. If an engine answers and its main player is playing, the app loads that set, adopts the player by its ID, and the person in front of the screen sees the timeline come back with the playhead already moving
There's a catch. The reopened app has to analyze its tracks again before it knows the full layout. Even with the analysis cache, that takes a moment. Meanwhile, it holds back: it doesn't send the engine a layout until its own is complete. Otherwise the first thing a freshly reopened app would do is tell the engine to play a half-loaded set
Housekeeping
Three small rules keep an outliving process from becoming a stray one:
- one app at a time. A new connection replaces the old one. Thats exactly what happens after a crash: the dead app's connection is gone, and the new one takes its place
- it leaves when it's not needed. With no app connected and nothing playing for 30 seconds, the engine quits. A quit from the app ends it right away
-
builds match. The first request on every connection is
hello, and the engine answers with an identity of its binary (its modification time). After an update, the new app finds an old engine still running, asks it to quit, and starts its own
The socket lives in the user's own temporary folder, read with confstr(_CS_DARWIN_USER_TEMP_DIR) rather than from TMPDIR. Only the user can reach it, and the path always fits in the 104 bytes a socket address holds
Layer 2: if the engine goes down, start another one
The engine is small, but it's where the DSP runs, and it talks to Core Audio and MIDI drivers that I don't control. It can crash too
When the socket closes without a quit, the app has the last state the engine sent. That's enough to know which bar the set had reached and at which tempo. It starts a new engine, loads the set and plays from the bar the set would have reached by now. The room hears a short gap, then the music comes back in time, on the bar where it should be
Layer 3: if both go down, resume from a file
The last case is the worst one: the app and the engine both crash, or the system kills both. No process is left that remembers anything
So while a set plays, the app writes a small file once a second:
public struct PlaybackRecovery: Codable {
public var set: URL
public var bar: Double // bar heard at `date`
public var bpm: Double // set tempo at `date`
public var date: Date
public var resumes: Int // times in a row this set came back
}
Stopping or quitting normally removes it. So if the app opens and finds it, something cut the set short. Bars at a constant tempo are easy to project: four beats per bar, so the set has advanced elapsed * bpm / 240 bars since the file was written. The app opens the set, waits for its analysis and plays from there
Two limits keep this from turning into a new problem:
- a time window. Past two minutes, the set doesn't resume. Someone opening the app the next morning doesn't want a set to start playing on its own
- a crash loop breaker. Every resume counts, and the count is written before the set plays. After three in a row without a clean stop, the app opens without playing. A set that crashes the app on load would otherwise crash it forever
An earlier version also reopened the app on its own, through a small watcher process. I removed it. With the engine playing on through an app crash, the common case no longer needs it, and an app that relaunches itself is suprising. The person at the booth reopens it from the crash dialog or the Dock, and it picks up wherever the music is
What it costs
Splitting a real-time app in two isn't free:
- every command is a message. Anything the app wants the player to do has to exist in the protocol, and both sides have to agree on it. Adding a feature now often means touching the request enum, the engine and the app
- the app's view of playback is a copy. It's 20 ms old at most, and the code has to treat it that way. Questions like "is it playing?" are answered from the last state, never from a variable the app set itself
- requests can fail in new ways. The engine might not answer within its timeout, or be gone. Commands that can fail in a way the user should see throw; the rest are sent and forgotten, and a lost engine is reported once, in one place
What made it manageable is that the engine's server is plain code over a socket path. The tests run it inside the test process on a socket in the temporary folder, with players that render offline instead of through a sound device, and drive it with the same client the app uses
Takeaways
- put the thing that must not stop in the smallest process you can, and let everything else be a client of it
- make the client's state a copy with a timestamp, and extrapolate between updates instead of asking
- send identifiers that mean the same thing in every process, like device UIDs and file URLs
- every automatic recovery needs a limit, in time and in count
If you DJ, or you're curious how this feels from the booth, Blendline has a 7-day free trial. I'm happy to answer questions about the engine in the comments
Top comments (0)