Skip to content
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

cpp: fix reference leak when reader create #7793

Merged
merged 2 commits into from
Aug 11, 2020
Merged

Conversation

jiazhai
Copy link
Member

@jiazhai jiazhai commented Aug 10, 2020

Motivation

User reports a valgrind error for client::createReader method:

==23308== 284,826 (160 direct, 284,666 indirect) bytes in 1 blocks are definitely lost in loss record 113 of 113
==23308==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==23308==    by 0x5303B4A: allocate (new_allocator.h:104)
==23308==    by 0x5303B4A: allocate (alloc_traits.h:351)
==23308==    by 0x5303B4A: __shared_count<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:499)
==23308==    by 0x5303B4A: __shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:957)
==23308==    by 0x5303B4A: shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:316)
==23308==    by 0x5303B4A: allocate_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:598)
==23308==    by 0x5303B4A: make_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader> > (shared_ptr.h:614)
==23308==    by 0x5303B4A: Promise (Future.h:91)
==23308==    by 0x5303B4A: pulsar::Client::createReader(std::string const&, pulsar::MessageId const&, pulsar::ReaderConfiguration const&, pulsar::Reader&) (Client.cc:142)
==23308==    by 0x401DDB: main (pulsarReader.cpp:92)
==23308== 

It seems the ReaderImpl has been tracked twice when call WaitForCallbackValue. this PR is to fix the issue.

Modifications

Verifying this change

ut passed.
valgrind found no issue:

==14758== LEAK SUMMARY:
==14758==    definitely lost: 0 bytes in 0 blocks
==14758==    indirectly lost: 0 bytes in 0 blocks
==14758==      possibly lost: 0 bytes in 0 blocks
==14758==    still reachable: 12,621 bytes in 145 blocks
==14758==         suppressed: 0 bytes in 0 blocks
==14758== 
==14758== For lists of detected and suppressed errors, rerun with: -s
==14758== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

@sijie sijie added this to the 2.7.0 milestone Aug 11, 2020
@jiazhai jiazhai changed the title [WIP]cpp: fix reference leak when reader create cpp: fix reference leak when reader create Aug 11, 2020
@sijie sijie merged commit 0e67fc5 into apache:master Aug 11, 2020
wolfstudy pushed a commit that referenced this pull request Aug 12, 2020
### Motivation
User reports a valgrind error for `client::createReader` method:

```
==23308== 284,826 (160 direct, 284,666 indirect) bytes in 1 blocks are definitely lost in loss record 113 of 113
==23308==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==23308==    by 0x5303B4A: allocate (new_allocator.h:104)
==23308==    by 0x5303B4A: allocate (alloc_traits.h:351)
==23308==    by 0x5303B4A: __shared_count<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:499)
==23308==    by 0x5303B4A: __shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:957)
==23308==    by 0x5303B4A: shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:316)
==23308==    by 0x5303B4A: allocate_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:598)
==23308==    by 0x5303B4A: make_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader> > (shared_ptr.h:614)
==23308==    by 0x5303B4A: Promise (Future.h:91)
==23308==    by 0x5303B4A: pulsar::Client::createReader(std::string const&, pulsar::MessageId const&, pulsar::ReaderConfiguration const&, pulsar::Reader&) (Client.cc:142)
==23308==    by 0x401DDB: main (pulsarReader.cpp:92)
==23308== 
```
It seems the `ReaderImpl` has been tracked twice when call WaitForCallbackValue. this PR is to fix the issue.

### Modifications

- fix WaitForCallbackValue which is changed in PR #3484.
- add test for the reference issue.

### Verifying this change
ut passed.
valgrind found no issue:
```
==14758== LEAK SUMMARY:
==14758==    definitely lost: 0 bytes in 0 blocks
==14758==    indirectly lost: 0 bytes in 0 blocks
==14758==      possibly lost: 0 bytes in 0 blocks
==14758==    still reachable: 12,621 bytes in 145 blocks
==14758==         suppressed: 0 bytes in 0 blocks
==14758== 
==14758== For lists of detected and suppressed errors, rerun with: -s
==14758== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
```

(cherry picked from commit 0e67fc5)
huangdx0726 pushed a commit to huangdx0726/pulsar that referenced this pull request Aug 24, 2020
### Motivation
User reports a valgrind error for `client::createReader` method:

```
==23308== 284,826 (160 direct, 284,666 indirect) bytes in 1 blocks are definitely lost in loss record 113 of 113
==23308==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==23308==    by 0x5303B4A: allocate (new_allocator.h:104)
==23308==    by 0x5303B4A: allocate (alloc_traits.h:351)
==23308==    by 0x5303B4A: __shared_count<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:499)
==23308==    by 0x5303B4A: __shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:957)
==23308==    by 0x5303B4A: shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:316)
==23308==    by 0x5303B4A: allocate_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:598)
==23308==    by 0x5303B4A: make_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader> > (shared_ptr.h:614)
==23308==    by 0x5303B4A: Promise (Future.h:91)
==23308==    by 0x5303B4A: pulsar::Client::createReader(std::string const&, pulsar::MessageId const&, pulsar::ReaderConfiguration const&, pulsar::Reader&) (Client.cc:142)
==23308==    by 0x401DDB: main (pulsarReader.cpp:92)
==23308== 
```
It seems the `ReaderImpl` has been tracked twice when call WaitForCallbackValue. this PR is to fix the issue.

