Skip to content

Commit

Permalink
fix: use net.ErrClosed
Browse files Browse the repository at this point in the history
fixes: #19
  • Loading branch information
fastcat committed Mar 10, 2021
1 parent 46fb7c6 commit 5110e3c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/networking/native/go-udpconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package native

import (
"context"
"errors"
"net"
"time"

"github.com/fastcat/wirelink/internal/networking"
"github.com/fastcat/wirelink/util"
)

// GoUDPConn implements networking.UDPConn by wrapping net.UDPConn
Expand Down Expand Up @@ -69,7 +69,7 @@ READLOOP:
now := time.Now()

if err != nil {
if util.IsNetClosing(err) {
if errors.Is(err, net.ErrClosed) {
// the socket has been closed, we're done
break READLOOP
}
Expand Down
2 changes: 1 addition & 1 deletion internal/networking/vnet/socket-udpconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (sc *socketUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err er
inbound := sc.inbound
sc.s.m.Unlock()
if inbound == nil {
err = errors.New(util.NetClosingErrorString)
err = net.ErrClosed
return
}
p := <-sc.inbound
Expand Down
17 changes: 0 additions & 17 deletions util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,8 @@ import (
"net"
"sort"
"strings"

"github.com/pkg/errors"
)

// NetClosingErrorString is the voodoo string returned when you try to use a
// Close()d network connection, because https://github.com/golang/go/issues/4373
const NetClosingErrorString = "use of closed network connection"

// IsNetClosing checks err and its Unwrap chain for NetClosingErrorString
func IsNetClosing(err error) bool {
if err == nil {
return false
}
if strings.Contains(err.Error(), NetClosingErrorString) {
return true
}
return IsNetClosing(errors.Unwrap(err))
}

// UDPEqualIPPort checks if to UDPAddrs are equal in terms of their IP and Port
// fields, but ignoring any Zone value
func UDPEqualIPPort(a, b *net.UDPAddr) bool {
Expand Down

0 comments on commit 5110e3c

Please sign in to comment.