Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit b774f5d

Browse files
authored
fix: fix a NullPtr when user closes a writer without connection being ever established (#1454)
1 parent cb8b0ad commit b774f5d

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/Exceptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
/** Exceptions for Storage Client Libraries. */
2929
public final class Exceptions {
30+
public static class WriterClosedException extends Exception {
31+
public WriterClosedException(String streamName) {
32+
super("Writer closed on: " + streamName);
33+
}
34+
}
3035
/** Main Storage Exception. Might contain map of streams to errors for that stream. */
3136
public static class StorageException extends RuntimeException {
3237

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,13 @@ private AppendRowsRequest prepareRequestBasedOnPosition(
502502
}
503503

504504
private void cleanupInflightRequests() {
505-
Throwable finalStatus;
505+
Throwable finalStatus = new Exceptions.WriterClosedException(streamName);
506506
Deque<AppendRequestAndResponse> localQueue = new LinkedList<AppendRequestAndResponse>();
507507
this.lock.lock();
508508
try {
509-
finalStatus = this.connectionFinalStatus;
509+
if (this.connectionFinalStatus != null) {
510+
finalStatus = this.connectionFinalStatus;
511+
}
510512
while (!this.inflightRequestQueue.isEmpty()) {
511513
localQueue.addLast(pollInflightRequestQueue());
512514
}

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/StreamWriterTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,12 @@ public void testRetryAfterAllRecordsInflight() throws Exception {
638638
assertEquals(1, appendFuture2.get().getAppendResult().getOffset().getValue());
639639
}
640640
}
641+
642+
@Test
643+
public void testWriterClosedStream() throws Exception {
644+
try (StreamWriter writer = getTestStreamWriter()) {
645+
// Writer is closed without any traffic.
646+
TimeUnit.SECONDS.sleep(1);
647+
}
648+
}
641649
}

0 commit comments

Comments
 (0)