瀏覽代碼

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 <kolyshkin@gmail.com>
Kir Kolyshkin 7 年之前
父節點
當前提交
05e2f7e2fa
共有 2 個文件被更改,包括 4 次插入2 次删除
  1. 2 1
      daemon/cluster/swarm.go
  2. 2 1
      pkg/ioutils/readers_test.go

+ 2 - 1
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()

+ 2 - 1
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