Skip to content

fix(inspection/googlecloudcommon): avoid race condition dropping partition boundary logs in log fetcher - #947

Merged
kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-ylpsuqltrlop
Sep 3, 2026
Merged

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

Conversation

@kyasbal

@kyasbal kyasbal commented Sep 3, 2026

Copy link
Copy Markdown
Member

Summary

This PR resolves an intermittent issue where logs at time partition boundaries were randomly dropped during log fetching, causing non-deterministic log counts across inspection runs.

Root Cause

In StandardProgressReportableLogFetcher.FetchLogsWithProgress, a consumer goroutine forwards log entries from stubChan to dest. When FetchLogs returned synchronously, cancelSubroutine() immediately cancelled subroutineCtx.
Because the consumer goroutine used:

select {
case <-subroutineCtx.Done():
    return
case dest <- logEntry:
}

If cancelSubroutine() executed while forwarding a log entry, Go's select statement would randomly pick between the cancelled context and dest <- logEntry. When <-subroutineCtx.Done() won the race (~50% chance), the final log entry of that time partition was discarded.

Fix Details

  1. Draining stubChan before stopping progress reporting (progressreportablelogfetcher.go):
    • Separated the consumer and progress synchronization contexts and WaitGroups (consumerCtx, progressCtx, consumerWg, progressWg).
    • After FetchLogs completes, consumerWg.Wait() waits for the consumer goroutine to fully drain stubChan until closed (!ok) without cancelling consumerCtx.
    • Only after all logs are drained does it cancel progress reporting and send the final 100% progress report.
  2. Deterministic Node Ordering (nodenameinventory_task.go):
    • Added slices.Sort(ret) to nodeNameMergeStrategy.Merge to ensure node query split orders are strictly deterministic across runs.
  3. Graceful null Parameter Handling (setform.go):
    • Handled valueRaw == nil in SetFormTask so passing null in job inspection values falls back to the default empty array instead of failing with request parameter ... was not given in array.

Verification

  • Ran 3 consecutive full-stack E2E inspection jobs with all log features enabled (autoscaler, compute-api, gke-api, k8s-container, k8s-control-plane, k8s-event, k8s-node, network-api, serialport, k8s-auditlog).
  • Verified that all 10 log ingesters produced exact, identical log counts across all 3 runs (total: 241,819 entries, 0 differences).
  • make lint-go and make test-go passed with 0 issues.
… fetcher

In StandardProgressReportableLogFetcher.FetchLogsWithProgress, when
FetchLogs returned, subroutineCtx was cancelled while dest <- logEntry
could still be pending in the consumer goroutine. Go select would
randomly pick <-subroutineCtx.Done(), causing the final log entry of a
time partition to be dropped intermittently.

This change:
- Separates consumer and progress synchronization contexts so that
  successful log fetching drains stubChan completely before progress
  reporting stops.
- Sorts node names in NodeNameInventoryTask to guarantee deterministic
  query partitioning order.
- Allows null values in SetFormTask to gracefully fall back to default
  values instead of erroring on array cast.
@kyasbal kyasbal added the type:bug Something isn't working as expected label Sep 3, 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 introduces a nil check for form task values, sorts node names in the node name inventory task, and refactors context and wait group management in FetchLogsWithProgress to separate consumer and progress reporting lifecycles. Feedback is provided to ensure that context cancellation errors are correctly propagated if the context is cancelled during the log draining phase.

@kyasbal
kyasbal merged commit 463422d into GoogleCloudPlatform:main Sep 3, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-ylpsuqltrlop branch September 3, 2026 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:bug Something isn't working as expected

1 participant