Flaky Godot headless test? Wait for state
A Godot integration test failed its reconnect checks about one run in six. The cause was a reference snapshot taken a frame too early, and the fix was a wait for state, not a sleep.
Contents
We have a headless Godot test that plays a real multi-round run against a local backend and a
local language model. It has 23 checks. Two of them, both about reconnecting, started failing
on one branch: 2 runs out of 3. Then we ran the same test on older main and it failed there
too, 1 run out of 3.
This post is how we found the cause, what the fix looks like, and the rule we took from it: when a test needs "the state after X", wait for that state. Do not sleep and do not retry.
First guess: the new files
The branch had added a handful of new files, so we blamed them. We moved them out, re-ran, and got 23/23. That looked like proof.
It was luck. On a clean checkout of main the test had passed 23/23 once, and then failed on
a later run. A check that fails on code you did not touch is a flaky check, and a single green
run after removing something proves nothing. We needed a trace.
Trace the client, not the backend
The backend logs were the same in passing and failing runs. So we made a temporary, instrumented copy of the test that logged every protocol event with the frame it was applied in, and ran it six times. One run failed. That was enough, because the failing run showed the sequence:
- Events 31, 32 and 33 (the reveal and both agent declarations) were applied in frame 2360.
- The test recorded its "reference" state in frame 2361. At that point the client said round 2.
- Event 34, the start of round 3, was not applied before the reference was taken. It only reached the client later, inside the snapshot sent after the reconnect.
In passing runs, events 31 to 34 all landed in the same frame.
What the test was doing
The reconnect test works like this. Play until a round is revealed. Record the current round and scoreboard as a reference. Drop the connection on purpose. Reconnect. Compare the restored state with the reference.
The test treated "the reveal arrived" as "the round is over, the state is settled". But the backend sends the reveal, the declarations and the next round's start back to back. The Godot client applies whatever has arrived each frame. Usually the whole burst is in one frame. Sometimes the last event comes a frame later.
So when the burst was split, the test recorded round 2 while the backend was already on round 3. After the reconnect, the snapshot correctly restored round 3, and both reconnect checks failed because they compared against an out-of-date reference. The app was right and the test was wrong.
The fix: wait for an explicit state
We did not add a sleep. A sleep is a guess about how long the burst takes, and it fails on a slow machine and wastes time on a fast one. We did not add a retry either, because a retry hides the race instead of removing it.
Before taking the reference, the test now waits until one of two things is true: the next round is open (round N+1, and the Next button is enabled), or the run has finished. In GDScript the shape is:
var round_before := stage.presenter.round
var open_or_done: bool = await _wait_for(func() -> bool:
return stage.run_status == "completed" \
or (stage.presenter.round > round_before \
and not stage.controls.next_button.disabled), 15.0)
_check(open_or_done, "next round open or run finished")
# only now record the reference
var ref_round := stage.presenter.round
The wait is 8 lines in the test file, and nothing else changed. If the condition is never reached within 15 seconds, the wait fails as its own named check. That matters: a hang now reads as "next round never opened", not as a vague reconnect mismatch three steps later. It is also why the suite went from 23 checks to 24.
Did it work
We ran the test 10 times in a row against a live backend, each time with a fresh scratch database. All 10 runs passed all 24 checks. In 3 of those 10 runs, the next-round event did arrive late, so the new wait was doing real work. Without it, those three runs were the ones at risk.
A later batch of 20 runs had one failure that we could not reproduce. It left no evidence, so we do not claim a cause for it. We list it as unexplained and keep running the test.
A second trap: the port that was already taken
While repeating these runs we hit a different problem. Our integration test connects to a port on localhost, and the default was 8765. An old backend from an earlier acceptance run was still alive on that port. The test connected to it without complaint and drove that old run from round 3 to completion. Nothing was saved, because that old build had no storage, so the only sign was a result that did not match the backend we thought we had started.
Two changes came out of it:
- Live runs use a spare port (we use 8768) and a fresh scratch database every time.
- Before every live run, we check that the port is free. If something is listening, the run stops.
A check like that is one line in a shell script, and it removes a whole kind of "the test passed against the wrong thing" result.
The rule
A test that records state after an event is reading from a pipeline with more than one stage. In our case the stages were the backend, the socket, and the client's per-frame apply step. The test's reference point was at the end of the first stage and it assumed it was at the end of the last.
When a test is flaky, three questions find most of these:
- What state does the check assume is final, and who says it is?
- Can that state arrive in more than one step?
- Can I wait for the final state by name, instead of for a duration?
If the answer to the third is yes, the fix is usually short, and it turns a one-in-six failure into one that cannot happen. Ten out of ten is weaker evidence than a trace, but the trace came first here, and the ten runs only confirmed it.