Fix file handle leak in BinTableHDU ASCII table load helpers#20059
Open
Debajeet-1411 wants to merge 3 commits into
astropy:mainastropy/astropy:mainfrom
Debajeet-1411:fix/bintable-load-file-handle-leakDebajeet-1411/astropy:fix/bintable-load-file-handle-leakCopy head branch name to clipboard
Open
Fix file handle leak in BinTableHDU ASCII table load helpers#20059Debajeet-1411 wants to merge 3 commits intoastropy:mainastropy/astropy:mainfrom Debajeet-1411:fix/bintable-load-file-handle-leakDebajeet-1411/astropy:fix/bintable-load-file-handle-leakCopy head branch name to clipboard
Debajeet-1411 wants to merge 3 commits into
astropy:mainastropy/astropy:mainfrom
Debajeet-1411:fix/bintable-load-file-handle-leakDebajeet-1411/astropy:fix/bintable-load-file-handle-leakCopy head branch name to clipboard
Conversation
Contributor
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
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.
fixes #20057
Summary
This PR ensures that files opened internally by
BinTableHDU.load()are always closed, even when parsing raises an exception.Previously, the internal load helpers explicitly called
fileobj.close()only at the end of the function. If parsing failed before reaching that point, the file handle remained open until it was eventually released by the interpreter, which can lead to file-locking issues on platforms such as Windows.What changed
This PR updates the following helper methods:
_load_data_load_coldefsWhen these helpers receive a path-like object, they now open the file using a context manager and recursively invoke the existing parsing logic with the opened file object.
Behavior for caller-provided file objects is unchanged—the helper only manages the lifetime of files that it opens itself.
Why this approach?
These helpers support two kinds of inputs:
Using a context manager for path-like inputs guarantees that internally opened files are closed even if parsing raises an exception, while preserving the existing behavior for caller-owned file objects.
Regression test
A regression test has been added for
BinTableHDU.load().The test passes malformed column-definition input through the public API, verifies that the expected parsing exception is raised, and then immediately removes the temporary files. This confirms that internally opened file handles are released correctly. On Windows, this also prevents file-locking issues caused by unclosed handles.
Motivation
While investigating
BinTableHDU.load(), I found that parsing exceptions could bypass the explicit cleanup path, leaving internally opened files open until they were later released by the interpreter.This change makes resource cleanup exception-safe without changing parsing behavior, exception types, or the public API.
If there's a preferred approach for handling ownership of internally opened file objects, I can update the implementation accordingly.