Quantefakt

· 5 min

Flaky tests don't exist. Five red CI runs and one leaked thread.

Five CI runs in a row went red, each time on a different timing test and only on some operating systems. A fresh agent with none of the earlier reasoning read the logs and found the cause, a test that left a thread spinning, and CI went from 2.5 hours back to 58 minutes.


A test that fails in some runs and passes in others is called flaky. The usual answer is to run it again, and if it passes, to move on. I don’t accept that. A test that sometimes fails has a bug, in the code or in the test. Last week gave me a clean example of why.

The symptom

Five CI runs in a row went red. CI, continuous integration, is the build that runs the whole test suite on every commit, here on Linux, macOS and Windows. Each run failed on a different test, and every one of them was a timing test: a check that something answers within a time limit. They failed only on some operating systems, never the same test twice.

At the same time the suite had become slow. A run used to take about an hour. Since 10 November it took about 2.5 hours. The time limit for a run had been raised to 240 minutes, so the slow runs still finished.

That is what a flaky test looks like from the outside: red, then green, then red somewhere else, and no pattern you can point at.

Three causes, not luck

When a test fails now and then, it is usually one of three things:

  • A refactoring the test expects, but the code never got. The test describes the new behavior and the code still does the old one on some paths.
  • A stale test. The code changed on purpose and the test still checks what used to be right.
  • A concurrency or timing problem. About 4,000 tests run on 20 cores. The order they run in changes, and a test that depends on what ran before it passes or fails depending on that order.

Someone has to find out which one it is. Rerunning the suite finds nothing, and raising the time limit hides it.

A fresh agent, with none of the reasoning

The agent that had written the recent code had its own story about these failures. I didn’t ask it. I started a fresh agent and gave it only the task, the IDs of the five failed runs and where the evidence was: the run logs, the step timings, the git history and the code. It got none of the earlier reasoning.

It had three questions to answer: why a run now takes 2.5 hours instead of one, the root cause of every failure with the log lines and code locations as evidence, and the fix for each cause, or “not determined” plus the one observation that would settle it. It was not allowed to start, rerun or cancel a CI run, or to edit anything.

It found the cause from the logs of the runs that already existed. No new run was needed.

What it found

One test of the debugger connection never closed its session. The thread it left behind asked “has the program stopped?” over and over, with no pause, for the rest of the run. Every test after it ran on a machine that was busy with that thread. The tests got slower and slower until memory ran out, which is the out-of-memory error in the macOS logs.

On the slower machines, that was enough to push a timing test over its limit. Which timing test failed depended on which one happened to run when the machine was slowest. That is why every red run named a different test, and why none of those tests was actually broken.

One CI run, tests in the order they run. A debugger test passes but leaves a thread running that never stops. The tests after it pass, slower and slower. Then a timing test fails as too slow, and the run goes on until memory runs out. In each of five runs a different timing test turned red; a rerun would only have picked the next victim. The fix: the debugger test stops its thread, and the CI run is back from 2.5 hours to 58 minutes.

It started with the debugger commits of 10 November, the same day the run time went from one hour to 2.5. The raised time limit absorbed the slowdown, so the runs got longer instead of failing, and the cause stayed hidden.

The fix

  • The test now closes its session after every test.
  • The product. The leaked thread exposed a real bug: the debugger connection kept asking when the answer said neither that the program had stopped nor that it was still running. It now stops in that case, and a new test proves it. With the guard taken out, the new test failed three runs out of three.
  • The two timing tests that failed on macOS and Windows got their own fixes. One waited a fixed 300 milliseconds for retries and now waits with a deadline. The other has a two-second limit for asking other local servers, and each ask now ends at its own limit, so the total holds on every system.
  • The time limit is back to 100 minutes. The slowest healthy run takes 58, so a slowdown like this one now fails the run instead of stretching it.

After the fix, CI was green on Linux, macOS and Windows for the first time since 10 November: 40 minutes on Linux, 50 on macOS, 58 on Windows.

What is still not proven

Two things stayed open, and the audit said so instead of guessing:

  • Why a single ask to another local server took about twice its limit on macOS and Windows is still unknown. The fix caps it, so it can no longer overrun, but the cause was not found.
  • The link between the leaked thread and one failure of the retry test on macOS has only medium confidence.

What I take from it

The failing tests were all innocent. The guilty test passed every time. If I had rerun the red runs until they were green, or raised the time limit again, the thread would still be spinning, the suite would still take 2.5 hours, and the product bug would still be in the debugger connection.

A test that fails now and then is a message. Someone has to read it, and it helps when that someone has no reasoning of its own to defend.

← All posts