Skip to content

Commit

Permalink
use time.NewTimer instead of time.After in Sleep
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvdan committed Apr 1, 2019
1 parent 0d568ec commit ad8809e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad8809e

Please sign in to comment.