From 1b7ce4c378bfa13f147c386e28f8f1774bc29b4d Mon Sep 17 00:00:00 2001 From: Cenk Alti Date: Tue, 25 Dec 2018 11:23:47 +0300 Subject: [PATCH] protect socket variable with mutex; fix #437 --- frontend/udp/frontend.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index bc5b421..1c0f32d 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -49,6 +49,7 @@ func (cfg Config) LogFields() log.Fields { // Frontend holds the state of a UDP BitTorrent Frontend. type Frontend struct { socket *net.UDPConn + m sync.Mutex closing chan struct{} wg sync.WaitGroup @@ -103,10 +104,20 @@ func (t *Frontend) Stop() stop.Result { c := make(stop.Channel) go func() { + t.m.Lock() + defer t.m.Unlock() + close(t.closing) - t.socket.SetReadDeadline(time.Now()) + if t.socket != nil { + t.socket.SetReadDeadline(time.Now()) + } t.wg.Wait() - c.Done(t.socket.Close()) + + var err error + if t.socket != nil { + err = t.socket.Close() + } + c.Done(err) }() return c.Result() @@ -120,7 +131,9 @@ func (t *Frontend) listenAndServe() error { return err } + t.m.Lock() t.socket, err = net.ListenUDP("udp", udpAddr) + t.m.Unlock() if err != nil { return err }