Skip to content

[fix][client] Fix possible npe - #18406

Merged
tisonkun merged 5 commits into
apache:masterfrom
crossoverJie:fix-possible-npe
Nov 15, 2022
Merged

tisonkun merged 5 commits into
apache:masterfrom
crossoverJie:fix-possible-npe

Conversation

@crossoverJie

Copy link
Copy Markdown
Member

Motivation

NPE can occur in extreme cases.
For example: when thread A is executing here:


another thread has consumed all requestTimeoutQueue data.

Modifications

Add NPE check

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • Anything that affects deployment

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Nov 10, 2022
@codecov-commenter

codecov-commenter commented Nov 10, 2022 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.19%. Comparing base (b31c5a6) to head (2f9cfe9).
⚠️ Report is 3357 commits behind head on master.

Files with missing lines Patch % Lines
.../java/org/apache/pulsar/client/impl/ClientCnx.java 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             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     
Flag Coverage Δ
unittests 47.19% <0.00%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../java/org/apache/pulsar/client/impl/ClientCnx.java 29.97% <0.00%> (-0.20%) ⬇️

... and 49 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tisonkun tisonkun left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..

@tisonkun tisonkun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM.

@tisonkun

Copy link
Copy Markdown
Member

But still it seems weird.

If this patch works, shall we recheck the request polled not fit isTimedOut? Otherwise, who is competing here?

@@ -1296,6 +1296,9 @@ private void checkRequestTimeout() {
break;
}
request = requestTimeoutQueue.poll();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
            }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@crossoverJie crossoverJie Nov 10, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove() will throw a NoSuchElementException when the queue is empty.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            request = requestTimeoutQueue.poll();
            if (request == null) {
                // the request has been removed by another thread
                continue;
            }

Wouldn't this be better?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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); // false

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I misread the API.

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have some report/gh issue about this error ?

This queue shouldn't be drained by multiple threads

@tisonkun

Copy link
Copy Markdown
Member

I second @eolivelli that scheduleAtFixedRate should keep the submitted task executed individually. That is, before the current checkRequestTimeout returns, there's no new execution to checkRequestTimeout when be triggered.

@crossoverJie

Copy link
Copy Markdown
Member Author

I second @eolivelli that scheduleAtFixedRate should keep the submitted task executed individually. That is, before the current checkRequestTimeout returns, there's no new execution to checkRequestTimeout when be triggered.

You're right.

io.netty.util.concurrent.EventExecutorGroup#scheduleAtFixedRate
java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate

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 tisonkun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eolivelli in another perspective, I agree the new diff of this patch is good as defensive programming.

+1 to merge

@crossoverJie

Copy link
Copy Markdown
Member Author

​/pulsarbot run-failure-checks

@tisonkun
tisonkun requested a review from eolivelli November 11, 2022 14:13
@tisonkun

Copy link
Copy Markdown
Member

ping @eolivelli

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tisonkun

Copy link
Copy Markdown
Member

@eolivelli Thanks for your input.

Merging...

@tisonkun
tisonkun merged commit 94a6d36 into apache:master Nov 15, 2022
congbobo184 pushed a commit that referenced this pull request Nov 17, 2022
@congbobo184 congbobo184 added the cherry-picked/branch-2.9 Archived: 2.9 is end of life label Nov 17, 2022
congbobo184 pushed a commit that referenced this pull request Dec 7, 2022
liangyepianzhou pushed a commit that referenced this pull request Dec 14, 2022
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request Jan 10, 2023
(cherry picked from commit 94a6d36)
(cherry picked from commit 581c2a3)
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request Jan 11, 2023
(cherry picked from commit 94a6d36)
(cherry picked from commit 581c2a3)
@Technoboy- Technoboy- added this to the 3.0.0 milestone Feb 8, 2023
Technoboy- pushed a commit that referenced this pull request Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants