Skip to content

Close jsondiff input files after loading documents - #254

Merged
stefankoegl merged 1 commit into
stefankoegl:masterfrom
arin30:fix/jsondiff-close-inputs
Nov 5, 2026
Merged

stefankoegl merged 1 commit into
stefankoegl:masterfrom
arin30:fix/jsondiff-close-inputs

Conversation

@arin30

@arin30 arin30 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

diff_files() leaves both argparse-opened input files open after reading them. They also remain open if either JSON document fails to parse. This makes repeated in-process calls depend on garbage collection to release descriptors and file locks.

Use ExitStack to close both owned inputs after loading the documents, including on parsing errors. Standard input is left open when either filename is '-'. Output and exit codes are unchanged.

Three tests retain real input handles and check cleanup for equal and differing documents, malformed JSON in either file, and stdin in either position. All six subcases fail on unchanged master and pass with the fix.

Validation on Python 3.12.14: 289 tests and 24 property-based tests pass, the CI Flake8 error check passes, and git diff --check is clean.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the input-file cleanup change.

No open issues 🎉

What changed in this PR

This PR fixes input-file cleanup in the jsondiff CLI, releasing file handles after loading documents even when JSON parsing fails.

Changes:

  • Uses ExitStack to close both owned inputs while leaving standard input open.
  • Adds regression tests for equal and differing documents, malformed JSON in either file, and stdin in either position.
  • Preserves existing output and exit-code behavior.
File Description
tests.py Adds input cleanup and stdin preservation tests.
jsonpatch_cli.py Closes owned input files after loading, including on parsing errors.

Review effort: Balanced

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — arin30:fix/jsondiff-close-inputs into stefankoegl:master

@stefankoegl
stefankoegl merged commit 115186f into stefankoegl:master Nov 5, 2026
22 checks passed
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.

4 participants