Skip to content

Commit

Permalink
util: fix race in cidr startup
Browse files Browse the repository at this point in the history
Previously if the `server.cidr_mapping_url` was set and a node
restarted, there was a race condition where `SetOnChange` for the
setting could be called before the `Start` was called. This could result
in it blocking while attempting to submit to the channel.

Fixes: #130589

Release note: None
  • Loading branch information
andrewbaptist committed Sep 12, 2024
1 parent 3424fc9 commit 896f2fc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/util/cidr/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,28 @@ func NewLookup(st *settings.Values) *Lookup {
byLength := make([]map[string]string, 0)
c.byLength.Store(&byLength)
c.lastUpdate.Store(time.Time{})
c.changed = make(chan time.Duration)
c.changed = make(chan time.Duration, 1)

cidrMappingUrl.SetOnChange(st, func(ctx context.Context) {
log.Infof(ctx, "url changed to '%s'", cidrMappingUrl.Get(st))
// Reset the lastUpdate time so that the URL is always reloaded even if
// the new file/URL has an older timestamp.
c.lastUpdate.Store(time.Time{})
c.changed <- cidrRefreshInterval.Get(c.st)
select {
case c.changed <- cidrRefreshInterval.Get(c.st):
default:
}
})
// We have to register this callback first. Otherwise we may run into
// an unlikely but possible scenario where we've started the ticker,
// and the setting is changed before we register the callback and the
// ticker will not be reset to the new value.
cidrRefreshInterval.SetOnChange(c.st, func(ctx context.Context) {
log.Infof(ctx, "refresh interval changed to '%s'", cidrRefreshInterval.Get(c.st))
c.changed <- cidrRefreshInterval.Get(c.st)
select {
case c.changed <- cidrRefreshInterval.Get(c.st):
default:
}
})
return c
}
Expand Down

0 comments on commit 896f2fc

Please sign in to comment.