From de3601f0ae73a9ebc9afb6bfbaf0cf529278f851 Mon Sep 17 00:00:00 2001 From: Sivamurugan P Date: Tue, 23 Dec 2025 01:48:49 +0530 Subject: [PATCH 1/2] fix: gracefully handle thread interruption in ConnectionImpl to prevent CI flakes Fixes #3992 --- .../com/google/cloud/bigquery/ConnectionImpl.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java index c3465c33a..0effc0f9e 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java @@ -1069,7 +1069,20 @@ private void processArrowStreamAsync( } } catch (Exception e) { - throw BigQueryException.translateAndThrow(e); + boolean isInterrupted = e.getCause() instanceof InterruptedException; + + if (e instanceof com.google.api.gax.rpc.CancelledException) { + isInterrupted = true; + } + if (isInterrupted) { + // Log silently and let it fall through to 'finally' for cleanup. + // This is the "graceful shutdown". + logger.log( + Level.INFO, "Background thread interrupted (Connection Closed). Stopping."); + Thread.currentThread().interrupt(); + } else { + throw BigQueryException.translateAndThrow(e); + } } finally { // logic needed for graceful shutdown // marking end of stream try { From c38c6c452a1b97a0c2d74b33644473c3a88907e2 Mon Sep 17 00:00:00 2001 From: Sivamurugan P Date: Tue, 23 Dec 2025 02:02:25 +0530 Subject: [PATCH 2/2] fix: consolidate interrupt checks and handle raw InterruptedException as per review --- .../java/com/google/cloud/bigquery/ConnectionImpl.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java index 0effc0f9e..d66903ce5 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java @@ -1069,12 +1069,9 @@ private void processArrowStreamAsync( } } catch (Exception e) { - boolean isInterrupted = e.getCause() instanceof InterruptedException; - - if (e instanceof com.google.api.gax.rpc.CancelledException) { - isInterrupted = true; - } - if (isInterrupted) { + if (e instanceof InterruptedException + || e.getCause() instanceof InterruptedException + || e instanceof com.google.api.gax.rpc.CancelledException) { // Log silently and let it fall through to 'finally' for cleanup. // This is the "graceful shutdown". logger.log(