Founder Notes
It Only Ever Left By Raising
I had a process that was meant to stay connected all day. It opened a connection, listened for messages, and when the connection dropped it waited a moment and tried again. Each failed attempt doubled the wait, which is ordinary. Once a connection came back, the wait was supposed to drop back to short, so that the next drop, hours later, would be met quickly.
The waits in the log kept getting longer over the life of the process, even across long healthy stretches. I had a ready explanation: the wait was resetting on every good connection, exactly as designed. I was wrong, and the code said so the moment I read it instead of reasoning about it.
The reset sat on the line directly after the call that listened. That looks right. You connect, you listen, and when listening is over you know the connection was good, so you reset. The trouble is how listening ends. That call never comes back with an answer. It waits for as long as the connection is up, and its only way out is an exception, when the connection drops. So the line after it was not skipped now and then. It was on a path the program had no way to take. Every drop went straight to the handler, the wait grew, and nothing ever shrank it. A drop late in a long day meant a long silence before the process tried again.
What interests me is that nothing about the line itself was wrong. It said the right thing, in the right order, in the place a reader expects to find it. Read top to bottom, the loop is a correct description of what should happen. What it leaves out is the one fact that decides whether that description is ever carried out: the ways the call above it is able to end. That fact is not written in the loop. It lives inside the called function, and you only see it by going and asking.
I think we read code as a sequence, and a sequence implies arrival. A statement under another statement looks like the thing that happens next. That is true only when the statement above it can finish normally. When it cannot, the order on the page is a story about a path, not the path.
The right home for a reset like that is a point the program actually passes through, such as the moment a connection is made, not the far side of an event that never happens. The habit it left me with is bigger than the line. When I read the recovery half of a loop now, I first ask how the call before it ends: by returning, by raising, or by never ending at all. Only one of those reaches the next line.
The general form: a line after a call runs only when that call returns. If its only exit is an exception, everything written after it belongs to a path the program cannot take, however natural it looks on the page. Before relying on what follows a call, find out how the call is able to stop.