diff options
author | Forest Johnson <[email protected]> | 2022-01-19 19:26:44 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2022-01-19 12:26:44 -0700 |
commit | b3f7ce34b4209c13186c8c9626ffb9316fb5603a (patch) | |
tree | 019456447921a2856bae4b9c23172d21989ab16d /listeners.go | |
parent | a79b4055e56dc4e2f2caaae9aea555d1be471948 (diff) | |
download | caddy-b3f7ce34b4209c13186c8c9626ffb9316fb5603a.tar.gz caddy-b3f7ce34b4209c13186c8c9626ffb9316fb5603a.zip |
More explanatory error message from Listen (#4534)
* explain cryptic unix socket listener error related to process kill
https://github.com/caddyserver/caddy/pull/4533
* less ambiguous wording: clean up -> delete
* shorten error message explanation
* link back to pull request in comment for later archeaology
Diffstat (limited to 'listeners.go')
-rw-r--r-- | listeners.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/listeners.go b/listeners.go index 15ffbbeef..c23ff783b 100644 --- a/listeners.go +++ b/listeners.go @@ -17,6 +17,7 @@ package caddy import ( "fmt" "net" + "os" "strconv" "strings" "sync" @@ -39,6 +40,10 @@ func Listen(network, addr string) (net.Listener, error) { sharedLn, _, err := listenerPool.LoadOrNew(lnKey, func() (Destructor, error) { ln, err := net.Listen(network, addr) if err != nil { + // https://github.com/caddyserver/caddy/pull/4534 + if isUnixNetwork(network) && isListenBindAddressAlreadyInUseError(err) { + return nil, fmt.Errorf("%w: this can happen if Caddy was forcefully killed", err) + } return nil, err } return &sharedListener{Listener: ln, key: lnKey}, nil @@ -59,6 +64,10 @@ func ListenPacket(network, addr string) (net.PacketConn, error) { sharedPc, _, err := listenerPool.LoadOrNew(lnKey, func() (Destructor, error) { pc, err := net.ListenPacket(network, addr) if err != nil { + // https://github.com/caddyserver/caddy/pull/4534 + if isUnixNetwork(network) && isListenBindAddressAlreadyInUseError(err) { + return nil, fmt.Errorf("%w: this can happen if Caddy was forcefully killed", err) + } return nil, err } return &sharedPacketConn{PacketConn: pc, key: lnKey}, nil @@ -322,6 +331,20 @@ func isUnixNetwork(netw string) bool { return netw == "unix" || netw == "unixgram" || netw == "unixpacket" } +func isListenBindAddressAlreadyInUseError(err error) bool { + switch networkOperationError := err.(type) { + case *net.OpError: + switch syscallError := networkOperationError.Err.(type) { + case *os.SyscallError: + if syscallError.Syscall == "bind" { + return true + } + } + } + + return false +} + // ParseNetworkAddress parses addr into its individual // components. The input string is expected to be of // the form "network/host:port-range" where any part is |