From 4333e6a6d6471f4058d46928d4f117a814ed3bc8 Mon Sep 17 00:00:00 2001 From: Daniel Canter Date: Wed, 28 Sep 2022 17:59:11 -0700 Subject: [PATCH] Swap to net.ErrClosed checks for services In Go 1.16 `net.ErrClosed` was exported, removing the need to check the exact text of "use of closed network connection". The stdlib's net listeners are all setup for this to be a reality, but on Windows containerd uses the the go-winio projects named pipe implementation as the listener for services. Before version 0.6.0 this project returned a different error named `ErrPipeListenerClosed` for using a closed pipe, where this error was just an `errors.New` with the same text as `net.ErrClosed`, so checking against `net.ErrClosed` wasn't possible. Starting in 0.6.0 go-winio has that error assigned to `net.ErrClosed` directly so this *should* be alright to finally change. Signed-off-by: Daniel Canter --- cmd/containerd-shim/main_unix.go | 4 ++-- runtime/v2/shim/shim.go | 5 ++--- services/server/server.go | 6 +----- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/cmd/containerd-shim/main_unix.go b/cmd/containerd-shim/main_unix.go index ac6f51c03..c6235f7cf 100644 --- a/cmd/containerd-shim/main_unix.go +++ b/cmd/containerd-shim/main_unix.go @@ -22,6 +22,7 @@ package main import ( "bytes" "context" + "errors" "flag" "fmt" "io" @@ -237,8 +238,7 @@ func serve(ctx context.Context, server *ttrpc.Server, path string) error { logrus.WithField("socket", path).Debug("serving api on unix socket") go func() { defer l.Close() - if err := server.Serve(ctx, l); err != nil && - !strings.Contains(err.Error(), "use of closed network connection") { + if err := server.Serve(ctx, l); err != nil && !errors.Is(err, net.ErrClosed) { logrus.WithError(err).Fatal("containerd-shim: ttrpc server failure") } }() diff --git a/runtime/v2/shim/shim.go b/runtime/v2/shim/shim.go index a6078a569..22339368f 100644 --- a/runtime/v2/shim/shim.go +++ b/runtime/v2/shim/shim.go @@ -22,11 +22,11 @@ import ( "flag" "fmt" "io" + "net" "os" "path/filepath" "runtime" "runtime/debug" - "strings" "time" shimapi "github.com/containerd/containerd/api/runtime/task/v2" @@ -486,8 +486,7 @@ func serve(ctx context.Context, server *ttrpc.Server, signals chan os.Signal, sh } go func() { defer l.Close() - if err := server.Serve(ctx, l); err != nil && - !strings.Contains(err.Error(), "use of closed network connection") { + if err := server.Serve(ctx, l); err != nil && !errors.Is(err, net.ErrClosed) { log.G(ctx).WithError(err).Fatal("containerd-shim: ttrpc server failure") } }() diff --git a/services/server/server.go b/services/server/server.go index d121c4b0b..d607e39d4 100644 --- a/services/server/server.go +++ b/services/server/server.go @@ -30,7 +30,6 @@ import ( "os" "path/filepath" "runtime" - "strings" "sync" "time" @@ -473,10 +472,7 @@ func (pc *proxyClients) getClient(address string) (*grpc.ClientConn, error) { } func trapClosedConnErr(err error) error { - if err == nil { - return nil - } - if strings.Contains(err.Error(), "use of closed network connection") { + if err == nil || errors.Is(err, net.ErrClosed) { return nil } return err