Skip to content

Commit b0fd5bb

Browse files
committed
fix(batch): guard against null reference
1 parent c3d1751 commit b0fd5bb

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/batch/BatchContext.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ private void shutdown() {
363363
queue.put(TaskHandle.POISON);
364364

365365
// Wait for both "send" to exit; "send" will not exit until "recv" completes.
366-
send.get();
366+
if (send != null) {
367+
send.get();
368+
}
367369
closing.complete(null);
368370
} catch (Exception e) {
369371
closing.completeExceptionally(e);
@@ -378,7 +380,9 @@ private void shutdownNow(Exception ex) {
378380
messages.onError(Status.INTERNAL.withCause(ex).asRuntimeException());
379381

380382
// Terminate the "send" routine.
381-
send.cancel(true);
383+
if (send != null) {
384+
send.cancel(true);
385+
}
382386

383387
if (!closed) {
384388
// Since shutdownNow is never triggered by the "main" thread,

src/test/java/io/weaviate/client6/v1/api/collections/batch/BatchContextTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.concurrent.TimeUnit;
1717
import java.util.stream.Stream;
1818

19+
import javax.annotation.concurrent.GuardedBy;
20+
1921
import org.assertj.core.api.Assertions;
2022
import org.junit.After;
2123
import org.junit.AfterClass;
@@ -75,6 +77,7 @@ public class BatchContextTest {
7577
1);
7678

7779
/** Batch context for the current test case. */
80+
@GuardedBy("this")
7881
private BatchContext<Map<String, Object>> context;
7982
/** Server half of the stream. */
8083
private volatile OutboundStream out;
@@ -92,7 +95,8 @@ private StreamObserver<Message> createStream(StreamObserver<Event> recv) {
9295
* descriptor, and collection handle defaults.
9396
*/
9497
@Before
95-
public void startContext() {
98+
public synchronized void startContext() {
99+
assert context == null;
96100
context = new BatchContext.Builder<>(this::createStream, MAX_SIZE_BYTES, DESCRIPTOR, DEFAULTS)
97101
.batchSize(BATCH_SIZE)
98102
.queueSize(QUEUE_SIZE)
@@ -102,7 +106,7 @@ public void startContext() {
102106
}
103107

104108
@After
105-
public void reset() throws Exception {
109+
public synchronized void reset() throws Exception {
106110
if (context != null) {
107111
// Some of the tests may close the context, so this
108112
// implicitly tests that closing it multiple times is OK.

0 commit comments

Comments
 (0)