fix(udprelay): синхронизировать watcher-горутину с возвратом Run
fatalErr писался watcher-горутиной вне WaitGroup и читался после wg.Wait(); загрузке не хватало happens-after упорядочивания с записью. Заменено на atomic.Pointer и канал watcherDone, который Run дренажит после отмены, так что загрузка всегда видит полную запись.
This commit is contained in:
1 parent
0e2d42c912
commit
fba9a5e26d
1 file changed
+10
-4
@@ -135,19 +135,25 @@ func Run(ctx context.Context, dtlsDialer *dtlsdial.Dialer, auth AuthHandler, log
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If a fatal error was sent, cancel remaining goroutines and propagate up.
|
// If a fatal error was sent, cancel remaining goroutines and propagate up.
|
||||||
var fatalErr error
|
// watcherDone synchronises the watcher goroutine with Run's return so the
|
||||||
|
// fatalErr load happens-after the store.
|
||||||
|
var fatalErr atomic.Pointer[error]
|
||||||
|
watcherDone := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
|
defer close(watcherDone)
|
||||||
select {
|
select {
|
||||||
case err := <-fatalCh:
|
case err := <-fatalCh:
|
||||||
fatalErr = err
|
fatalErr.Store(&err)
|
||||||
runCancel()
|
runCancel()
|
||||||
case <-runCtx.Done():
|
case <-runCtx.Done():
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
if fatalErr != nil {
|
runCancel()
|
||||||
return fatalErr
|
<-watcherDone
|
||||||
|
if p := fatalErr.Load(); p != nil {
|
||||||
|
return *p
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user