Skip to content

Conversation

@MrSnakeDoc
Copy link
Owner

📝 Description

This PR addresses critical bugs identified during post-optimization code review by GitHub Copilot. The changes fix race conditions, cache state inconsistencies, and logger thread-safety issues introduced during the performance optimization work.

Key Fixes:

  1. Cache State Consistency: MarkUpgraded now correctly sets the Installed flag when upgrading packages
  2. Logger Race Condition: ColorPrinter access is now protected with read locks to prevent concurrent access issues
  3. Logger Double-Formatting: Removed redundant color formatting in writeDirect function
  4. Cache Race Condition: Eliminated race condition in IsInstalled by simplifying the refresh check logic
  5. Core Initialization: Changed to fail-fast when cache initialization fails instead of creating invalid empty cache
  6. Error Handling: Added comment documenting that cache preserves existing data on error instead of clearing it
  7. Test Coverage: Added comprehensive unit tests for UnifiedCache core functionality (9 tests covering all critical paths)

🔗 Related Issue(s)

🔄 Type of Change

  • 🐛 Bug fix (non-breaking change fixing an issue)
  • ✨ New feature (non-breaking change adding functionality)
  • 💥 Breaking change (fix or feature with breaking changes)
  • 📚 Documentation update
  • 🔧 Code refactoring
  • ⚙️ CI/CD pipeline update

📋 Checklist

  • My code follows the project's style guidelines
  • I have updated the documentation accordingly
  • I have added tests for my changes
  • All new and existing tests pass
  • My changes don't affect performance negatively
  • I have tested my changes on different Linux distributions
  • I have verified Homebrew integration works correctly

🔍 Additional Notes

These fixes address subtle but critical bugs that could cause:

  • Race conditions during concurrent logger operations or cache refresh
  • Cache inconsistencies where upgraded packages weren't marked as installed
  • Application crashes if cache initialization failed (now fails gracefully with clear error)
  • Data loss during cache refresh errors (now preserves existing data)

All changes have been tested with keg list, keg upgrade --check, and full test suite passes.

@MrSnakeDoc MrSnakeDoc self-assigned this Jan 20, 2026
Copilot AI review requested due to automatic review settings January 20, 2026 18:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses critical race conditions and cache consistency issues identified during post-optimization review. The changes improve thread-safety in the logger, fix middleware argument propagation, ensure cache state consistency, and add comprehensive test coverage.

Changes:

  • Fixed race conditions in logger by protecting ColorPrinter access with read locks
  • Corrected middleware chain to propagate modified commands and arguments
  • Updated cache operations to maintain consistent state (MarkUpgraded now sets Installed flag)
  • Changed core initialization to fail-fast on cache errors instead of creating invalid empty cache
  • Added comprehensive unit tests for UnifiedCache functionality

Reviewed changes

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

Show a summary per file
File Description
internal/middleware/middleware.go Fixed middleware chain to properly propagate modified cmd/args through the chain
internal/logger/logger.go Added RLock protection for ColorPrinter access and removed double-formatting in writeDirect
internal/core/core.go Changed to fail-fast with Fatal error when cache initialization fails
internal/brew/unified_cache_test.go Added comprehensive unit tests for UnifiedCache core functionality (9 test cases)
internal/brew/unified_cache.go Fixed MarkUpgraded to set Installed flag, simplified IsInstalled race condition fix, added error handling comment, optimized splitLines
go.mod Moved testify from indirect to direct dependency for new tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 174 to 176
assert.False(t, cache.Packages["wget"].Installed)
assert.Empty(t, cache.Packages["wget"].InstalledVersion)
assert.False(t, cache.Packages["wget"].Outdated)
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

This test expects the package to remain in the cache with Installed set to false, but the actual MarkUninstalled implementation deletes the package from the cache entirely. These assertions will fail because cache.Packages["wget"] will not exist after calling MarkUninstalled. Either update the test to check that the package is deleted (using assert.NotContains or checking the map directly), or update MarkUninstalled to preserve the package with Installed=false instead of deleting it.

Suggested change
assert.False(t, cache.Packages["wget"].Installed)
assert.Empty(t, cache.Packages["wget"].InstalledVersion)
assert.False(t, cache.Packages["wget"].Outdated)
assert.NotContains(t, cache.Packages, "wget", "package should be removed from cache after uninstall")

Copilot uses AI. Check for mistakes.
"github.com/stretchr/testify/require"
)

// mockRunner implements runner.CommandRunner for testing
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The comment describes this as "mockRunner" but the type is named "testRunner". Update the comment to match the type name for consistency.

Suggested change
// mockRunner implements runner.CommandRunner for testing
// testRunner implements runner.CommandRunner for testing

Copilot uses AI. Check for mistakes.
@MrSnakeDoc MrSnakeDoc merged commit af7af5e into main Jan 20, 2026
3 checks passed
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.

2 participants