Skip to content

Commit cb44cb7

Browse files
authored
chore: Check if projectId is already set from the MDS call (#1904)
1 parent b04f046 commit cb44cb7

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,14 @@ public String getProjectId() {
364364

365365
String projectIdFromMetadata = getProjectIdFromMetadata();
366366
synchronized (this) {
367-
this.projectId = projectIdFromMetadata;
367+
// Check first if another thread set the Project ID. No need to overwrite
368+
// if a Projects ID already exists. Tries to prevent a case where the last call
369+
// for `getProjectIdFromMetadata()` returns null and overwrites valid data.
370+
if (this.projectId == null) {
371+
this.projectId = projectIdFromMetadata;
372+
}
368373
}
369-
return projectIdFromMetadata;
374+
return this.projectId;
370375
}
371376

372377
private String getProjectIdFromMetadata() {

0 commit comments

Comments
 (0)