Skip to content

fix: close file handles in readxml filecache and CLI patch loading#2711

Open
henryiii wants to merge 1 commit into
mainfrom
fix/io-file-handles
Open

fix: close file handles in readxml filecache and CLI patch loading#2711
henryiii wants to merge 1 commit into
mainfrom
fix/io-file-handles

Conversation

@henryiii

Copy link
Copy Markdown
Member

🤖 AI text below 🤖

Part of the code review in #2706.

Summary

  • src/pyhf/readxml.py line 218: modtag.attrib["Activate"] raised a raw KeyError when a <StatError> element lacked the attribute. Changed to modtag.attrib.get("Activate", "False") == "True", consistent with all sibling attribute accesses.
  • src/pyhf/readxml.py clear_filecache: the global __FILECACHE__ stores open uproot file handles. Previously, clearing the dict abandoned them without closing. Now each handle is closed (using contextlib.suppress(Exception) to guard against already-closed handles) before the dict is reset.
  • src/pyhf/cli/infer.py (two sites) and src/pyhf/cli/rootio.py (one site): json.loads(click.open_file(...).read()) leaked file handles. Replaced with explicit with click.open_file(...) as f: json.load(f) context managers, matching the style used everywhere else in those files.

Test plan

  • pytest tests/test_import.py tests/test_scripts.py -q — 78 tests pass, no regressions
  • prek -a --quiet (pre-commit / ruff) — clean, no lint errors

🤖 Generated with Claude Code

- readxml.py: use .get("Activate", "False") on StatError to avoid
  KeyError when the attribute is absent
- readxml.py: close cached uproot file handles in clear_filecache()
  before resetting the dict, using contextlib.suppress for already-
  closed handles
- cli/infer.py, cli/rootio.py: replace bare click.open_file().read()
  (which leaks the handle) with proper with-statement context managers

Assisted-by: ClaudeCode:claude-sonnet-4.6
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@henryiii, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 29 minutes and 58 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: c43946e1-37cf-47e8-9e60-00e7c2e42728

📥 Commits

Reviewing files that changed from the base of the PR and between 0e54a51 and 262820e.

📒 Files selected for processing (3)
  • src/pyhf/cli/infer.py
  • src/pyhf/cli/rootio.py
  • src/pyhf/readxml.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@henryiii henryiii marked this pull request as ready for review June 12, 2026 17:56
@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.40%. Comparing base (0e54a51) to head (262820e).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/pyhf/cli/infer.py 62.50% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2711      +/-   ##
==========================================
- Coverage   98.28%   96.40%   -1.88%     
==========================================
  Files          65       65              
  Lines        4305     4316      +11     
  Branches      465      468       +3     
==========================================
- Hits         4231     4161      -70     
- Misses         46      121      +75     
- Partials       28       34       +6     
Flag Coverage Δ
contrib ?
doctest ?
unittests-3.10 ?
unittests-3.11 96.40% <80.00%> (-0.07%) ⬇️
unittests-3.12 ?
unittests-3.13 ?
unittests-3.9 ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant