Skip to content

Commit 49f3930

Browse files
luciopaivamp911de
authored andcommitted
Fix NPE when manually flushing a batch #2444
When manually flushing a batch with a set size, we could potentially run into a NPE if the command queue is empty when flush() is called.
1 parent ab5df24 commit 49f3930

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/main/java/io/lettuce/core/dynamic/SimpleBatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* </ul>
3636
*
3737
* @author Mark Paluch
38+
* @author Lucio Paiva
3839
*/
3940
class SimpleBatcher implements Batcher {
4041

@@ -148,12 +149,12 @@ private List<RedisCommand<Object, Object, Object>> prepareForceFlush() {
148149

149150
List<RedisCommand<Object, Object, Object>> batch = new ArrayList<>(Math.max(batchSize, 10));
150151

151-
do {
152+
while (!queue.isEmpty()) {
153+
152154
RedisCommand<Object, Object, Object> poll = queue.poll();
153155

154-
assert poll != null;
155156
batch.add(poll);
156-
} while (!queue.isEmpty());
157+
}
157158

158159
return batch;
159160
}
@@ -166,7 +167,6 @@ private List<RedisCommand<Object, Object, Object>> prepareDefaultFlush(int consu
166167

167168
RedisCommand<Object, Object, Object> poll = queue.poll();
168169

169-
assert poll != null;
170170
batch.add(poll);
171171
}
172172

src/test/java/io/lettuce/core/dynamic/RedisCommandsBatchingIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
/**
4444
* @author Mark Paluch
45+
* @author Lucio Paiva
4546
*/
4647
@ExtendWith(LettuceExtension.class)
4748
class RedisCommandsBatchingIntegrationTests extends TestSupport {
@@ -99,6 +100,16 @@ void selectiveBatchingShouldHandleErrors() {
99100
}
100101
}
101102

103+
@Test
104+
void shouldNotCrashWhenFlushCalledWithEmptyQueue() {
105+
106+
RedisCommandFactory factory = new RedisCommandFactory(redis.getStatefulConnection());
107+
108+
SelectiveBatchingWithSize api = factory.getCommands(SelectiveBatchingWithSize.class);
109+
110+
api.flush();
111+
}
112+
102113
@Test
103114
void shouldExecuteBatchingSynchronously() {
104115

@@ -215,4 +226,9 @@ static interface SelectiveBatching extends Commands, BatchExecutor {
215226

216227
}
217228

229+
@BatchSize(5)
230+
interface SelectiveBatchingWithSize extends Commands, BatchExecutor {
231+
232+
}
233+
218234
}

0 commit comments

Comments
 (0)