Skip to content

cmd/commandfuncs: use net.ErrClosed instead of doing substring error checks [requires go 1.16] #3805

Closed
@jimen0

Description

@jimen0

Go 1.16 will introduce a new exported net.ErrClosed (see this issue for context) error allowing Caddy to properly check for this error instead of using the substring matching hack.

Current code:

caddy/cmd/commandfuncs.go

Lines 115 to 123 in 385adf5

go func() {
for {
conn, err := ln.Accept()
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
log.Println(err)
}
break
}

Proposed patch once Caddy starts using go 1.16:

diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index 772fe012..36c686c5 100644
--- a/cmd/commandfuncs.go
+++ b/cmd/commandfuncs.go
@@ -19,6 +19,7 @@ import (
 	"context"
 	"crypto/rand"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -116,7 +117,7 @@ func cmdStart(fl Flags) (int, error) {
 		for {
 			conn, err := ln.Accept()
 			if err != nil {
-				if !strings.Contains(err.Error(), "use of closed network connection") {
+				if !errors.Is(err, net.ErrClosed) {
 					log.Println(err)
 				}
 				break

Please, let me know what do you think about it and, if you would prefer me to open the PR now or if I should wait for 1.16 to be released. Also, please feel free to close this issue if you don't think we need this code change.

Best,
Miguel

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions