Yesterday I encountered my favorite bug on the spectrum of "Fun Bugs to Solve". Here's a few key characteristics:
- It only showed up once or twice roughly every 20 calls or so
- Even in unit testing (with a specified seed), the bug wasn't consistent
- It followed no discernible pattern
That's right ladies and gents.
I had a *checks notes* ~race condition~ strange ordering bug that only showed up during specific business logic!
Some business logic for the Tournament software relied on the order of records returned from ActiveRecord, and 99% of the time the order was what I needed it to be (despite not explicitly calling .order
)
Because it was almost always correct, I didn't notice it for a very long time.
Eventually, as I continuously failed to reproduce it, it dawned on me that it wasn't a race condition, which was my first guess. One 2-hour debugging session later, I realized that the order of my records was wrong a vanishingly infrequent number of times.
The logic surrounding this bug was related to building a Tournament Bracket. When restructuring the bracket (due to adding a new Mat or additional Competitors), I used the position of the Matches in the first round to inform the position and hierarchy of the later rounds. Sometimes that order was something totally different, but 95% of the time, it was correct.
And because this only happened on relatively rare events, it didn't have many opportunities to manifest.
Anyway, at the end of the day I learned a valuable lesson:
Tell your ORM what order you want your records in!
has_many :matchups, -> { order(:round_position) }
I'll soon have a more detailed post up about this exact software I'm building. When I do, it'll be listed in the projects page!