Skip to content

Goroutine leak in timeout #35

@peter-crist

Description

@peter-crist

In timeout.go, context cancellation isn't check for when running the command goroutine. This causes the errc <- next.Run(ctx, f) call to block indefinitely if the command times out.

ctx, _ = context.WithTimeout(ctx, cfg.Timeout)

// Run the command
errc := make(chan error)
go func() {
	errc <- next.Run(ctx, f) //This blocks indefinitely if the context is cancelled due to a timeout.
}()

// Wait until the deadline has been reached or we have a result.
select {
// Finished correctly.
case err := <-errc:
	return err
// Timeout.
case <-ctx.Done():
	metricsRecorder.IncTimeout()
	return errors.ErrTimeout
}

The simple fix is to select:

go func() {
	select {
	case <-ctx.Done():
	case errc <- next.Run(ctx, f):
	}
}()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions