From ec0897f92415d0857ac145465d2c154711e1e7ee Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Fri, 20 Mar 2026 17:20:24 -0400 Subject: [PATCH] chore: Check if projectId is already set from the MDS call --- .../com/google/auth/oauth2/ComputeEngineCredentials.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java index de4b84599..ad5fb8e7d 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java @@ -364,9 +364,14 @@ public String getProjectId() { String projectIdFromMetadata = getProjectIdFromMetadata(); synchronized (this) { - this.projectId = projectIdFromMetadata; + // Check first if another thread set the Project ID. No need to overwrite + // if a Projects ID already exists. Tries to prevent a case where the last call + // for `getProjectIdFromMetadata()` returns null and overwrites valid data. + if (this.projectId == null) { + this.projectId = projectIdFromMetadata; + } } - return projectIdFromMetadata; + return this.projectId; } private String getProjectIdFromMetadata() {