My job queue could starve its biggest job
New in Purrchiver 1.1. If you don't see it, update Purrchiver from the App Store.
In June I wrote about the job queue in Purrchiver: lanes for the two engines that can’t run concurrently, a global FIFO line, and one rule that kept it fast: a job blocked on a busy lane gets skipped, so the ZIP behind it doesn’t wait for a RAR it has nothing to do with.
That rule had a hole. A job that needs two lanes could be passed over again and again, by jobs that each needed only one of them, for as long as they kept arriving. There’s a textbook name for this: starvation. Here’s how it happened, why I missed it the first time, and the small change that fixed it.
The one job that needs two lanes
Most jobs need one lane or none. libarchive handles ZIP, 7z, tar and the rest,
and needs no lane at all. Encrypted RAR goes through UnRAR, which gets the
UnRAR lane. Apple Archive (.aar, .aea) gets the Apple Archive lane.
One job needs both: opening a RAR in Edit mode and saving it as an Apple Archive. It reads through UnRAR and writes through the Archive framework, so it holds both lanes for its whole run.
Two ways in, one blind spot
A job gets admitted one of two ways. If its lanes are free and the overall limit isn’t reached when it’s submitted, it starts on the spot. Otherwise it joins the line, and an admission loop, the one from the June post, walks the line front to back whenever something finishes and admits every waiter that fits:
func pump() {
var i = 0
while i < waiters.count {
let w = waiters[i]
if running < limit && w.lanes.allSatisfy({ laneCount[$0, default: 0] == 0 }) {
admit(w)
waiters.remove(at: i)
} else {
i += 1 // blocked: skip it, keep looking
}
}
}Both paths asked the same question: are my lanes free right now? Neither asked whether someone earlier was already waiting for them.
Line up four jobs:
- A, a RAR extract, is running. It holds UnRAR.
- W, the RAR-to-Apple-Archive save, arrives. It needs UnRAR and Apple Archive. UnRAR is busy, so W joins the line. Apple Archive is free.
- X, an
.aarextract, arrives. Its one lane is free, so it starts on the spot. Nothing checked that W was already waiting for that lane. - A finishes and UnRAR frees up. W still can’t start, because X holds Apple Archive. If a RAR job was waiting behind W, the loop skips W and admits it, and now UnRAR is taken again.
Each lane comes free at some point, but something smaller always takes it before the other one does. Every individual decision was correct: every job admitted really could run. Taken together, W doesn’t get a turn while RAR and Apple Archive jobs keep overlapping.
I found it reading the code, not from a user report. Today only that one job needs two lanes, and it only waits while other RAR and Apple Archive work keeps overlapping, so the real-world impact was small. But it broke the promise the queue makes, and the next two-lane job would inherit it.
Update, October 23: it didn’t have to wait for a future one. Compare Archives, also new in 1.1, holds the lanes of both archives it reads, so comparing a RAR with an Apple Archive is a second two-lane job, and it gets the same reservation.
Why I didn’t see it in June
The June post said a later job never jumps an earlier one that could equally run. That’s true, and it’s the problem. W couldn’t run, so by that rule it was fair game to skip. The rule protects jobs that are ready. It says nothing about a job waiting for several things that each come free at a different time.
With one lane per job, skipping is harmless: when your lane frees up, you’re first in line for it. With two, you can be first in line for each lane and still never hold both.
The fix: a waiting job’s lanes are reserved
When the loop passes over a waiter, it now remembers that waiter’s lanes as reserved. A later job that needs any reserved lane waits, even if the lane is free right now. A job on an unrelated lane still skips ahead.
func pump() {
var reserved: Set<Lane> = []
var i = 0
while i < waiters.count {
let w = waiters[i]
if canAdmit(w.lanes, reserved: reserved) {
admit(w)
waiters.remove(at: i)
} else {
reserved.formUnion(w.lanes) // passed over: hold its lanes for it
i += 1
}
}
}
func canAdmit(_ lanes: Set<Lane>, reserved: Set<Lane>) -> Bool {
running < limit
&& lanes.isDisjoint(with: reserved)
&& lanes.allSatisfy { laneCount[$0, default: 0] == 0 }
}The start-on-the-spot path gets the same check. Every job already in line arrived first, so all of their lanes count as reserved:
func submit(_ job: Job) async {
let reserved = waiters.reduce(into: Set<Lane>()) { $0.formUnion($1.lanes) }
if canAdmit(job.lanes, reserved: reserved) { admit(job); return }
await park(job)
}Replay the four jobs. W is waiting, so UnRAR and Apple Archive are reserved. X needs Apple Archive, which is free but reserved, so X joins the line behind W. When A finishes, W gets both lanes. When W finishes, X runs. Meanwhile a ZIP that arrives in the middle needs no lane and, as long as the overall limit allows, starts straight away.
What it costs
Reservation trades some throughput for fairness. X could have run during the time W was waiting on A, and now it doesn’t. In the worst case, Apple Archive sits idle while W waits for UnRAR.
That’s the right trade here. Lanes only exist for the two engines that can’t run concurrently, and those jobs are a small part of what Purrchiver does. The cost lands on RAR and Apple Archive work, and only when a two-lane job is waiting. ZIP, 7z and tar jobs never notice.
The obvious alternative is to let W grab Apple Archive while it waits for UnRAR. That idles the lane just as reservation does, but it holds the real engine lock, so even dragging a file out of an Apple Archive, which skips the queue and waits only on that lock, would have to wait for W. Reservation changes nothing but the order of the batch line. Aging, where a job’s priority rises the longer it waits, would also fix starvation, but it’s more machinery to get the same answer in a line that’s already FIFO.
The test
The test lines up that scenario, with a limit of 4 so the overall cap never gets in the way:
- A RAR extract starts and holds UnRAR.
- The RAR-to-Apple-Archive save queues and checks that it really needs both lanes.
- An
.aarextract is submitted next. It must stay pending, and the Apple Archive lane must stay empty. - A plain ZIP is submitted last. It must start and finish while the others wait.
- When the RAR extract finishes, the two-lane save starts, and the
.aarextract must still be pending while it runs.
As with the rest of the queue’s tests, I checked it can fail: take the reservation out of the start-on-the-spot path and it does. To be precise about coverage, this test exercises that path. The loop’s half of the fix, a job already in line behind W taking a lane as it frees, is covered by the same reasoning but not yet by its own test.
Update, October 23: it has one now. A second test parks the .aar extract
behind the two-lane save on the overall limit rather than on a lane, then frees
a slot, so only the loop’s reservation keeps it back. It fails with that
reservation taken out of the loop.
The general lesson
A scheduler that lets blocked work be skipped needs a second rule for work that’s blocked on more than one thing. “Skip what can’t run” is fair for single-resource jobs. For multi-resource jobs it isn’t, because the resources come free one at a time and something smaller is always ready to take each one.
Reserving what an earlier job is waiting for is the simplest fix I know. It keeps the line FIFO for the jobs that compete with each other, and leaves everything else free to move.
The fix ships in Purrchiver 1.1.
Have you hit starvation in a scheduler of your own, and what did you reach for: reservation, aging, or something else?