### Modifications

- fix WaitForCallbackValue which is changed in PR apache#3484.
- add test for the reference issue.

### Verifying this change
ut passed.
valgrind found no issue:
```
==14758== LEAK SUMMARY:
==14758==    definitely lost: 0 bytes in 0 blocks
==14758==    indirectly lost: 0 bytes in 0 blocks
==14758==      possibly lost: 0 bytes in 0 blocks
==14758==    still reachable: 12,621 bytes in 145 blocks
==14758==         suppressed: 0 bytes in 0 blocks
==14758== 
==14758== For lists of detected and suppressed errors, rerun with: -s
==14758== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
```
lbenc135 pushed a commit to lbenc135/pulsar that referenced this pull request Sep 5, 2020
### Motivation
User reports a valgrind error for `client::createReader` method:

```
==23308== 284,826 (160 direct, 284,666 indirect) bytes in 1 blocks are definitely lost in loss record 113 of 113
==23308==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==23308==    by 0x5303B4A: allocate (new_allocator.h:104)
==23308==    by 0x5303B4A: allocate (alloc_traits.h:351)
==23308==    by 0x5303B4A: __shared_count<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:499)
==23308==    by 0x5303B4A: __shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:957)
==23308==    by 0x5303B4A: shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:316)
==23308==    by 0x5303B4A: allocate_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:598)
==23308==    by 0x5303B4A: make_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader> > (shared_ptr.h:614)
==23308==    by 0x5303B4A: Promise (Future.h:91)
==23308==    by 0x5303B4A: pulsar::Client::createReader(std::string const&, pulsar::MessageId const&, pulsar::ReaderConfiguration const&, pulsar::Reader&) (Client.cc:142)
==23308==    by 0x401DDB: main (pulsarReader.cpp:92)
==23308== 
```
It seems the `ReaderImpl` has been tracked twice when call WaitForCallbackValue. this PR is to fix the issue.

### Modifications

- fix WaitForCallbackValue which is changed in PR apache#3484.
- add test for the reference issue.

### Verifying this change
ut passed.
valgrind found no issue:
```
==14758== LEAK SUMMARY:
==14758==    definitely lost: 0 bytes in 0 blocks
==14758==    indirectly lost: 0 bytes in 0 blocks
==14758==      possibly lost: 0 bytes in 0 blocks
==14758==    still reachable: 12,621 bytes in 145 blocks
==14758==         suppressed: 0 bytes in 0 blocks
==14758== 
==14758== For lists of detected and suppressed errors, rerun with: -s
==14758== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
```
lbenc135 pushed a commit to lbenc135/pulsar that referenced this pull request Sep 5, 2020
### Motivation
User reports a valgrind error for `client::createReader` method:

```
==23308== 284,826 (160 direct, 284,666 indirect) bytes in 1 blocks are definitely lost in loss record 113 of 113
==23308==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==23308==    by 0x5303B4A: allocate (new_allocator.h:104)
==23308==    by 0x5303B4A: allocate (alloc_traits.h:351)
==23308==    by 0x5303B4A: __shared_count<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:499)
==23308==    by 0x5303B4A: __shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:957)
==23308==    by 0x5303B4A: shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:316)
==23308==    by 0x5303B4A: allocate_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:598)
==23308==    by 0x5303B4A: make_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader> > (shared_ptr.h:614)
==23308==    by 0x5303B4A: Promise (Future.h:91)
==23308==    by 0x5303B4A: pulsar::Client::createReader(std::string const&, pulsar::MessageId const&, pulsar::ReaderConfiguration const&, pulsar::Reader&) (Client.cc:142)
==23308==    by 0x401DDB: main (pulsarReader.cpp:92)
==23308== 
```
It seems the `ReaderImpl` has been tracked twice when call WaitForCallbackValue. this PR is to fix the issue.

### Modifications

- fix WaitForCallbackValue which is changed in PR apache#3484.
- add test for the reference issue.

### Verifying this change
ut passed.
valgrind found no issue:
```
==14758== LEAK SUMMARY:
==14758==    definitely lost: 0 bytes in 0 blocks
==14758==    indirectly lost: 0 bytes in 0 blocks
==14758==      possibly lost: 0 bytes in 0 blocks
==14758==    still reachable: 12,621 bytes in 145 blocks
==14758==         suppressed: 0 bytes in 0 blocks
==14758== 
==14758== For lists of detected and suppressed errors, rerun with: -s
==14758== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
```
lbenc135 pushed a commit to lbenc135/pulsar that referenced this pull request Sep 5, 2020
### Motivation
User reports a valgrind error for `client::createReader` method:

