Skip to content

Commit

Permalink
Merge pull request #1029 from ersonp/fix/data-race
Browse files Browse the repository at this point in the history
Fix/data race
  • Loading branch information
jdknives authored Dec 17, 2021
2 parents b6ca868 + 2a1a564 commit 22137f2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 1 addition & 2 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ var BuildTag string

// Summary implements API.
func (v *Visor) Summary() (*Summary, error) {
v.wgTrackers.Wait()

overview, err := v.Overview()
if err != nil {
return nil, fmt.Errorf("overview")
Expand Down Expand Up @@ -218,6 +216,7 @@ func (v *Visor) Summary() (*Summary, error) {
}

dmsgStatValue := &dmsgtracker.DmsgClientSummary{}
v.wgTrackers.Wait()
if v.trackers != nil {
if dmsgTracker := v.trackers.GetBulk([]cipher.PubKey{v.conf.PK}); len(dmsgTracker) > 0 {
dmsgStatValue = &dmsgTracker[0]
Expand Down
5 changes: 4 additions & 1 deletion pkg/visor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ func initDiscovery(ctx context.Context, v *Visor, log *logging.Logger) error {
}

func initStunClient(ctx context.Context, v *Visor, log *logging.Logger) error {
v.wgStunClient.Add(1)
defer v.wgStunClient.Done()
sc := network.GetStunDetails(v.conf.StunServers, log)
v.initLock.Lock()
v.stunClient = sc
Expand Down Expand Up @@ -513,8 +515,9 @@ func getRouteSetupHooks(ctx context.Context, v *Visor, log *logging.Logger) []ro
errSlice := make([]error, 0, 2)
for _, trans := range transports {
ntype := network.Type(trans)
v.wgStunClient.Wait()
// skip if SUDPH is under symmetric NAT / under UDP firewall.
if ntype == network.SUDPH && v.stunClient != nil && (v.stunClient.NATType == stun.NATSymmetric ||
if ntype == network.SUDPH && (v.stunClient.NATType == stun.NATSymmetric ||
v.stunClient.NATType == stun.NATSymmetricUDPFirewall) {
continue
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ type Visor struct {
dmsgHTTP *http.Client // dmsghttp client
trackers *dmsgtracker.Manager

stunClient *network.StunDetails
tpM *transport.Manager
arClient addrresolver.APIClient
router router.Router
rfClient rfclient.Client
stunClient *network.StunDetails
wgStunClient *sync.WaitGroup
tpM *transport.Manager
arClient addrresolver.APIClient
router router.Router
rfClient rfclient.Client

procM appserver.ProcManager // proc manager
appL *launcher.Launcher // app launcher
Expand Down Expand Up @@ -116,10 +117,9 @@ func NewVisor(conf *visorconfig.V1, restartCtx *restart.Context) (*Visor, bool)
initLock: new(sync.Mutex),
isServicesHealthy: newInternalHealthInfo(),
wgTrackers: new(sync.WaitGroup),
wgStunClient: new(sync.WaitGroup),
transportCacheMu: new(sync.Mutex),
}
v.wgTrackers.Add(1)
defer v.wgTrackers.Done()

v.isServicesHealthy.init()

Expand Down Expand Up @@ -155,6 +155,8 @@ func NewVisor(conf *visorconfig.V1, restartCtx *restart.Context) (*Visor, bool)
if !v.processRuntimeErrs() {
return nil, false
}
v.wgTrackers.Add(1)
defer v.wgTrackers.Done()
v.trackers = dmsgtracker.NewDmsgTrackerManager(v.MasterLogger(), v.dmsgC, 0, 0)
return v, true
}
Expand Down

0 comments on commit 22137f2

Please sign in to comment.