Skip to content

fix: prevent infinite Readdirnames loops in audit manager and enforce non-nil errors in ConcurrentErrorSlice#4587

Open
pillatipriyanka wants to merge 5 commits into
open-policy-agent:masterfrom
pillatipriyanka:pillatipriyanka_gatekeeper
Open

fix: prevent infinite Readdirnames loops in audit manager and enforce non-nil errors in ConcurrentErrorSlice#4587
pillatipriyanka wants to merge 5 commits into
open-policy-agent:masterfrom
pillatipriyanka:pillatipriyanka_gatekeeper

Conversation

@pillatipriyanka

Copy link
Copy Markdown

Summary
This PR includes targeted refactoring improvements to enhance code quality, error handling, and maintainability across key packages.

Changes

  1. ConcurrentErrorSlice Safety (pkg/syncutil/concurrent_slice.go)
    Added bounds checking to Last() method to prevent index out of range panics
    Returns nil for empty slices instead of panicking
    Improves reliability of concurrent error collection
  2. Audit Manager Optimizations (pkg/audit/manager.go)
    readUnstructured(): Removed unnecessary empty map initialization

Direct JSON unmarshaling initializes the Object field automatically
Reduces memory allocations and improves performance
removeAllFromDir(): Enhanced error handling

Properly distinguishes between io.EOF and other errors
Prevents incorrectly ignoring read errors
Improved variable shadowing with inline error checks
3. Readiness Package (pkg/readiness/list.go)
Reduced redundant logging in retryLister()
Removed duplicate 'err' parameter from log.Error call
Cleaner and more accurate error logging
4. Upgrade Manager (pkg/upgrade/manager.go)
Improved initialization in New() constructor

Initialize client field at construction instead of leaving it nil
Prevents nil pointer dereference risks
Cleaner and more efficient code
Fixed documentation: Corrected comment from "audit" to "upgrade"

Impact
✅ Prevents runtime panics in concurrent error handling
✅ Reduces memory allocations in JSON unmarshaling
✅ Improves error handling robustness
✅ Enhances code maintainability and clarity
✅ No breaking changes or functional modifications
Testing
All changes maintain existing functionality while improving implementation details. Code compiles successfully without errors.

Type
Refactoring
Code Quality Improvement
Bug Prevention

Copilot AI review requested due to automatic review settings May 23, 2026 19:07
@pillatipriyanka pillatipriyanka requested a review from a team as a code owner May 23, 2026 19:07
@linux-foundation-easycla

linux-foundation-easycla Bot commented May 23, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: pillatipriyanka / name: pillati lakshmi priyanka (6083b9f)

Copilot AI 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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR makes several small robustness/cleanup updates across upgrade, audit, readiness logging, and a concurrency utility.

Changes:

  • Updates upgrade Manager construction and comment to reflect upgrade usage.
  • Makes ConcurrentErrorSlice.Last() safe on empty slices.
  • Improves audit directory cleanup / JSON unmarshal patterns and simplifies readiness logging.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
pkg/upgrade/manager.go Adjusts New() and manager initialization for upgrade usage.
pkg/syncutil/concurrent_slice.go Avoids panic by returning nil when no errors exist.
pkg/readiness/list.go Simplifies error logging message/fields during listing retries.
pkg/audit/manager.go Tweaks directory deletion loop error handling and simplifies unstructured JSON unmarshal.

Comment thread pkg/upgrade/manager.go
Comment thread pkg/audit/manager.go Outdated
@pillatipriyanka pillatipriyanka force-pushed the pillatipriyanka_gatekeeper branch from ce16ca7 to 6083b9f Compare May 23, 2026 19:15
Copilot AI review requested due to automatic review settings May 23, 2026 19:24

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

Comment thread pkg/upgrade/manager.go
Comment thread pkg/syncutil/concurrent_slice.go
Comment thread pkg/audit/manager.go Outdated
Comment thread pkg/audit/manager.go Outdated
Comment thread pkg/audit/manager.go Outdated
Comment on lines +788 to +799
if len(names) > 0 {
files = append(files, names...)
}
if err == nil {
continue
}
if errors.Is(err, io.EOF) {
break
}
return files, err
}
return files, nil
Comment thread pkg/audit/manager.go Outdated
@pillatipriyanka pillatipriyanka force-pushed the pillatipriyanka_gatekeeper branch from 5452655 to 7772f8f Compare May 23, 2026 19:46
Copilot AI review requested due to automatic review settings May 23, 2026 20:01

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread pkg/syncutil/concurrent_slice.go Outdated
Comment thread pkg/audit/manager.go Outdated
Comment thread pkg/audit/manager.go Outdated
Comment on lines +789 to +802
if len(names) == 0 {
if err == nil || errors.Is(err, io.EOF) {
break
}
return files, err
}
files = append(files, names...)
if err == nil {
continue
}
if errors.Is(err, io.EOF) {
break
}
return files, err

@pillatipriyanka pillatipriyanka left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

all comments addressed

@pillatipriyanka pillatipriyanka force-pushed the pillatipriyanka_gatekeeper branch 2 times, most recently from 9bf38fc to f9abe8d Compare May 23, 2026 20:16
@pillatipriyanka pillatipriyanka changed the title Pillatipriyanka gatekeeper fix: prevent infinite Readdirnames loops in audit manager and enforce non-nil errors in ConcurrentErrorSlice May 23, 2026
Signed-off-by: pillati lakshmi priyanka <pillatilakshmi@gmail.com>
Signed-off-by: pillati lakshmi priyanka <pillatilakshmi@gmail.com>
@pillatipriyanka pillatipriyanka force-pushed the pillatipriyanka_gatekeeper branch 4 times, most recently from dd92581 to 03e7cd4 Compare May 23, 2026 20:22
Signed-off-by: pillati lakshmi priyanka <pillatilakshmi@gmail.com>
Signed-off-by: pillati lakshmi priyanka <pillatilakshmi@gmail.com>
@pillatipriyanka pillatipriyanka force-pushed the pillatipriyanka_gatekeeper branch from 03e7cd4 to 4cce7ec Compare May 23, 2026 20:22
…; make ConcurrentErrorSlice zero-value safe

Signed-off-by: pillati lakshmi priyanka <pillatilakshmi@gmail.com>
Copilot AI review requested due to automatic review settings May 23, 2026 23:28

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment thread pkg/upgrade/manager.go
Comment on lines +39 to 42
return &Manager{
client: mgr.GetClient(),
mgr: mgr,
}
type ConcurrentErrorSlice struct {
s []error
mu *sync.RWMutex
mu sync.RWMutex
Comment on lines 12 to +18
func NewConcurrentErrorSlice() ConcurrentErrorSlice {
return ConcurrentErrorSlice{
s: make([]error, 0),
mu: &sync.RWMutex{},
s: make([]error, 0),
}
}

func (c ConcurrentErrorSlice) Append(e error) ConcurrentErrorSlice {
func (c *ConcurrentErrorSlice) Append(e error) {
Comment thread pkg/audit/manager.go
func (am *Manager) readUnstructured(jsonBytes []byte) (*unstructured.Unstructured, error) {
u := &unstructured.Unstructured{
Object: make(map[string]interface{}),
func (am *Manager) readDirNames(dir *os.File, batchSize int) (names []string, done bool, err error) {
@JaydipGabani

Copy link
Copy Markdown
Contributor

@pillatipriyanka can you fix CI and address copilot feedback?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants