Skip to content

Commit 861ba4b

Browse files
committed
fix(bigquery): narrow mutex in close() to closed flag and shutdown clients directly
1 parent b53dc85 commit 861ba4b

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

  • java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ BigQueryReadClient getBigQueryReadClient(String location) {
559559
configureReadSettings(settingsBuilder, getOptions());
560560
try {
561561
client = BigQueryReadClient.create(settingsBuilder.build());
562+
if (closed) {
563+
client.close();
564+
throw new IllegalStateException("BigQuery service has been closed");
565+
}
562566
if (bqReadClients.size() < MAX_CACHED_READ_CLIENTS) {
563567
bqReadClients.put(cacheKey, client);
564568
}
@@ -596,23 +600,21 @@ void setBigQueryReadClient(String location, BigQueryReadClient client) {
596600
*/
597601
@Override
598602
public void close() {
599-
List<BigQueryReadClient> clientsToClose = new ArrayList<>();
600603
synchronized (this) {
601604
if (closed) {
602605
return;
603606
}
604607
closed = true;
605-
if (bqReadClients != null) {
606-
clientsToClose.addAll(bqReadClients.values());
607-
bqReadClients.clear();
608-
}
609608
}
610-
for (BigQueryReadClient client : clientsToClose) {
611-
try {
612-
client.close();
613-
} catch (Exception e) {
614-
// Ignore exceptions during teardown
609+
if (bqReadClients != null) {
610+
for (BigQueryReadClient client : bqReadClients.values()) {
611+
try {
612+
client.close();
613+
} catch (Exception e) {
614+
// Ignore exceptions during teardown
615+
}
615616
}
617+
bqReadClients.clear();
616618
}
617619
}
618620

0 commit comments

Comments
 (0)