The match that froze between hands
One timer, two jobs, and a table that never moved again. From the RUNG game server.
In RUNG, each match is owned by one goroutine, and each session had one timer. That timer ran the turn clock during play, and the short pause between hands before the next deal.
The bug
During the pause, the rules engine still reported a turn: the seat that won the last trick. Nobody could act, but the value was there.
If a player's connection dropped for long enough in that window, the session handed the seat to a bot. If it was also the seat the engine reported, the session scheduled a turn. Arming the timer replaced the pending deal with a turn nobody could take. The next hand was never dealt, nothing armed the timer again, and four players sat looking at a finished hand.
There was no race and no crash. Every line did what it said.
The fix
Only two phases have somebody to wait for, so only those may start a turn:
switch s.engine.Phase() {
case game.PhaseChoosingTrump, game.PhasePlaying:
default:
return // leave the pending timer alone
}
Two other early returns had the same shape: a server move with no legal action, and one the engine rejected. Both left without arming anything. Now both log the fault and settle the match on the score it reached.
Reproducing it
I wrote a small command that plays a real match over a real socket, drops the winner of the last trick during the pause, and waits to see whether the table deals again.
The first version proved little. Its automated players always threw their lowest legal card, so bots won almost every last trick, and the bug needed a human seat to win it. Playing the strongest card fixed that. I also made a run that never reaches the window print INCONCLUSIVE, because it must not look like a pass.
Guarding against the next one
- A watchdog. If the engine hasn't advanced for 90 seconds, longer than any correct path takes, the match is settled and the state is logged as an error.
- A session trace. A freeze leaves nothing in the game's event log, because the engine did nothing wrong. The session now records each timer armed, fired and replaced, with the engine phase.
What I took from it
One goroutine per match removed data races, but not logic races between events. When a single timer has two meanings, replacing it is a decision, and the code should only be able to make it in the right state.
Chasing a bug that only shows up in production?
Email haiderzamanusafxay@gmail.com and tell me what's going on.