check for net.ErrClosed instead of "use of closed network connection"

The infamous "use of closed network connection" error was added in
[cl-5649076] as a non-exported error. This made it not possible to
write code to handle it as a sentinel error, other than through string-
matching.

Commit [moby@cc851db] (docker v0.6.4) added a [`IsClosedError`] utility
for this (as [net.errClosing@go1.1.2] did not yet export this error).
The `IsClosedError` was later moved to the `go-connections` module, but
various other places in our code used similar matching.

There was a feature-request [go-4373] to export it, which
got accepted and implemented in [CL 5649076], so starting with go1.16
we now have [net.ErrClosed@go1.16], so can remove the string matching.

[CL 5649076]: https://golang.org/cl/5649076
[moby@cc851db]: cc851dbb3f
[`IsClosedError`]: cc851dbb3f/utils/utils.go (L1032-L1040)
[net.errClosing@go1.1.2]: https://github.com/golang/go/blob/go1.1.2/src/pkg/net/net.go#L341
[go-4373]: https://github.com/golang/go/issues/4373
[net.ErrClosed@go1.16]: https://github.com/golang/go/blob/go1.16/src/net/net.go#L636-L645

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-08 09:57:44 +02:00
parent 5a30aa3670
commit 3862a0875c
5 changed files with 11 additions and 9 deletions

View File

@@ -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")
}
}()

View File

@@ -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() {

View File

@@ -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")
}
}()

View File

@@ -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 {

View File

@@ -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 {