Skip to content

Commit

Permalink
server: Ignore net.ErrClosed errors when shutting down
Browse files Browse the repository at this point in the history
Also make server.Wait wait for all the listeners to exit, instead of
returning when the first one closes, which we couldn't do before because
of the spurious errors it would return

errors.Join requires go 1.20, update go.mod

Fixes #103

Signed-off-by: Anatole Denis <anatole@unverle.fr>
  • Loading branch information
Natolumin committed May 5, 2024
1 parent e3f7529 commit 28988eb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
4 changes: 1 addition & 3 deletions cmds/coredhcp-generator/coredhcp.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"io"
"os"
"time"

"github.com/coredhcp/coredhcp/config"
"github.com/coredhcp/coredhcp/logger"
Expand Down Expand Up @@ -100,7 +99,6 @@ func main() {
log.Fatal(err)
}
if err := srv.Wait(); err != nil {
log.Print(err)
log.Error(err)
}
time.Sleep(time.Second)
}
4 changes: 1 addition & 3 deletions cmds/coredhcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"os"
"time"

"github.com/coredhcp/coredhcp/config"
"github.com/coredhcp/coredhcp/logger"
Expand Down Expand Up @@ -122,7 +121,6 @@ func main() {
log.Fatal(err)
}
if err := srv.Wait(); err != nil {
log.Print(err)
log.Error(err)
}
time.Sleep(time.Second)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/coredhcp/coredhcp

go 1.19
go 1.20

require (
github.com/bits-and-blooms/bitset v1.13.0
Expand Down
11 changes: 9 additions & 2 deletions server/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package server

import (
"errors"
"fmt"
"net"
"sync"
Expand Down Expand Up @@ -209,7 +210,10 @@ func (l *listener6) Serve() error {
b = b[:MaxDatagram] //Reslice to max capacity in case the buffer in pool was resliced smaller

n, oob, peer, err := l.ReadFrom(b)
if err != nil {
if errors.Is(err, net.ErrClosed) {
// Server is quitting
return nil
} else if err != nil {
log.Printf("Error reading from connection: %v", err)
return err
}
Expand All @@ -225,7 +229,10 @@ func (l *listener4) Serve() error {
b = b[:MaxDatagram] //Reslice to max capacity in case the buffer in pool was resliced smaller

n, oob, peer, err := l.ReadFrom(b)
if err != nil {
if errors.Is(err, net.ErrClosed) {
// Server is quitting
return nil
} else if err != nil {
log.Printf("Error reading from connection: %v", err)
return err
}
Expand Down
10 changes: 8 additions & 2 deletions server/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package server

import (
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -164,9 +165,14 @@ cleanup:
// Wait waits until the end of the execution of the server.
func (s *Servers) Wait() error {
log.Debug("Waiting")
err := <-s.errors
errs := make([]error, 1, len(s.listeners))
errs[0] = <-s.errors
s.Close()
return err
// Wait for the other listeners to close
for i := 1; i < len(s.listeners); i++ {
errs = append(errs, <-s.errors)
}
return errors.Join(errs...)
}

// Close closes all listening connections
Expand Down

0 comments on commit 28988eb

Please sign in to comment.