Skip to content

Commit

Permalink
fix race conditions in shutdown/close
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Sep 28, 2016
1 parent 93c20a6 commit 9d49aa4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ func newClient() (*client, error) {
return c, nil
}

func (c *client) getIsClosed() bool {
c.closeLock.Lock()
defer c.closeLock.Unlock()
return c.closed
}

// Close is used to cleanup the client
func (c *client) Close() error {
c.closeLock.Lock()
Expand Down Expand Up @@ -326,7 +332,7 @@ func (c *client) recv(l *net.UDPConn, msgCh chan *dns.Msg) {
return
}
buf := make([]byte, 65536)
for !c.closed {
for !c.getIsClosed() {
n, err := l.Read(buf)
if err != nil {
log.Printf("[ERR] mdns: Failed to read packet: %v", err)
Expand Down
8 changes: 7 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func NewServer(config *Config) (*Server, error) {
return s, nil
}

func (s *Server) getIsShutdown() bool {
s.shutdownLock.Lock()
defer s.shutdownLock.Unlock()
return s.shutdown
}

// Shutdown is used to shutdown the listener
func (s *Server) Shutdown() error {
s.shutdownLock.Lock()
Expand All @@ -107,7 +113,7 @@ func (s *Server) recv(c *net.UDPConn) {
return
}
buf := make([]byte, 65536)
for !s.shutdown {
for !s.getIsShutdown() {
n, from, err := c.ReadFrom(buf)
if err != nil {
continue
Expand Down

0 comments on commit 9d49aa4

Please sign in to comment.