Your code is buggy vs. your computer is too fast
We run into a hard failure during some diagnostics runs that was quite interesting. Here it the relevant code:
The code was meant to serve as a repository of commands so we can see in what order they run into the system. (Yes, we could use a queue, instead, but the code above isn’t what we actually run, it is just enough to reproduce the problem I wanted to talk about). And it failed, because there was already a value with the same key.
That was strange, because we didn’t expect that to happen, obviously. We wrote the following code:
And we got some really interesting results. On my laptop, I get:
Changes: 9388, Idle: 5442
We got widely variant results. Mostly, I believe, it related to the system configuration and the CPU frequency. But the basic problem is that on that particular machine, we were fast enough to be able to run commands faster than the machine could count .
That is a good problem to have, I think. We fixed it by making sure our time source is monotonically increasing, which is good enough for debug code.
I also had a problem when migrating to a faster server. The code was obviously wrong, but worked on slow machines, it was generating Random numbers one digit at time, but for each digit it was creating a new Random() object instead of using the same instance. For some reason there was a rule that when any digits repeats, it calls the function recursively.
When we migrated to a new server, it caused a Stack Overflow.
Look at this answer: https://stackoverflow.com/questions/243351/environment-tickcount-vs-datetime-now/8865560#8865560 Kind of "eyes opening" in terms of choosing proper tool for proper job.