Skip to content

fix: use missing_ok for temp file cleanup to avoid masking errors - #3820

Open
Quratulain-bilal wants to merge 2 commits into
github:mainfrom
Quratulain-bilal:fix/presets-registry-encoding
Open

fix: use missing_ok for temp file cleanup to avoid masking errors#3820
Quratulain-bilal wants to merge 2 commits into
github:mainfrom
Quratulain-bilal:fix/presets-registry-encoding

Conversation

@Quratulain-bilal

Copy link
Copy Markdown
Contributor

Replace if temp_path.exists(): temp_path.unlink() with emp_path.unlink(missing_ok=True) in 3 files: integrations/manifest.py, shared_infra.py, _utils.py. The old pattern could raise OSError if unlink() fails, masking the original exception in inally/except blocks.

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.

Pull request overview

This PR attempts to prevent temporary-file cleanup from masking write failures.

Changes:

  • Replaces existence checks with Path.unlink(missing_ok=True) in three atomic-write paths.
  • However, non-FileNotFoundError cleanup failures can still mask original exceptions.
Show a summary per file
File Description
src/specify_cli/shared_infra.py Updates shared-file cleanup.
src/specify_cli/integrations/manifest.py Updates manifest cleanup.
src/specify_cli/_utils.py Updates settings JSON cleanup.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 3
  • Review effort level: Medium

Comment thread src/specify_cli/shared_infra.py Outdated
Comment thread src/specify_cli/integrations/manifest.py Outdated
Comment thread src/specify_cli/_utils.py Outdated

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.

Review details

Suppressed comments (3)

src/specify_cli/shared_infra.py:268

  • Add a regression test for this new cleanup-failure path. Have the write/replace operation raise a sentinel exception and Path.unlink raise OSError, then assert the sentinel remains the propagated error; otherwise the behavior this PR is intended to guarantee can regress unnoticed.
        try:
            temp_path.unlink(missing_ok=True)
        except OSError:
            pass

src/specify_cli/integrations/manifest.py:445

  • Add a manifest persistence regression test that forces the save operation to fail and the temporary-file unlink to raise OSError, and verifies the original save error is preserved. The existing persistence tests cover successful round trips but not the masking scenario introduced here.
            try:
                temp_path.unlink(missing_ok=True)
            except OSError:
                pass

src/specify_cli/_utils.py:199

  • Please cover this exception branch by making os.replace fail with a distinguishable original error and cleanup unlink fail with OSError, then asserting handle_vscode_settings still propagates the replace error. Current settings tests only exercise a successful atomic write, so this fix has no regression guard.
            if temp_path:
                try:
                    temp_path.unlink(missing_ok=True)
                except OSError:
                    pass
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Wrap unlink(missing_ok=True) in try/except OSError in finally/except blocks
to prevent cleanup failures from masking the original exception.
@Quratulain-bilal
Quratulain-bilal force-pushed the fix/presets-registry-encoding branch from bbe9657 to c0d362c Compare July 31, 2026 15:20
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.

3 participants