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() {