Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
10 changes: 6 additions & 4 deletions google/auth/compute_engine/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@

# Timeout in seconds to wait for the GCE metadata server when detecting the
# GCE environment.
try:
_METADATA_DEFAULT_TIMEOUT = int(os.getenv("GCE_METADATA_TIMEOUT", 3))
except ValueError: # pragma: NO COVER
_METADATA_DEFAULT_TIMEOUT = 3
_METADATA_DEFAULT_TIMEOUT = 3
Comment thread
ludoch marked this conversation as resolved.
Outdated

# Detect GCE Residency
_GOOGLE = "Google"
Expand Down Expand Up @@ -119,6 +116,11 @@ def ping(request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=3):
# could lead to false negatives in the event that we are on GCE, but
# the metadata resolution was particularly slow. The latter case is
# "unlikely".
try:
timeout = float(os.getenv("GCE_METADATA_TIMEOUT", timeout))
except ValueError: # pragma: NO COVER
pass
Copy link
Copy Markdown
Contributor

@clundin25 clundin25 Feb 21, 2024

Choose a reason for hiding this comment

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

I would not capture a ValueError here. Additionally, the timeout variable would not be usable, so the downstream call would have problems.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

FWIW the original code caught the ValueError. The value is set by the user.
I am thinking if the Exception happens timeout will retain the passed in/default value

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.

In that case, can you remove the pragma, and include test coverage for this change?


retries = 0
headers = _METADATA_HEADERS.copy()
headers[metrics.API_CLIENT_HEADER] = metrics.mds_ping()
Expand Down