Jaime,

AFAIK, cancellation function is expected to be called regardless of if 
ctx.Done() got triggered. 
So, to fix it, drop a direct call to cancel() and add "defer cancel()" 
right after you got it from the "WithTimeout" call.

Hope it helps,
  Andrey


On Thursday, November 15, 2018 at 8:17:21 AM UTC-7, Jaime Breva wrote:
>
> Hello,
>
> I'm having go vet complain about the cancelFunc on a context.WithTimeout 
> not being called, even though the flow path is already past the context 
> .Done()
> Something like:
>
>  ctx, cancel := context.WithTimeout(parentCtx, timeout)
>
>  select {
>  case <-ctx.Done():
>    //... go vet complains here that this flowpath doesnt call cancel()
>    return nil
>
>  case msg := <-ch:
>    //...
>    cancel()
>    return msg
>  }
>
> Reading context/context.go:
>
> // 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 know in this example I could just defer the call but, should I consider 
> this a false positive? Should I open an issue on github?
>
> Best regards
>

-- 
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.

Reply via email to