-
Notifications
You must be signed in to change notification settings - Fork 992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix NPE when manually flushing a batch #2444
Conversation
RedisCommand<Object, Object, Object> poll = queue.poll(); | ||
|
||
assert poll != null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am removing this assert
because it doesn't help with production code. I searched for uses of assert
throughout the code and found only this and another one a few lines below, which I'm also removing.
@@ -166,7 +166,6 @@ private List<RedisCommand<Object, Object, Object>> prepareDefaultFlush(int consu | |||
|
|||
RedisCommand<Object, Object, Object> poll = queue.poll(); | |||
|
|||
assert poll != null; | |||
batch.add(poll); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code here in prepareDefaultFlush()
is already checking if the queue is empty before poll()
ing from it, so I didn't have to do anything.
0a94832
to
6b05b7c
Compare
6b05b7c
to
971cfec
Compare
Care to provide the stack trace showing where the exception originates from? |
Sure. This is
|
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.
971cfec
to
b7011eb
Compare
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.
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.
Thank you for your contribution. That's merged, polished, and backported now. |
@mp911de how is this being backported? Will we see this fix in 5.x, 4.x, etc at some point? |
It is backported to Lettuce 6.2.5, which I released today. I maintain only a single bugfix branch at a time. |
When manually flushing a batch with a set size, we can potentially run into a NPE if the command queue is empty when flush() is called. This PR is fixing that.
I ran into this problem while working on a code that sends commands to Redis in batches. In my code I defined an interface like this:
For each batch, no matter how many commands it has, I want to make sure that it flushes the commands as soon as I finish enqueuing them, like this:
And that's when I ran into a problem: when
myBatchSize == 100
, after the 100th is enqueued a flush is automatically triggered because of@BatchSize(100)
, and the queue gets emptied. Right after,api.flush()
is called and Lettuce raises a NPE.A simpler way to reproduce the problem is to just call
flush()
without ever enqueueing anything, so that's what I did in the test that I wrote.The fix is really simple and just involves replacing a
do..while
with awhile
block that first checks if the queue is empty before trying to poll from it.The bug is there since at least 5.2 (the earliest version I tested).
Make sure that: