Fix resource leak on close error in SessionFactory #1448
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 critical resource leak vulnerability in the
SessionFactory.Close()method where errors from intermediate cache closure could be silently lost, potentially leaving resources unfreed in production systems.Issue Analysis
The Problem
The original code in
session.go:97-100had a resource management flaw:Critical Issues:
f.intermediateKeys.Close()fails butf.systemKeys.Close()succeeds, the first error is completely lostWhy This Matters
Solution
The Fix
Collect all errors and ensure all cleanup operations are attempted:
Key Improvements
Testing
Added comprehensive test coverage for all error scenarios:
Test Cases Covered
intermediate_error_only: Intermediate cache fails, system cache succeedssystem_error_only: System cache fails, intermediate cache succeedsboth_errors: Both caches fail with different errorsno_shared_cache_system_error: System error when shared cache disabledno_errors: All operations succeed (regression test)Example Test Results
Performance Impact
Validation
Impact
This fix prevents production resource leaks and improves system reliability by:
The solution follows Go best practices for resource management and error handling without introducing external dependencies.
🤖 Generated with Claude Code