Skip to content

fix(progressutil): resolve race condition in ProgressUpdator - #910

Merged
kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-nwtuowwqkrrv
Aug 26, 2026
Merged

kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-nwtuowwqkrrv

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 26, 2026

Copy link
Copy Markdown
Member

Summary

This PR fixes a flaky test failure in TestProgressUpdator_StartAndDone caused by race conditions in ProgressUpdator.

Background & Motivation

TestProgressUpdator_StartAndDone occasionally failed in CI, for example in GitHub Actions Run 32937279763 (PR #908):

--- FAIL: TestProgressUpdator_StartAndDone (0.60s)
    progress_updator_test.go:87: OnTick should not be called after Done, got count 3, want 2
FAIL

Root Cause

  1. Asynchronous Done(): ProgressUpdator.Done() previously only invoked cancel() on the context and returned immediately without synchronizing with or waiting for the worker goroutine.
  2. Race at Interval Boundary: The test verifies ticks, sleeps for interval * 2 (100ms), and calls Done(). At roughly the 100ms mark, the periodic timer triggers. When Done() returned before the worker goroutine terminated, tickCountJustAfterDone was recorded as 2, and then the worker goroutine executed OnTick, incrementing tickCount to 3.
  3. select Non-determinism: In Go, when both <-p.context.Done() and the timer/ticker channel are simultaneously ready, select picks uniformly at random, which could execute OnTick even after cancellation.
  4. Timer Allocation in Loop: Using time.After in a loop repeatedly created temporary timers.

Key Changes

  • Synchronized Worker Goroutine in Done(): Added sync.WaitGroup to ProgressUpdator. Done() now calls p.wg.Wait() to guarantee that all in-flight updates have completely finished before Done() returns.
  • Ticker & Context Cancellation Guard: Replaced time.After with time.NewTicker and added a non-blocking check for <-p.context.Done() before calling OnTick to prevent race execution if cancellation arrived concurrently with the tick.
  • Start Guard: Prevented restarting an already started updator by returning an error as documented in Start() GoDoc.
  • Unit Tests: Added TestProgressUpdator_StartTwice in progress_updator_test.go to test starting twice error handling without modifying existing tests.

Verification

  • Stress Test: go test ./pkg/core/inspection/progressutil -count=100 (100/100 passes without failure).
  • Race Detector: go test -race ./pkg/core/inspection/progressutil passed with 0 data races.
  • Backend Tests: make test-go passed across all backend packages.
  • Linters & Formatters: make format-go, make lint-go, and make pre-commit passed with 0 issues.
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 kyasbal added type:bug Something isn't working as expected area:cicd GitHub Actions workflows and release pipelines labels Aug 26, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kyasbal
kyasbal merged commit db14005 into GoogleCloudPlatform:main Aug 26, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-nwtuowwqkrrv branch August 26, 2026 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cicd GitHub Actions workflows and release pipelines type:bug Something isn't working as expected

1 participant