display agent: back off retrying a mirror that keeps failing

The daemon restarted a monitor's mirroring process 10 s after every
failure, however often it failed. Overnight on 2026-10-10 the DP-4
process failed "CGVirtualDisplay could not be created" for ten hours
and was started 3,269 times, each start opening a session and running
the device init.

Wait 10 s after the first failure, doubling after each further failure
in a row up to 5 minutes (MirrorRetryBackoff), with one log line per
step giving the count and the next delay. A mirror that comes up resets
the backoff, as do the monitor leaving or being turned off (which also
drops a pending retry, so a re-plugged monitor starts at once) and a
GPU reset, after which mirrors start again immediately as before. The
success path is unchanged.
This commit is contained in:
geramyloveless committed 2026-10-10 18:08:41 -07:00
1 parent e31d50c09f
commit 420a97df30
3 files changed
+57 -1

No files matched your search

+22
View File
@@ -971,6 +971,28 @@ enum ResetAction: Equatable {
}
}
/// How long the daemon waits before starting a monitor's mirroring process
/// again after it ended with an error: 10 s, doubled after each further
/// failure in a row, at most 5 minutes. A mirror that comes up resets it, as
/// does the monitor leaving or being turned off. A failure that lasts (a
/// virtual display the WindowServer refuses while the Mac is locked) then
/// costs one attempt every 5 minutes, not one every 10 s.
struct MirrorRetryBackoff: Equatable {
static let initial: TimeInterval = 10
static let maximum: TimeInterval = 300
private(set) var failures = 0
/// The delay before the next attempt, after one more failure.
mutating func failed() -> TimeInterval {
failures += 1
var delay = MirrorRetryBackoff.initial
for _ in 1..<failures where delay < MirrorRetryBackoff.maximum { delay *= 2 }
return min(delay, MirrorRetryBackoff.maximum)
}
mutating func reset() { failures = 0 }
}
/// What the daemon reports for the menu bar.
struct DisplayStatus: Codable, Equatable {
enum State: String, Codable { case off, starting, mirroring, error }
+20 -1
View File
@@ -1482,6 +1482,7 @@ func runDisplayAgentDaemon(_ options: [String]) -> Int32 {
var children: [String: MirrorChild] = [:]
var retryAt: [String: Date] = [:]
var backoff: [String: MirrorRetryBackoff] = [:]
var lastErrors: [String: String] = [:]
var monitors: [(connector: String, key: String, name: String)] = []
var published: DisplayStatus?
@@ -1673,12 +1674,14 @@ func runDisplayAgentDaemon(_ options: [String]) -> Int32 {
for child in children.values { stop(child) }
children = [:]
retryAt = [:]
backoff = [:]
lastErrors = [:]
restartNowUntil = Date().addingTimeInterval(15)
case .restartEnded:
agentLog("display-agent: GPU queue reset (generation \(reset.generation), \(reset.queueResets) in all); " +
"the outputs redraw, and a mirror that ended starts again now")
retryAt = [:]
backoff = [:]
restartNowUntil = Date().addingTimeInterval(15)
case .none:
break
@@ -1741,11 +1744,22 @@ func runDisplayAgentDaemon(_ options: [String]) -> Int32 {
}
// Reconcile the children with the choices and the monitors.
let wanted = Set(connectorsToMirror(connected: monitors.map { ($0.connector, $0.key) }, prefs: prefs))
// A mirror that came up starts its connector's backoff over.
for (connector, child) in children where child.state == .mirroring { backoff[connector] = nil }
for (connector, child) in children where child.exited || !wanted.contains(connector) {
if !child.exited { stop(child) }
if child.state == .error, let error = child.lastError {
lastErrors[connector] = error
retryAt[connector] = Date() < restartNowUntil ? nil : Date().addingTimeInterval(10)
if Date() < restartNowUntil {
retryAt[connector] = nil
} else {
var steps = backoff[connector] ?? MirrorRetryBackoff()
let delay = steps.failed()
backoff[connector] = steps
retryAt[connector] = Date().addingTimeInterval(delay)
agentLog("display-agent: \(connector): mirroring failed \(steps.failures) time(s) in a row; " +
"next attempt in \(Int(delay)) s")
}
} else {
lastErrors[connector] = nil
}
@@ -1759,6 +1773,11 @@ func runDisplayAgentDaemon(_ options: [String]) -> Int32 {
start(connector)
}
for connector in Array(lastErrors.keys) where !wanted.contains(connector) { lastErrors[connector] = nil }
// A monitor that left or was turned off starts over when it is back.
for connector in Array(backoff.keys) where !wanted.contains(connector) {
backoff[connector] = nil
retryAt[connector] = nil
}
publish()
_ = RunLoop.main.run(mode: .default, before: Date().addingTimeInterval(2))
}
+15
View File
@@ -734,3 +734,18 @@ do {
check(cpuSeconds() - cpu < 0.1, "idle after the processes ended")
}
print("PASS mirroring process output: every chunk, the read end closed at end of file, no descriptor left, no spin")
// A mirroring process that keeps failing: 10 s, doubling, at most 5 min;
// starts over after a success.
do {
var backoff = MirrorRetryBackoff()
let delays = (0..<8).map { _ in backoff.failed() }
check(delays == [10, 20, 40, 80, 160, 300, 300, 300], "delays \(delays)")
check(backoff.failures == 8)
backoff.reset()
check(backoff.failures == 0 && backoff.failed() == 10, "a success starts over")
var many = MirrorRetryBackoff()
for _ in 0..<10_000 { _ = many.failed() }
check(many.failed() == 300, "capped however long it fails")
}
print("PASS mirror retry backoff: 10 s doubling to 5 min, reset on success")