All courses

Sleeps, retries and longer timeouts as 'fixes'

Recognise a race 'fixed' with a sleep, a flaky call wrapped in a retry loop, or a raised timeout. Explain why these pass locally and in CI but fail under load or on slower machines, and what a real ordering fix looks like.

Builds on: Patching the symptom instead of the cause, Race conditions and interleavings

Read it card by card Start the whole course

The flaky test that went green in a minute

You asked the agent to fix a test that fails about one run in ten. A minute later: *"Root cause was timing. Added a short wait before the assertion; 50/50 runs green locally."* CI is green too. Other versions of the same report: *"added a retry around the flaky call"* or *"raised the timeout to 30 s"*. Did any of these fix the problem?

Timing changes the odds, not the order

The agent optimises the proxy signal it can see: a green run. A sleep, retry or longer timeout shifts timing so the bad interleaving gets rarer **on this machine, today**. It creates no ordering. Its green runs happened on a quiet machine. A loaded CI runner, a GC pause or production traffic can make the work take longer than its guess. Later cost: the race ships, fails rarely and can't be reproduced. A sleep also slows every single run.

Tells, and the question that settles it

**Tells** in a fix for an intermittent failure: - a new sleep (`time.sleep`, `Thread.sleep`, `time.Sleep`, `setTimeout`) between starting work and using its result - a retry loop around a failure caused inside your own code - a raised timeout with no reason the work got slower - "passed 50/50 runs" as the evidence **Settle it:** name what must happen before what, then wait on that event: join, channel, future, latch.

A retry hiding a missing wait (Java)

The agent reports: *"Upload-then-fetch was flaky (404). Added 3 retries, 1 s apart, to the fetch endpoint. Tests green."* The upload handler hands the write to an `ExecutorService` and responds before the write runs. Tell: a retry around a failure whose cause is your own ordering. Later cost: when the executor's queue backs up past 3 s, users get 404s again, and each retry adds load. Real fix: wait on the write's `Future` before responding.

Sleep versus waiting on the event (Go)

The agent first tried 10 ms, then 200 ms when CI still failed. The tell: `time.Sleep` sits between starting work and reading its result, and nothing ties 200 ms to the refresh finishing. On a loaded runner the refresh takes 250 ms: red. BETTER waits on the event itself. In Go, closing a channel is ordered before a receive that sees the close. So the refresh's writes are visible to the check.
// Go
// BAD: the agent's fix
c.StartRefresh(ctx)
time.Sleep(200 * time.Millisecond)
if c.Len() != 3 {
	t.Fatalf("len = %d", c.Len())
}

// BETTER
done := c.StartRefresh(ctx) // returns a channel closed when refresh ends
<-done
if c.Len() != 3 {
	t.Fatalf("len = %d", c.Len())
}

Remember

A sleep, retry or longer timeout changes how often the race loses, not whether it can. Ask "what must happen before what, and what signals it?" and make the code wait on that signal. Retries belong around transient failures outside your control, such as a network blip. Timeouts are ceilings on how long to wait. Neither creates ordering.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.