> ## Content Index
> Fetch the complete content index at: https://debugly.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# Why Go’s time.After Loop Keeps Postponing the Timeout
- URL: https://debugly.dev/go-time-after-select-loop-timeout/
- Published: 2026-11-14T09:54:00.000Z
- Updated: 2026-11-14T09:54:00.000Z
- Description: A fresh time.After call on every select iteration renews the wait. Keep one timer outside the loop for an overall budget, and check what it cannot interrupt.
- Author: Rohit Bhadani
- Tags: Backend, Concurrency, Debugging

The worker keeps receiving messages, but its five-second timeout never ends the loop. If the select case calls `time.After()` inside that loop, check the timer’s lifetime before changing the duration. Each iteration creates a fresh timer. A steady stream of messages can keep winning before the new wait expires. For one overall waiting budget, create the timer once outside the loop, or use an existing context deadline.

*This is an AI-prepared, documentation-based explainer, not a personally reproduced incident. The Go fragments are illustrative and were not executed for this article. See the* [*editorial policy*](https://debugly.dev/editorial-policy/)*.*

## The timeout moved when the loop moved

Imagine a worker waiting for enough events to finish an operation. It should give up after five seconds, even if the events keep arriving. The loop looks sensible: handle an event, or return when a timer fires. Under light traffic it times out; under steady traffic it keeps going. That’s a useful clue. The timer hasn’t developed a fondness for busy servers. Its starting line keeps getting replaced.

This illustrative fragment assumes an `events` channel, a `handle` function and the `time` package:

```
for {
    select {
    case event, ok := <-events:
        if !ok {
            return
        }
        handle(event)
    case <-time.After(5 * time.Second):
        return
    }
}
```

[time.After](https://pkg.go.dev/time?ref=debugly.dev#After) creates a timer and returns its channel. The [Go select rules](https://go.dev/ref/spec?ref=debugly.dev#Select%5Fstatements) evaluate receive-channel operands when entering the select. So the call runs again on every loop iteration, including iterations where an event is ready.

When an event wins, the loop eventually starts another select with another timer channel. The previous timer may expire, but the next iteration isn’t listening to that channel. If every iteration gets an event before its new timer becomes ready, the timeout branch never runs. Counting from when the worker started and counting from the latest select entry are different budgets.

## Decide whether activity should buy more time

The per-iteration pattern can be intentional if you want a wait between handled events. But even then, be precise: in this fragment the next timer starts after `handle` returns. It doesn't measure all time since a message arrived on the network. A slow handler can create a gap your wait timer never observes.

For an overall wait budget, move ownership outside the repeated select. This illustrative replacement keeps the same timer channel across event handling:

```
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()

for {
    select {
    case event, ok := <-events:
        if !ok {
            return
        }
        handle(event)
    case <-timer.C:
        return
    }
}
```

Now a handled event doesn’t create another five seconds. `Stop` gives the timer an explicit cleanup point when the function returns early. If the caller already supplied a context deadline, propagate that budget and select on `ctx.Done()` instead of quietly starting a new one. [context.WithTimeout](https://pkg.go.dev/context?ref=debugly.dev#WithTimeout) can establish a budget where the operation is owned; release its resources by calling its cancel function.

## A ready timer doesn’t interrupt your handler

Moving the timer fixes the replacement problem. It doesn’t guarantee that the function returns at an exact wall-clock instant. While `handle` is running, this goroutine isn't selecting. If that function blocks, the timer can become ready without preempting it.

There’s another boundary: if an event and the timer are both ready, select doesn’t give the timeout case priority merely because it appears last or first. The language specifies a pseudo-random choice among cases that can proceed. If it’s unacceptable to start another unit of work after the deadline, check the operation’s deadline policy before starting it and make the work cooperate with cancellation. Don’t turn a timer notification into a claim that arbitrary work can be forcibly interrupted.

For an idle policy that deliberately restarts a reusable timer, follow the [Timer.Reset contract](https://pkg.go.dev/time?ref=debugly.dev#Timer.Reset) for the Go behavior your program uses. Resetting one timer is a separate design from replacing it with a new timer on every loop. Keep that distinction explicit in the code review.

## The old timer-leak advice answers a different question

You may find older advice that focuses entirely on memory retained by repeated `time.After` calls. [Go 1.23 changed timer behavior](https://go.dev/doc/go1.23?ref=debugly.dev) so unreferenced, unstopped channel timers can be collected. The [timer migration guide](https://go.dev/wiki/Go123Timer?ref=debugly.dev) explains the version and compatibility controls involved. Check the actual deployed toolchain and compatibility settings before applying an older memory diagnosis.

That change doesn't turn many fresh waiting periods into one deadline. Nor does garbage collection mean timer allocation is free. Separate three questions: is the code renewing its wait, are timer objects retained, and is allocation cost significant in this workload? Use measurements from the deployed program for the last two. This article supplies no benchmark or heap measurement.

## Test the two budgets separately

In a controlled test, cover no events, regular events that arrive before each fresh wait expires, channel closure, and a handler that runs beyond the intended budget. Assert whether the operation ends under continuing traffic, rather than checking only that a silent channel eventually times out. Give timing tests reasonable scheduling tolerance; they shouldn't depend on one exact nanosecond race.

Also test the caller’s deadline path and what the handler does when cancellation becomes observable. Debugly’s [WithoutCancel deadline explainer](https://debugly.dev/go-context-withoutcancel-lost-deadline/) covers a different way a Go call can lose its inherited budget. Here the deadline wasn't stripped. It was never one shared deadline in the first place.