>From https://godoc.org/context
Failing to call the CancelFunc leaks the child and its children until the > parent is canceled or the timer fires. The go vet tool checks that > CancelFuncs are used on all control-flow paths. I'm not sure how it's *meant* to be used, but a way to do it, while adhering to everything you say, is to use defer cancel() In that case, a) cancel will always be called at least once, b) if you want to cancel it before, you can. Just call cancel and c) if you call ctx.Err() (e.g. in a return statement) it will return Cancelled iff you called cancel before in the same function. So, yes. If you get a cancel function, always make sure to call it (you might use go vet for that) at least once and a simple way to ensure that would be defer. On Wed, Nov 2, 2016 at 1:04 PM, Ian Davis <m...@iandavis.com> wrote: > Hi all, > > I'm trying to understand the idioms around cancellation of contexts. > I've read the godoc and the relevant blog > (https://blog.golang.org/context). > > Should you always call the cancelFunc of a cancellable context? Or > should it only be called if the operation is terminated before > successful completion? The godoc says: > > "Canceling this context releases resources associated with it, so code > should call cancel as soon as the operations running in this Context > complete." > > This implies to me that it must always be called. > > If so, then how can the Err function report the reason for the > termination of the operation? The godoc says > > "Err returns Canceled if the context was canceled" > > So if cancel must always be called to release resources, Err will always > return a value of Canceled. > > Any thoughts on this? > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.