Skip to content
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

Avoid crash when running task with (*task).run == nil #31

Merged
merged 1 commit into from
Nov 8, 2021

Conversation

tklauser
Copy link
Member

@tklauser tklauser commented Nov 8, 2021

The following program currently leads to a crash due to nil pointer
dereference:

wp := workerpool.New(runtime.NumCPU())
defer wp.Close()
if err := wp.Submit("foobar", nil); err != nil {
	panic(err)
}
tasks, err := wp.Drain()

Fix this by checking (*task).run before running it. This way we keep the
semantics of returning a result for every submitted task. The
alternative solution would be to not enqueue the task on
(*WorkerPool).Submit("foobar", nil) and return either nil or an error.
Both would lead to an imbalance between bumber of submitted tasks and
returned results.

The following program currently leads to a crash due to nil pointer
dereference:

	wp := workerpool.New(runtime.NumCPU())
	defer wp.Close()
	if err := wp.Submit("foobar", nil); err != nil {
		panic(err)
	}
	tasks, err := wp.Drain()

Fix this by checking (*task).run before running it. This way we keep the
semantics of returning a result for every submitted task. The
alternative solution would be to not enqueue the task on
(*WorkerPool).Submit("foobar", nil) and return either nil or an error.
Both would lead to an imbalance between bumber of submitted tasks and
returned results.

Signed-off-by: Tobias Klauser <[email protected]>
@tklauser tklauser requested review from kaworu and rolinh November 8, 2021 10:35
Copy link
Member

@rolinh rolinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

Fix this by checking (*task).run before running it. This way we keep the
semantics of returning a result for every submitted task.

I like this approach. Running a nil task is the best kind of task to process: not resource intensive and guaranteed to succeed 😄

@tklauser tklauser merged commit 14aa35f into master Nov 8, 2021
@tklauser tklauser deleted the pr/tklauser/submit-nil branch November 8, 2021 10:41
@rolinh rolinh added the kind/enhancement This would improve or streamline existing functionality. label Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement This would improve or streamline existing functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants