Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove incorrect use of TryLock for initial delay state in TUF autoupdater #1676

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions ee/tuf/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ type TufAutoupdater struct {
updateChannel string
pinnedVersions map[autoupdatableBinary]string // maps the binaries to their pinned versions
pinnedVersionGetters map[autoupdatableBinary]func() string // maps the binaries to the knapsack function to retrieve updated pinned versions
initialDelayLock *sync.Mutex
initialDelayLock *sync.RWMutex // locks inInitialDelay value
inInitialDelay bool
updateLock *sync.Mutex
interrupt chan struct{}
interrupted bool
Expand Down Expand Up @@ -130,7 +131,8 @@ func NewTufAutoupdater(ctx context.Context, k types.Knapsack, metadataHttpClient
binaryLauncher: func() string { return k.PinnedLauncherVersion() },
binaryOsqueryd: func() string { return k.PinnedOsquerydVersion() },
},
initialDelayLock: &sync.Mutex{},
initialDelayLock: &sync.RWMutex{},
inInitialDelay: false,
updateLock: &sync.Mutex{},
osquerier: osquerier,
osquerierRetryInterval: 30 * time.Second,
Expand Down Expand Up @@ -218,18 +220,17 @@ func DefaultLibraryDirectory(rootDirectory string) string {
// we store them in.
func (ta *TufAutoupdater) Execute() (err error) {
// Delay startup, if initial delay is set
ta.initialDelayLock.Lock() // prevent updates during delay
ta.setIsInInitialDelay(true)
select {
case <-ta.interrupt:
ta.slogger.Log(context.TODO(), slog.LevelDebug,
"received external interrupt during initial delay, stopping",
)
ta.initialDelayLock.Unlock()
return nil
case <-time.After(ta.knapsack.AutoupdateInitialDelay()):
break
}
ta.initialDelayLock.Unlock()
ta.setIsInInitialDelay(false)

// For now, tidy the library on startup. In the future, we will tidy the library
// earlier, after version selection.
Expand Down Expand Up @@ -278,10 +279,22 @@ func (ta *TufAutoupdater) Interrupt(_ error) {
ta.interrupt <- struct{}{}
}

func (ta *TufAutoupdater) setIsInInitialDelay(inInitialDelay bool) {
ta.initialDelayLock.Lock()
defer ta.initialDelayLock.Unlock()
ta.inInitialDelay = inInitialDelay
}

func (ta *TufAutoupdater) isInInitialDelay() bool {
ta.initialDelayLock.RLock()
defer ta.initialDelayLock.RUnlock()
return ta.inInitialDelay
}

// Do satisfies the actionqueue.actor interface; it allows the control server to send
// requests down to autoupdate immediately.
func (ta *TufAutoupdater) Do(data io.Reader) error {
if !ta.initialDelayLock.TryLock() {
if ta.isInInitialDelay() {
ta.slogger.Log(context.TODO(), slog.LevelWarn,
"received update request during initial delay, discarding",
)
Expand Down Expand Up @@ -377,7 +390,7 @@ func (ta *TufAutoupdater) FlagsChanged(flagKeys ...keys.FlagKey) {
}

// No updates, or we're in the initial delay
if len(binariesToCheckForUpdate) == 0 || !ta.initialDelayLock.TryLock() {
if len(binariesToCheckForUpdate) == 0 || ta.isInInitialDelay() {
return
}

Expand Down