Skip to content

fix: replace bare except with ValueError in metadata_helpers.py - #13981

Open
praneshnikhar wants to merge 2 commits into
kubeflow:masterfrom
praneshnikhar:fix-bare-except-metadata-helpers
Open

fix: replace bare except with ValueError in metadata_helpers.py#13981
praneshnikhar wants to merge 2 commits into
kubeflow:masterfrom
praneshnikhar:fix-bare-except-metadata-helpers

Conversation

@praneshnikhar

@praneshnikhar praneshnikhar commented Aug 5, 2026

Copy link
Copy Markdown

Description

The get_or_create_* functions and get_or_create_context_with_type in metadata_helpers.py used bare except: clauses to catch "not found" errors from the ML Metadata store. The ml-metadata library raises ValueError when a type or context is not found, but bare except: catches everything — including TypeError, KeyError, network errors, memory errors — and silently falls through to the create path. This risks creating duplicate or invalid MLMD entities when unrelated errors occur.

Change type

/kind bug

Changelog

fix: Replaced bare except: with except ValueError: in metadata_helpers.py get_or_create_* and get_or_create_context_with_type functions to prevent swallowing unrelated errors

The get_or_create_* functions and get_or_create_context_with_type used bare
except: clauses to catch "not found" errors from the ML Metadata store. This
silently swallowed unrelated errors (network, serialization, memory) and fell
through to the create path, risking duplicate/invalid MLMD entities.

Replace bare except: with except ValueError: to only catch the specific
"not found" exception raised by ml-metadata store methods.

Signed-off-by: praneshnikhar <praneshnikhar@gmail.com>
Copilot AI balanced review requested due to automatic review settings August 5, 2026 11:09
@google-oss-prow

Copy link
Copy Markdown

@praneshnikhar: The label(s) kind/fix cannot be applied, because the repository doesn't have them.

Details

In response to this:

Description

The get_or_create_* functions and get_or_create_context_with_type in metadata_helpers.py used bare except: clauses to catch "not found" errors from the ML Metadata store. The ml-metadata library raises ValueError when a type or context is not found, but bare except: catches everything — including TypeError, KeyError, network errors, memory errors — and silently falls through to the create path. This risks creating duplicate or invalid MLMD entities when unrelated errors occur.

Change type

/kind fix

Changelog

fix: Replaced bare except: with except ValueError: in metadata_helpers.py get_or_create_* and get_or_create_context_with_type functions to prevent swallowing unrelated errors

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@google-oss-prow
google-oss-prow Bot requested a review from hbelmiro August 5, 2026 11:09
@google-oss-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign nsingla for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-prow
google-oss-prow Bot requested a review from VaniHaripriya August 5, 2026 11:09

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@google-oss-prow

Copy link
Copy Markdown

Hi @praneshnikhar. Thanks for your PR.

I'm waiting for a kubeflow member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@hbelmiro

hbelmiro commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

/ok-to-test

@hbelmiro

hbelmiro commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

/retest

@hbelmiro hbelmiro 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.

Could you please add unit tests for the four get-or-create functions? The store is passed as a parameter, so they're easy to test with a mock. For each function, cover at least:

  1. Get path: mock the get method to return an existing type/context — assert the put method is never called and the existing object is returned.
  2. Create path: mock the get method to raise the caught exception — assert the put method is called and the new object is returned.
  3. Unrelated exception propagates: mock the get method to raise a different exception (e.g., RuntimeError) — assert it propagates instead of silently falling through to the create path.

@google-oss-prow google-oss-prow Bot added size/L and removed size/XS labels Aug 6, 2026
@hbelmiro

hbelmiro commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

/retest

@hbelmiro hbelmiro 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.

The PR description claims ml-metadata raises ValueError on not-found, but inspecting the actual ml-metadata 1.21.0 source (metadata_store.py:1104-1105) shows get_artifact_type documents:

Raises:
    errors.NotFoundError: if no type exists.
    errors.InternalError: if query execution fails.

errors.NotFoundError inherits from StatusError(Exception) — it is not a subclass of ValueError. Catching ValueError here will never match a not-found response from the store, so the create path becomes unreachable. Every call will propagate NotFoundError as an unhandled exception instead of falling through to create the type.

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