use time.NewTimer instead of time.After in Sleep

To fix a potential temporary goroutine leak; see the added comment. The
leaked goroutine would eventually stop once the timer is fired, but
until then, the goroutine could be unnecessarily left around.
This commit is contained in:
Daniel Martí 2019-04-01 17:02:40 +01:00
parent 0d568ec2a4
commit ad8809efb7

View File

@ -47,9 +47,13 @@ func (t Tasks) Do(ctx context.Context, h cdp.Executor) error {
// been able to be written/tested.
func Sleep(d time.Duration) Action {
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
// Don't use time.After, to avoid a temporary goroutine leak if
// ctx is cancelled before the timer fires.
t := time.NewTimer(d)
select {
case <-time.After(d):
case <-t.C:
case <-ctx.Done():
t.Stop()
return ctx.Err()
}
return nil