diff --git a/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableIO.java b/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableIO.java index da989280bd4a..ed16fe174e71 100644 --- a/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableIO.java +++ b/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableIO.java @@ -1419,6 +1419,23 @@ public void finishBundle(FinishBundleContext c) throws Exception { } } + @Teardown + public void tearDown() throws IOException { + // in case of exception in processElement, finishBundle will not be called + // cleanup resources allocated in startBundle + try { + if (bigtableWriter != null) { + bigtableWriter.close(); + bigtableWriter = null; + } + } finally { + if (serviceEntry != null) { + serviceEntry.close(); + serviceEntry = null; + } + } + } + @Override public void populateDisplayData(DisplayData.Builder builder) { config.populateDisplayData(builder);