```
==23308== 284,826 (160 direct, 284,666 indirect) bytes in 1 blocks are definitely lost in loss record 113 of 113
==23308==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==23308==    by 0x5303B4A: allocate (new_allocator.h:104)
==23308==    by 0x5303B4A: allocate (alloc_traits.h:351)
==23308==    by 0x5303B4A: __shared_count<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:499)
==23308==    by 0x5303B4A: __shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr_base.h:957)
==23308==    by 0x5303B4A: shared_ptr<std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:316)
==23308==    by 0x5303B4A: allocate_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader>, std::allocator<pulsar::InternalState<pulsar::Result, pulsar::Reader> > > (shared_ptr.h:598)
==23308==    by 0x5303B4A: make_shared<pulsar::InternalState<pulsar::Result, pulsar::Reader> > (shared_ptr.h:614)
==23308==    by 0x5303B4A: Promise (Future.h:91)
==23308==    by 0x5303B4A: pulsar::Client::createReader(std::string const&, pulsar::MessageId const&, pulsar::ReaderConfiguration const&, pulsar::Reader&) (Client.cc:142)
==23308==    by 0x401DDB: main (pulsarReader.cpp:92)
==23308== 
```
It seems the `ReaderImpl` has been tracked twice when call WaitForCallbackValue. this PR is to fix the issue.

### Modifications

- fix WaitForCallbackValue which is changed in PR apache#3484.
- add test for the reference issue.

### Verifying this change
ut passed.
valgrind found no issue:
```
==14758== LEAK SUMMARY:
==14758==    definitely lost: 0 bytes in 0 blocks
==14758==    indirectly lost: 0 bytes in 0 blocks
==14758==      possibly lost: 0 bytes in 0 blocks
==14758==    still reachable: 12,621 bytes in 145 blocks
==14758==         suppressed: 0 bytes in 0 blocks
==14758== 
==14758== For lists of detected and suppressed errors, rerun with: -s
==14758== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
```
BewareMyPower added a commit to BewareMyPower/pulsar that referenced this pull request Mar 24, 2022
Fixes apache#14848

Fixes apache#14719

### Motivation

apache#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. apache#14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.
BewareMyPower added a commit to BewareMyPower/pulsar that referenced this pull request Mar 31, 2022
Fixes apache#14848

Fixes apache#14719

### Motivation

apache#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. apache#14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.
BewareMyPower added a commit to BewareMyPower/pulsar that referenced this pull request Apr 1, 2022
Fixes apache#14848

Fixes apache#14719

### Motivation

apache#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. apache#14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.
lhotari pushed a commit that referenced this pull request Apr 1, 2022
Fixes #14848
Fixes #14719

### Motivation

#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. #14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.
BewareMyPower added a commit that referenced this pull request Apr 2, 2022
Fixes #14848
Fixes #14719

### Motivation

#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. #14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.

(cherry picked from commit f84ff57)
BewareMyPower added a commit that referenced this pull request Apr 2, 2022
Fixes #14848
Fixes #14719

### Motivation

#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. #14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.

(cherry picked from commit f84ff57)
BewareMyPower added a commit that referenced this pull request Apr 2, 2022
Fixes #14848
Fixes #14719

### Motivation

#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. #14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.

(cherry picked from commit f84ff57)
Nicklee007 pushed a commit to Nicklee007/pulsar that referenced this pull request Apr 20, 2022
Fixes apache#14848
Fixes apache#14719

### Motivation

apache#7793 introduced a `testReferenceLeak` to avoid cyclic referenece of the
reader. However, it adds a unused field `readerImplWeakPtr_` only for
tests. The access to this field is not thread safe that the write
operation happens in `handleConsumerCreated` while the read operation
can happen anywhere via the getter. So there is a little chance that
`readerPtr` in `testReferenceLeak` doesn't point to the right object.

In addition, we should only guarantee the reference count becomes 0
after the producer, consumer or reader goes out of its scope. apache#14797
adds a `ClientTest.testReferenceCount` but it's also flaky. It's caused
by the shared pointer of `ProducerImpl` is published to another thread
via `shared_from_this()` but the test has a strong expectation that the
reference count is exactly 1.

### Modifications

- Remove `readerImplWeakPtr_` from `ReaderImpl` and get the weak pointer
  from `Reader` directly by adding a method to `PulsarFriend`.
- Add the check of reader's reference count to `testReferenceCount` and
  remove the redundant `testReferenceLeak`.
- Instead of asserting the reference count of producer/consumer/reader
  is 1, just assume the it's greater than 0.
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.

None yet

2 participants