Skip to content

Broad exception swallowing in find_git_ref hides repository/backend failures#3112

Open
huyhoang171106 wants to merge 1 commit intometabrainz:masterfrom
huyhoang171106:fix/broad-exception-swallowing-in-find-git-r
Open

Broad exception swallowing in find_git_ref hides repository/backend failures#3112
huyhoang171106 wants to merge 1 commit intometabrainz:masterfrom
huyhoang171106:fix/broad-exception-swallowing-in-find-git-r

Conversation

@huyhoang171106
Copy link
Copy Markdown

Summary

find_git_ref catches Exception and silently returns None. This makes real failures (e.g., repository I/O errors, backend API breakage, permission issues) indistinguishable from a normal "ref not found" result. Callers can then take wrong code paths (e.g., treating a broken repo as missing ref) and continue with invalid state, which is a classic silent-failure bug.

Files changed

  • picard/git/ref_utils.py (modified)

Testing

  • Not run in this environment.

Summary

  • This is a…
    • Bug fix
    • Feature addition
    • Refactoring
    • Minor / simple change (like a typo)
    • Other
  • Describe this change in 1-2 sentences:

Problem

  • JIRA ticket (optional): PICARD-XXX

Solution

AI Usage

In accordance with the AI use policy portion of the MetaBrainz Contribution Guidelines, the level of AI/LLM use in the development of this Pull Request is:

  • No AI/LLM use
  • Minimal use (e.g. autocompletion)
  • Moderate use (e.g. suggestions regarding code fragments)
  • Significant use (e.g. code structure, tests development, etc.)
  • Primarily AI developed
  • Other (please specify below)

Action

Additional actions required:

  • Update Picard documentation (please include a reference to this PR)
  • Other (please specify below)

…ry/backend failures

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 26, 2026 14:07
Copy link
Copy Markdown

Copilot AI left a comment

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 fixes a silent-failure bug in Git reference lookup by removing broad exception swallowing in find_git_ref, so repository/backend failures are no longer indistinguishable from a normal “ref not found” result.

Changes:

  • Removed try/except Exception: pass around repo.list_references() and ref matching logic in find_git_ref.
  • Allows backend/repository exceptions to propagate to callers instead of returning None silently.

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

Comment on lines +37 to 38
references = repo.list_references()

Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

find_git_ref no longer swallows exceptions from repo.list_references(), but the docstring still implies this function only returns GitRef/None. Please update the docstring (and any callers’ expectations) to note that backend/repository errors can propagate so failures aren’t mistaken for “ref not found”.

Copilot uses AI. Check for mistakes.
Comment on lines +37 to +46
references = repo.list_references()

# Check shortname first (most common case)
for git_ref in references:
if git_ref.shortname == ref_name:
return git_ref
# Check shortname first (most common case)
for git_ref in references:
if git_ref.shortname == ref_name:
return git_ref

# Check full name
for git_ref in references:
if git_ref.name == ref_name:
return git_ref

except Exception:
pass
# Check full name
for git_ref in references:
if git_ref.name == ref_name:
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

references is iterated twice (shortname pass, then full-name pass). This is unnecessary work and will behave incorrectly if repo.list_references() returns an iterator/generator (the second loop will be empty). Consider materializing references once (e.g., list(...)) or doing both checks in a single pass.

Copilot uses AI. Check for mistakes.
Comment on lines +37 to 38
references = repo.list_references()

Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

This change intentionally alters behavior from “return None on any exception” to “propagate errors”. Please add a unit test covering the new behavior (e.g., when list_references() raises) so regressions don’t reintroduce exception swallowing or silent failures.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

@phw phw left a comment

Choose a reason for hiding this comment

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

Thanks for opening a PR, but this change is incomplete. For one tests are failing. But also this PR changes behavior at quite some critical level, without actually handling the impact of the change higher up in the call-chain. find_git_ref is a fundamental utility function in the plugin implementation. This change alone likely will introduce some bugs, possible crashes.

So the usage of find_git_ref has to be checked up to the UI level and it must be ensured the errors get handled.

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.

3 participants