From 639b34c7d1d92ed52bbc9f6a51a2a1778ece1a16 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Wed, 28 Nov 2018 16:26:33 +0300 Subject: [PATCH] dnsforward library -- Fix race conditions found by -race --- dnsforward/dnsforward.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/dnsforward/dnsforward.go b/dnsforward/dnsforward.go index 546c4eae..c7559b42 100644 --- a/dnsforward/dnsforward.go +++ b/dnsforward/dnsforward.go @@ -185,19 +185,23 @@ func (s *Server) reconfigureListenAddr(new ServerConfig) error { if err != nil { return errorx.Decorate(err, "Couldn't bind to %v", newAddr) } + s.Lock() if s.udpListen != nil { - err := s.udpListen.Close() - if err != nil { - return errorx.Decorate(err, "Couldn't close UDP listening socket") - } + err = s.udpListen.Close() + } + s.Unlock() + if err != nil { + return errorx.Decorate(err, "Couldn't close UDP listening socket") } } else { log.Printf("Rebinding -- ports are same so close first then bind") + s.Lock() if s.udpListen != nil { - err := s.udpListen.Close() - if err != nil { - return errorx.Decorate(err, "Couldn't close UDP listening socket") - } + err = s.udpListen.Close() + } + s.Unlock() + if err != nil { + return errorx.Decorate(err, "Couldn't close UDP listening socket") } newListen, err = net.ListenUDP("udp", newAddr) if err != nil {