New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: Timer.Reset documentation not accurate for AfterFunc timers #28100

Open
bradfitz opened this Issue Oct 9, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@bradfitz
Member

bradfitz commented Oct 9, 2018

https://golang.org/pkg/time/#Timer.Reset says:

Resetting a timer must take care not to race with the send into t.C that happens when the current timer expires. If a program has already received a value from t.C, the timer is known to have expired, and t.Reset can be used directly. If a program has not yet received a value from t.C, however, the timer must be stopped and—if Stop reports that the timer expired before being stopped—the channel explicitly drained:

And proceeds to give an example of proper usage of Reset.

But a Timer created by AfterFunc never sends on the channel.

We should probably say something about AfterFunc in the Timer.Reset docs.

/cc @dmitshur @ianlancetaylor

@bradfitz bradfitz added this to the Go1.12 milestone Oct 9, 2018

@bradfitz

This comment has been minimized.

Member

bradfitz commented Oct 9, 2018

The code makes it pretty clear a value is never sent on a channel, but here's a playground example:

https://play.golang.org/p/2OSQiHS4Zt5

Code:

func NewTimer(d Duration) *Timer {
        c := make(chan Time, 1)
        t := &Timer{    
                C: c,
                r: runtimeTimer{
                        when: when(d),
                        f:    sendTime,  // <<<<<<<<<<<<<<<<<<
                        arg:  c,
                },                      
        }                       
        startTimer(&t.r)
        return t        
}                       

vs

func AfterFunc(d Duration, f func()) *Timer {
        t := &Timer{
                r: runtimeTimer{
                        when: when(d),
                        f:    goFunc, // <<<<<<<<<<<<<<<<<<
                        arg:  f,
                },
        }       
        startTimer(&t.r)
        return t
}               
@bradfitz

This comment has been minimized.

Member

bradfitz commented Oct 9, 2018

@dmitshur

This comment has been minimized.

Member

dmitshur commented Oct 9, 2018

We should probably say something about AfterFunc in the Timer.Reset docs.

Agreed.

My parsing of the Reset documentation was that the f parameter to AfterFunc being executed is equivalent to a value being received from t.C, and I figured that t.C gets received internally by AfterFunc to call f (or an equivalent mechanism).

However, it'd be great to be explicit and more accurate.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Oct 10, 2018

If Reset returns true, the function passed to AfterFunc will not be called. If Reset returns false, the function has already been called in a separate goroutine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment