In that case replacing the log line with an actual sleep should also work, right? How long? I tries sleeping for 1ms but the bug returned.
Race condition.
Caused by the log line actually waiting on a lock on a common shared resource like the stdout stream. Thus acting like a kind of micro sleep.
Related article for nodejs folks: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick#processnexttick-vs-setimmediate
Discussion
are you working in Go? if so you can pass the -race flag go test/build/run to invoke the race detector
Not necessarily. Does the log write direct or is there flushing, batched kernel io, etc? (Just pulling shit out of thin air without knowing context)
That is to say; *any* work can affect a race condition outcome. But that work may not be happening where you think it is, for all sorts of reasons.
Hard to make suggestions with no context but generated code and strace are good tools. Tricky if working within many layers of abstraction.