I logged the bug instead of fixing it: how my reconnect replay turned into a retry storm
A few weeks ago I wrote the code that re-sends buffered session events when an agent reconnects to our controller. It had a bug that should have been obvious from two numbers sitting in the same commit, and I wrote a warning log line for the failure instead of preventing it. After a routine controller redeploy, one machine running a few dozen sessions logged thousands of dropped frames in about ten minutes and nearly thirteen thousand in a day. It reconnected over and over, and the scheduler treated it as offline while it was connected and doing work.
This post covers what I built, why it failed, the part of my first diagnosis that was wrong, and what I’m changing.
What I built
Each agent keeps a local journal of session events. An event is written to the journal before it goes into the outbound queue, and it is deleted when the controller acknowledges it. If the connection drops, nothing is lost: on reconnect the agent replays whatever the controller hasn’t acknowledged.
The replay looked roughly like this:
for record in journal.records() {
if !outbox.send(record.event.clone()) {
warn!("the outbound queue dropped a frame to make room for a replayed event");
}
}
It’s one loop over the whole backlog, with no limit and no waiting for acknowledgements.
Why that was broken
I chose these two limits in the same change:
- The journal holds up to 8,192 records.
- The outbound queue for bulk traffic, which includes session events, holds 256 frames. When it’s full it drops the oldest frame to make room for the newest. That’s deliberate and correct for live output.
So the replay poured up to 32 times more records into the queue than it could hold. The newest 256 got sent. The rest were dropped.
The journal’s contract made this worse. A dropped frame is never sent, so it’s never acknowledged, so its record stays in the journal and gets replayed on the next connection. Each reconnect re-sent the whole backlog, dropped most of it, and kept all of it for next time. The design guaranteed it would repeat.
How a restart became an outage
- The controller restarts. Every agent reconnects holding every event the old controller hadn’t acknowledged. On a busy machine that’s thousands.
- Each agent fills its queue, drops most of the backlog, and sends the surviving frames as fast as the socket allows.
- The controller stores events through one serialized database writer, shared by every reconnecting machine at once. A connection’s recording backlog fills up, and the controller stops reading from that socket until the store catches up.
- While it isn’t reading, the controller sees no frames from that agent, so its periodic health checks count as unanswered. After three in a row it marks the machine offline and closes the socket.
- The agent reconnects and step 2 starts again with the same backlog.
Meanwhile, other requests that went through the same writer timed out, and the scheduler refused to send work to machines it believed were offline.
A small number of events made it permanent. They contained a NUL byte from binary data in tool output, and our database refuses to store that in a text column. The controller logged the failure and never acknowledged those events, so the agents replayed them on every connection with no end in sight.
Correcting my first diagnosis
My first write-up said the dropped frames included the agent’s answers to health checks and its status reports, and that this was why the machine went offline. The code says otherwise. Check answers are written straight to the socket, ahead of the queue. Status reports are held in dedicated slots and are never dropped. The machine went offline because the controller stopped reading, not because the agent dropped its answers.
The fix below still addresses the flood. But I put a wrong mechanism in writing and didn’t reconcile it with the code, and that’s a mistake too. A wrong explanation leads people to the wrong fix next time.
What’s fixed, and what isn’t
- Paced replay. The agent now sends the journal through a window of 64 records ahead of the acknowledgements, half the queue’s high-water mark. A record with no acknowledgement after 30 seconds gives up its place and waits for the next connection. Acknowledgements are processed in batches. An end-to-end test replays 1,024 events against a fake controller. With the window removed, the test fails with exactly 256 delivered and the rest dropped.
- Design docs. The design now says that session events are journaled, replayed in order a window at a time, and that the controller must record every event, escaping NUL bytes. Before this, our design docs didn’t mention the journal at all.
- Still open: escaping NUL bytes on the controller. Until that lands, the few unstorable events are still re-sent on each connection, but the window and the timeout now bound them.
- Not changed yet: the single shared writer, and the health check counting the controller’s own stalled reads as the agent being silent. A slow store can still mark a healthy machine offline.
What I should have done
I should have paced the replay from the start. Draining an 8,192-record backlog into a 256-frame queue was a size mismatch I could see in my own constants. The fix I shipped afterward is what the first version should have been.
I shouldn’t have logged a failure I knew would happen. I wrote that warning in the original change, so I knew drops could happen. Logging a drop is fine when the dropped thing is gone for good. Here the dropped record was kept and re-sent by design, so every log line marked work that would come back. If you can predict a failure well enough to write its log message, you can usually prevent it.
I should have put it in the design. Anything that re-sends on reconnect affects the whole fleet during a controller restart, and we treat restarts as routine. I left the journal as an implementation detail, so no written requirement limited the backlog or said what happens to an event that can’t be stored.
I should have thought through the receiving side. If the sender keeps a message until it’s acknowledged, the receiver can’t leave a message neither stored nor acknowledged. If it can’t store an event, it has to say so, either by acknowledging it as rejected or by fixing the data. Otherwise the sender retries forever. And a server that knows it stopped reading shouldn’t blame the client for going quiet. Our controller already skips its silence timeout when its own backlog is full, but not the health-check count.
Review checks I’m adding
- Flag any
sendorpushinto a bounded queue inside an unbounded loop over a stored backlog, especially when overflow is logged instead of pushed back on the producer. - Flag any handler for an incoming message whose error branch logs and returns without acknowledging, when the sender keeps the message until it’s acknowledged.
- When a change adds a retry or replay mechanism, ask what happens when every client reconnects at the same moment. If the design doc doesn’t answer that, the change isn’t done.
The bug was one loop. Logging the drop instead of preventing it is what made it last, and I won’t do that again.