Repository navigation
[fix][client] Fix possible npe - #18406
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #18406 +/- ##
============================================
+ Coverage 46.98% 47.19% +0.20%
- Complexity 10343 10401 +58
============================================
Files 692 692
Lines 67766 67767 +1
Branches 7259 7259
============================================
+ Hits 31842 31984 +142
+ Misses 32344 32193 -151
- Partials 3580 3590 +10
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
I'm thinking that with this patch whether we can replace the whole first several lines as:
RequestTime request = requestTimeoutQueue.poll();
if (request == null || !request.isTimedOut(operationTimeoutMs)) {
// if there is no request that is timed out then exit the loop
break;
}Oops. It's different semantic..
|
But still it seems weird. If this patch works, shall we recheck the |
| @@ -1296,6 +1296,9 @@ private void checkRequestTimeout() { | |||
| break; | |||
| } | |||
| request = requestTimeoutQueue.poll(); | |||
There was a problem hiding this comment.
I think we should check the request returned by poll() is exactly the same with what peek() returns. Otherwise, the request.isTimedOut call doesn't apply on the removed request.
I think it's better to use remove here instead of the null check.
if (!requestTimeoutQueue.remove(request)) {
// the request has been removed by another thread
continue;
}There was a problem hiding this comment.
remove() will throw a NoSuchElementException when the queue is empty.
There was a problem hiding this comment.
request = requestTimeoutQueue.poll();
if (request == null) {
// the request has been removed by another thread
continue;
}Wouldn't this be better?
There was a problem hiding this comment.
remove() will throw a NoSuchElementException when the queue is empty.
No. I mean the remove(Object) API. See the following examples:
var list = new ConcurrentLinkedQueue<String>();
list.add("hello");
var s = list.peek();
list.clear(); // assuming another thread remove the value
System.out.println(list.isEmpty()); // true
boolean removed = list.remove(s); // no exception would be thrown
System.out.println(removed); // falseThere was a problem hiding this comment.
My bad, I misread the API.
…ntCnx.java code style Co-authored-by: tison <[email protected]>
eolivelli
left a comment
There was a problem hiding this comment.
do we have some report/gh issue about this error ?
This queue shouldn't be drained by multiple threads
|
I second @eolivelli that |
You're right.
After testing, I found that netty's implementation is not the same as JDK, only one thread is running. But in other words, in the context of concurrent programming specifications and static scanning of code, I think NPE checking should also be necessary. |
tisonkun
left a comment
There was a problem hiding this comment.
@eolivelli in another perspective, I agree the new diff of this patch is good as defensive programming.
+1 to merge
|
/pulsarbot run-failure-checks |
|
ping @eolivelli |
eolivelli
left a comment
There was a problem hiding this comment.
I don't want to block this patch.
thank you for your contribution @crossoverJie
But I still do think that we should't add code that is useless.
|
@eolivelli Thanks for your input. Merging... |
(cherry picked from commit 94a6d36)
(cherry picked from commit 94a6d36)
(cherry picked from commit 94a6d36)
Motivation
NPE can occur in extreme cases.
For example: when thread A is executing here:
pulsar/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ClientCnx.java
Line 1298 in b6f0f91
another thread has consumed all
requestTimeoutQueuedata.Modifications
Add NPE check
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
docdoc-requireddoc-not-neededdoc-complete