Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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#20059
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

@Debajeet-1411

Copy link
Copy Markdown
Contributor

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_coldefs

When 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:

  • path-like objects (where the helper owns the file object), and
  • already-open file objects (which remain owned by the caller).

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.

@Debajeet-1411
Debajeet-1411 requested a review from saimn as a code owner July 12, 2026 07:29
@github-actions

Copy link
Copy Markdown
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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BinTableHDU.load() can leave file handles open when parsing fails

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.