diff --git a/daemon/command/metrics.go b/daemon/command/metrics.go index f658cea13a..4a2dfb9f4b 100644 --- a/daemon/command/metrics.go +++ b/daemon/command/metrics.go @@ -2,9 +2,9 @@ package command import ( "context" + "errors" "net" "net/http" - "strings" "time" "github.com/containerd/log" @@ -30,7 +30,7 @@ func startMetricsServer(addr string) error { Handler: mux, ReadHeaderTimeout: 5 * time.Minute, // "G112: Potential Slowloris Attack (gosec)"; not a real concern for our use, so setting a long timeout. } - if err := srv.Serve(l); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { + if err := srv.Serve(l); err != nil && !errors.Is(err, net.ErrClosed) { log.G(context.TODO()).WithError(err).Error("error serving metrics API") } }() diff --git a/daemon/internal/libcontainerd/remote/client_io_windows.go b/daemon/internal/libcontainerd/remote/client_io_windows.go index b74a33d3b6..67ff823ad0 100644 --- a/daemon/internal/libcontainerd/remote/client_io_windows.go +++ b/daemon/internal/libcontainerd/remote/client_io_windows.go @@ -23,7 +23,7 @@ func (dc *delayedConnection) Write(p []byte) (int, error) { if dc.con != nil { return dc.con.Write(p) } - return 0, errors.New("use of closed network connection") + return 0, net.ErrClosed } func (dc *delayedConnection) Read(p []byte) (int, error) { @@ -31,7 +31,7 @@ func (dc *delayedConnection) Read(p []byte) (int, error) { if dc.con != nil { return dc.con.Read(p) } - return 0, errors.New("use of closed network connection") + return 0, net.ErrClosed } func (dc *delayedConnection) unblockConnectionWaiters() { diff --git a/daemon/internal/metrics/plugin_unix.go b/daemon/internal/metrics/plugin_unix.go index bde1a3f860..4582982d03 100644 --- a/daemon/internal/metrics/plugin_unix.go +++ b/daemon/internal/metrics/plugin_unix.go @@ -7,7 +7,6 @@ import ( "net" "net/http" "os" - "strings" "sync" "time" @@ -123,7 +122,7 @@ func listen(path string) error { Handler: mux, ReadHeaderTimeout: 5 * time.Minute, // "G112: Potential Slowloris Attack (gosec)"; not a real concern for our use, so setting a long timeout. } - if err := srv.Serve(l); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { + if err := srv.Serve(l); err != nil && !errors.Is(err, net.ErrClosed) { log.G(context.TODO()).WithError(err).Error("error serving metrics API") } }() diff --git a/integration-cli/requirements_test.go b/integration-cli/requirements_test.go index de861bced7..f97a3c791e 100644 --- a/integration-cli/requirements_test.go +++ b/integration-cli/requirements_test.go @@ -2,7 +2,9 @@ package main import ( "context" + "errors" "fmt" + "net" "net/http" "os" "os/exec" @@ -67,7 +69,7 @@ func Network() bool { } resp, err := c.Get(url) - if err != nil && strings.Contains(err.Error(), "use of closed network connection") { + if err != nil && !errors.Is(err, net.ErrClosed) { panic(fmt.Sprintf("Timeout for GET request on %s", url)) } if resp != nil { diff --git a/integration/internal/requirement/requirement.go b/integration/internal/requirement/requirement.go index f0dfca3beb..b2c583355c 100644 --- a/integration/internal/requirement/requirement.go +++ b/integration/internal/requirement/requirement.go @@ -1,8 +1,9 @@ package requirement import ( + "errors" + "net" "net/http" - "strings" "testing" "time" ) @@ -17,7 +18,7 @@ func HasHubConnectivity(t *testing.T) bool { client := http.Client{Timeout: timeout} resp, err := client.Get(url) - if err != nil && strings.Contains(err.Error(), "use of closed network connection") { + if errors.Is(err, net.ErrClosed) { t.Fatalf("Timeout for GET request on %s", url) } if resp != nil {