Skip to content

Clean temporary output after fdopen setup failure - #241

Merged
stefankoegl merged 1 commit into
stefankoegl:masterfrom
ShahadatTest:fix/cleanup-failed-output-setup
Oct 26, 2026
Merged

stefankoegl merged 1 commit into
stefankoegl:masterfrom
ShahadatTest:fix/cleanup-failed-output-setup

Conversation

@ShahadatTest

Copy link
Copy Markdown
Contributor

If mkstemp succeeds but os.fdopen raises OSError, the in-place CLI falls back to direct output while leaving the temporary descriptor and file behind. Close the descriptor and remove that temporary file before entering the existing fallback. Cleanup errors remain best effort, and the existing backup behavior is preserved.

A regression uses a real mkstemp descriptor and injects fdopen failure, with and without backups. Both subcases fail on the unchanged base. After the fix they verify the descriptor is closed, the patch is written, backups contain the original, and no temporary file remains.

Validation on native Windows / Python 3.12:

  • Full tests.py: 224 tests, OK (1 POSIX-only skip).
  • Property-based suite: 21 tests, OK.
  • git diff --check passes.

This covers output setup before the write/close cleanup in #235. AI assistance: OpenAI Codex investigated, implemented, tested and drafted this change at the account owner's request for further useful contributions. No separate human code review is claimed.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — ShahadatTest:fix/cleanup-failed-output-setup into stefankoegl:master

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

The narrowly scoped cleanup preserves existing fallback behavior and has targeted regression coverage with no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes a temporary-resource leak in the in-place CLI when mkstemp succeeds but os.fdopen fails, complementing the write-error cleanup in #235.

Changes:

  • Closes the temporary descriptor and attempts to remove the temporary file before entering the existing fallback.
  • Preserves best-effort cleanup and backup behavior.
  • Adds regression coverage for descriptor closure, patched output, backups, and temporary-file removal.
File Description
tests.py Tests fdopen failure cleanup with and without backups.
jsonpatch_cli.py Cleans up temporary resources before falling back to direct output.

Review effort: Balanced

@stefankoegl
stefankoegl merged commit 3960180 into stefankoegl:master Oct 26, 2026
12 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