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

context: WithCancel should return a cancel function which can accept an error #34764

Closed
fanfuxiaoran opened this issue Oct 8, 2019 · 6 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@fanfuxiaoran
Copy link

Sometimes, we run multiple go routines in a context, if one go routine fails due to an error, we
want to cancel the context and record the error in it. Such as a task has several workers, each worker works independently, if a worker fails , the task is supposed to fail and report the cause.
I want to cancel the context if a worker fails due to an error, and record the error in the context.
However, the CancelFunc returned by WithCancel can not accept an error. I think a new type of
cancel function is needed which user can pass an error into it and can be returned by WithCancel.

@fanfuxiaoran fanfuxiaoran changed the title Need WithCancel can return a cancel function which can accept an error WithCancel should return a cancel function which can accept an error Oct 8, 2019
@mvdan
Copy link
Member

mvdan commented Oct 8, 2019

WithCancel can't be changed without breaking backwards compatibility. Likewise, we can't change CancelFunc. What exactly are you proposing?

@fanfuxiaoran
Copy link
Author

fanfuxiaoran commented Oct 8, 2019

WithCancel can't be changed without breaking backwards compatibility. Likewise, we can't change CancelFunc. What exactly are you proposing?

ref: #34761

@dmitshur dmitshur changed the title WithCancel should return a cancel function which can accept an error context: WithCancel should return a cancel function which can accept an error Oct 8, 2019
@bcmills
Copy link
Contributor

bcmills commented Oct 8, 2019

Note that it's fairly trivial to write your own wrapper around context.WithCancel that substitutes a different error: https://play.golang.org/p/X1hpcV2OC7i

@bcmills
Copy link
Contributor

bcmills commented Oct 8, 2019

Such as a task has several workers, each worker works independently, if a worker fails , the task is supposed to fail and report the cause.

That is exactly what an errgroup.Group does. There is typically no need to inject the error back into the controlling Context: the Context is an input to the tasks, but the error is an output.

@fanfuxiaoran
Copy link
Author

Such as a task has several workers, each worker works independently, if a worker fails , the task is supposed to fail and report the cause.

That is exactly what an errgroup.Group does. There is typically no need to inject the error back into the controlling Context: the Context is an input to the tasks, but the error is an output.

Great! Thanks for your help.

@ianlancetaylor
Copy link
Contributor

Is there anything else to do for this issue?

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 9, 2019
@golang golang locked and limited conversation to collaborators Oct 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants