Skip to content

time: Timer.C can still trigger even after Timer.Reset is called #11513

@rogpeppe

Description

@rogpeppe

A common idiom is to keep a single timer and extend its
use by calling Timer.Reset.

From a naive reading of the documentation, these two
lines are equivalent except for saving some garbage:

t.Reset(x)

t := time.NewTimer(x)

Unfortunately t.C is buffered, so if the timer has just expired,
the newly reset timer can actually trigger immediately.

The safe way to do it might be someting like:

t.Stop()
select {
case <-t.C:
default:
}
t.Reset(x)

but this is cumbersome. Perhaps we could change Reset to do this
as a matter of course.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions