Skip to content

Commit

Permalink
Ignore listen error when shutting down unix socket server
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
  • Loading branch information
hwipl committed Apr 17, 2023
1 parent dd239e9 commit ec65f26
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"net"
"os"
"sync"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -19,6 +20,26 @@ type Server struct {
sockFile string
listen net.Listener
requests chan *Request

mutex sync.Mutex
stop bool
}

// setStopping marks the server as stopping
func (s *Server) setStopping() {
s.mutex.Lock()
defer s.mutex.Unlock()

s.stop = true
}

// isStopping returns whether the server is stopping
func (s *Server) isStopping() bool {
s.mutex.Lock()
defer s.mutex.Unlock()

return s.stop

}

// handleRequest handles a request from the client
Expand Down Expand Up @@ -71,6 +92,11 @@ func (s *Server) handleClients() {
// wait for new client connection
conn, err := s.listen.Accept()
if err != nil {
if s.isStopping() {
// ignore error when shutting down
return
}

log.WithError(err).Error("Daemon listener error")
return
}
Expand Down Expand Up @@ -106,6 +132,7 @@ func (s *Server) Start() {
// Stop stops the API server
func (s *Server) Stop() {
// stop listener
s.setStopping()
err := s.listen.Close()
if err != nil {
log.WithError(err).Fatal("Daemon could not close unix listener")
Expand Down

0 comments on commit ec65f26

Please sign in to comment.