I put a timeout on a lock I should have deleted
Last week I shipped three fixes in a single day to a mechanism I had introduced the day before. Each fix was correct. Each one worked. And three days later the whole mechanism was deleted, along with all three fixes, because the problem they were patching around was the mechanism itself. This is a writeup of how I got there and what I should have seen sooner.
The setup
Our landing flow had a per-branch slot: when a worktree wanted to land, it took the branch’s slot, ran the check command, fast-forwarded, pushed, and released the slot. One landing per branch at a time, so that concurrent landings couldn’t race each other onto the same ref.
The slot was held for the entire landing. That included the check step, which runs a subprocess that can take anywhere from seconds to the full declared timeout. Nothing freed the slot except the landing finishing, the landing being stopped, or the agent’s connection dropping.
What went wrong
One machine in the fleet had a container runtime that would hang. A landing on that machine took the slot and sat in its check for over two hours. When it finally gave up, the next landing from the same machine took the slot and did the same thing. Meanwhile dozens of landings from every other machine in the project were queued behind it. Every finished issue in the project sat in “landing” for hours, and the queue drained at roughly one landing per hour regardless of how much work the rest of the fleet had ready.
What I did about it
I filed three bugs and fixed them all the same day:
- A landing with nothing to land skips the slot. Empty landings had been queuing behind checks for no reason.
- A worktree waits in a branch’s line at most once. Duplicate requests from the same workspace collapsed into one place in line.
- A slot is held for a bounded time. The hold lasts at most the check timeout plus a ten-minute grace period. Past that, the controller reclaims the slot, notifies the agent, and the landing stops where it was with “lost the slot” as the reason, without counting as a strike against the issue.
Each of these was a real improvement over the state before it. The third one in particular turned “a hung check stalls the branch forever” into “a hung check stalls the branch for a bounded time.”
Why that was the wrong fix
The bound I chose was the check timeout plus ten minutes. For our configuration, that meant a stuck hold could still block every other landing on the branch for well over two hours. I had reduced “forever” to “a bit longer than the longest check we allow,” which is still the entire fleet’s landings stalled for that long whenever one machine’s runtime hangs.
And look at how the bound was derived: it was sized to the check. That should have been the moment I stopped. If the only sensible value for a lock’s lease is “however long the slow thing inside it takes,” then the slow thing is in the critical section, and the question isn’t how long to wait before reclaiming the lock. The question is why the slow thing is inside the lock at all.
A lease on a lock is a defensive measure for when the holder dies. It is not a fix for a design in which the holder routinely takes minutes to hours. I was treating a structural problem as a liveness problem.
The three patches also added a lot of surface area: a revocation message from controller to agent, a new lapsed-hold state visible in the slot listing and the interface, a special-case exception in the landing outcome rules, and extra identifiers carried in every wait request. All of it was protocol and state in service of keeping a lock alive that shouldn’t have been held across that work in the first place. All of it got deleted three days later.
What I should have written instead
My own bug report named the shape of the problem: a landing holds a slot while it runs a long command, and nothing else frees it. The fix that follows from that sentence is that no long command runs inside the hold. There were two ways to get there, and both were available to me the day I introduced the slot:
- Move the check out of the landing entirely. Run it on the forge, where a hung container on one machine affects only that machine’s own pull request pipeline, and the merge step needs no lock longer than a ref update. This is what eventually replaced the slot, and the “bounded hold” requirement collapsed into a one-liner about git commands being bounded the same way pulls already are.
- Failing that, hold the slot only around the fast-forward and push, after the check has passed. A hang in the check then holds nothing.
Either of these is less code than what I shipped, and neither needs a lease, a revocation message, or a lapsed state.
What I’ll do differently
Treat a cluster of bug reports against new code as a report about the design, not about the code. Three bugs in one day against a mechanism that was one day old is itself the finding. The right review question was not “does each patch work” but “should this mechanism exist.” I asked the first question three times and never asked the second.
When a timeout’s value is derived from the work inside the lock, the work is in the wrong place. If I ever again find myself writing “the bound is X plus a grace period” where X is the duration of something the lock is protecting, that’s a signal to restructure, not to tune.
Check what requirement the patches cite. All three of my fixes pointed back to the same requirement, the one that had introduced the slot. When every workaround cites the same requirement as its justification, the requirement is what needs a second look.
Don’t file the bug and write the fix in the same commit. I did the diagnosis and the design in one sitting, which meant nobody, including me, had a chance to read the diagnosis on its own and notice that it was describing a different problem than the fix was solving. Separating those two steps costs almost nothing and would have given the design a chance to be wrong in public before it was wrong in production.
The mechanism is gone now, the replacement is simpler, and the queue no longer cares whether one machine’s container runtime is having a bad day. I’d just rather have gotten there on day one than on day four, with three fewer patches along the way.