From 05e2f7e2fafd0fbc818c9f4cda7ac513c785d49c Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 19 Apr 2018 15:51:35 -0700 Subject: [PATCH] context.WithTimeout: do call the cancel func govet complains (when using standard "context" package): > the cancel function returned by context.WithTimeout should be called, > not discarded, to avoid a context leak (vet) Signed-off-by: Kir Kolyshkin --- daemon/cluster/swarm.go | 3 ++- pkg/ioutils/readers_test.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/daemon/cluster/swarm.go b/daemon/cluster/swarm.go index 5deb8a024d..2a97207f33 100644 --- a/daemon/cluster/swarm.go +++ b/daemon/cluster/swarm.go @@ -504,7 +504,8 @@ func validateAddr(addr string) (string, error) { } func initClusterSpec(node *swarmnode.Node, spec types.Spec) error { - ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() for conn := range node.ListenControlSocket(ctx) { if ctx.Err() != nil { return ctx.Err() diff --git a/pkg/ioutils/readers_test.go b/pkg/ioutils/readers_test.go index e009ab26f6..a32e661210 100644 --- a/pkg/ioutils/readers_test.go +++ b/pkg/ioutils/readers_test.go @@ -80,7 +80,8 @@ func (p *perpetualReader) Read(buf []byte) (n int, err error) { } func TestCancelReadCloser(t *testing.T) { - ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + defer cancel() cancelReadCloser := NewCancelReadCloser(ctx, ioutil.NopCloser(&perpetualReader{})) for { var buf [128]byte