fix: use context managers for file I/O to prevent resource leaks#2526
Open
lxcxjxhx wants to merge 2 commits into
Open
fix: use context managers for file I/O to prevent resource leaks#2526lxcxjxhx wants to merge 2 commits into
lxcxjxhx wants to merge 2 commits into
Conversation
- Replace json.load(open(...)) with context manager in layer_wise/utils.py - Add __del__ method to CaptureOutputToFile to ensure file closure - Use context manager for output_path_file.write() in accuracy.py These changes ensure proper resource cleanup and prevent file handle leaks. Signed-off-by: lxcxjxhx <lxcxjxhx@users.noreply.github.com>
lxcxjxhx
force-pushed
the
fix/file-handle-resource-leaks
branch
from
July 15, 2026 13:12
3c674a3 to
c7d39f6
Compare
Author
|
Hi @chensuyue @xin3he, could you please review this PR? The changes are defensive resource management improvements using context managers for file I/O. I noticed the Model-Test-3x CI check is failing. This appears to be unrelated to the code changes (which only add context managers for file handling). Could you help investigate or re-run the CI if needed? Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Several places in the codebase open files without using context managers (
withstatements), which can lead to resource leaks if exceptions occur or if the file handle is not explicitly closed. This is a common Python anti-pattern that can cause issues in long-running processes or when processing many files.Changes
neural_compressor/torch/algorithms/layer_wise/utils.py: Replacedjson.load(open(...))withwith open(...) as f: json.load(f)inload_tensor_from_shardandload_tensor_from_safetensors_shardto ensure file handles are properly closed after reading.neural_compressor/tensorflow/utils/utility.py: Added__del__method toCaptureOutputToFileclass to ensure the temporary file is closed even when the object is not used as a context manager (i.e.,__exit__is never called).neural_compressor/evaluation/lm_eval/accuracy.py: Replacedoutput_path_file.open("w", encoding="utf-8").write(dumped)with awithstatement to ensure the output file is properly closed after writing.Testing
These are defensive resource management improvements that do not change functional behavior. The existing test suite should continue to pass without modification.