Skip to content

time: Timer with zero Duration seems to have a moment of delay #10124

@binary132

Description

@binary132

http://play.golang.org/p/9saBM6ph-9

There seems to be a small delay incurred when Timer starts. Note the "sync" workaround in notBroken. This behavior was noticed on go1.2.1 linux/amd64 and is repeatable on play.golang.org and go1.4.1 linux/amd64.

The following program outputs:

This is the undesired behavior.

This is the desired select.
package main

import "time"

func main() {
    broken()
    println()
    notBroken()
}

func broken() {
    t := time.NewTimer(0 * time.Second)

    select {
    case _ = <-t.C:
        println("This is the desired select.")
    default:
        println("This is the undesired behavior.")
    }
}

func notBroken() {
    t := time.NewTimer(0 * time.Second)

    sync := time.NewTimer(0 * time.Second)
    _ = <-sync.C

    select {
    case _ = <-t.C:
        println("This is the desired select.")
    default:
        println("This does not get selected.")
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions