From 1068d6b92b905adef34525fbac37a5af7290f4fa Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 20 May 2018 06:19:29 +0200 Subject: Give bind its own wait group In a waitgroup, all waits must come after all adds --- conn.go | 6 ++++-- device.go | 2 ++ receive.go | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/conn.go b/conn.go index b79b9cd..ded7419 100644 --- a/conn.go +++ b/conn.go @@ -75,6 +75,7 @@ func unsafeCloseBind(device *Device) error { err = netc.bind.Close() netc.bind = nil } + netc.stopping.Wait() return err } @@ -162,10 +163,11 @@ func (device *Device) BindUpdate() error { // start receiving routines - device.state.starting.Add(ConnRoutineNumber) - device.state.stopping.Add(ConnRoutineNumber) + device.net.starting.Add(ConnRoutineNumber) + device.net.stopping.Add(ConnRoutineNumber) go device.RoutineReceiveIncoming(ipv4.Version, netc.bind) go device.RoutineReceiveIncoming(ipv6.Version, netc.bind) + device.net.starting.Wait() device.log.Debug.Println("UDP bind has been updated") } diff --git a/device.go b/device.go index 6758cbc..7a23350 100644 --- a/device.go +++ b/device.go @@ -35,6 +35,8 @@ type Device struct { } net struct { + starting sync.WaitGroup + stopping sync.WaitGroup mutex sync.RWMutex bind Bind // bind interface port uint16 // listening port diff --git a/receive.go b/receive.go index 0f8554f..29fe5e9 100644 --- a/receive.go +++ b/receive.go @@ -125,11 +125,11 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind Bind) { logDebug := device.log.Debug defer func() { logDebug.Println("Routine: receive incoming IPv" + strconv.Itoa(IP) + " - stopped") - device.state.stopping.Done() + device.net.stopping.Done() }() logDebug.Println("Routine: receive incoming IPv" + strconv.Itoa(IP) + " - starting") - device.state.starting.Done() + device.net.starting.Done() // receive datagrams until conn is closed -- cgit v1.2.3