Repository navigation
fix(progressutil): resolve race condition in ProgressUpdator - #910
Merged
Merged
Conversation
TestProgressUpdator_StartAndDone occasionally failed in CI (e.g. https://github.com/GoogleCloudPlatform/khi/actions/runs/32937279763/job/98080874197?pr=908) with: OnTick should not be called after Done, got count 3, want 2 This was caused by: 1. ProgressUpdator.Done() not synchronizing with the worker goroutine, returning before in-flight OnTick calls completed. 2. Go select statement non-determinism picking the timer case even when context was cancelled. 3. time.After creating temporary timers in a loop. This commit fixes the issue by: - Adding sync.WaitGroup to ProgressUpdator so Done() waits for the worker goroutine to terminate. - Replacing time.After with time.NewTicker. - Checking context cancellation before executing OnTick when the ticker fires. - Returning an error if Start() is called when already started. - Adding a unit test to verify starting twice returns an error.
kyasbal
requested review from
K53,
RyuSA,
jyane,
kkuchima and
renamoo
as code owners
August 26, 2026 07:28
Contributor
There was a problem hiding this comment.
Code Review
This pull request improves the ProgressUpdator by preventing it from being started multiple times, replacing time.After with a more efficient time.Ticker in the background goroutine, and utilizing a sync.WaitGroup to ensure the goroutine completes when Done() is called. A unit test was also added to verify the double-start prevention. The review feedback suggests capturing the local cancellable context inside the goroutine to prevent potential data races, and resetting p.context and p.cancel to nil inside Done() to allow the updator to be safely reused.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a flaky test failure in
TestProgressUpdator_StartAndDonecaused by race conditions inProgressUpdator.Background & Motivation
TestProgressUpdator_StartAndDoneoccasionally failed in CI, for example in GitHub Actions Run 32937279763 (PR #908):Root Cause
Done():ProgressUpdator.Done()previously only invokedcancel()on the context and returned immediately without synchronizing with or waiting for the worker goroutine.interval * 2(100ms), and callsDone(). At roughly the 100ms mark, the periodic timer triggers. WhenDone()returned before the worker goroutine terminated,tickCountJustAfterDonewas recorded as 2, and then the worker goroutine executedOnTick, incrementingtickCountto 3.selectNon-determinism: In Go, when both<-p.context.Done()and the timer/ticker channel are simultaneously ready,selectpicks uniformly at random, which could executeOnTickeven after cancellation.time.Afterin a loop repeatedly created temporary timers.Key Changes
Done(): Addedsync.WaitGrouptoProgressUpdator.Done()now callsp.wg.Wait()to guarantee that all in-flight updates have completely finished beforeDone()returns.time.Afterwithtime.NewTickerand added a non-blocking check for<-p.context.Done()before callingOnTickto prevent race execution if cancellation arrived concurrently with the tick.Start()GoDoc.TestProgressUpdator_StartTwiceinprogress_updator_test.goto test starting twice error handling without modifying existing tests.Verification
go test ./pkg/core/inspection/progressutil -count=100(100/100 passes without failure).go test -race ./pkg/core/inspection/progressutilpassed with 0 data races.make test-gopassed across all backend packages.make format-go,make lint-go, andmake pre-commitpassed with 0 issues.