Skip to content

Commit 9f9314a

Browse files
Fix segmentation fault during the destruction of ConsumerImpl (#121)
### Motivation When I ran the tests of Python wrapper in my local env, I observed a segmentation fault. See the key stacktrace: ``` #3 0x00007ffff6d742c5 in std::unique_lock<std::mutex>::lock() () from /usr/local/lib/python3.8/dist-packages/_pulsar.cpython-38-x86_64-linux-gnu.so #4 0x00007ffff6d72523 in std::unique_lock<std::mutex>::unique_lock(std::mutex&) () from /usr/local/lib/python3.8/dist-packages/_pulsar.cpython-38-x86_64-linux-gnu.so #5 0x00007ffff67de193 in pulsar::ClientImpl::newRequestId (this=0x0) at /home/xyz/github.com/apache/pulsar-client-cpp/lib/ClientImpl.cc:644 #6 0x00007ffff685d2c2 in pulsar::ConsumerImpl::~ConsumerImpl (this=0x7fff9800f9e0, __in_chrg=<optimized out>) at /home/xyz/github.com/apache/pulsar-client-cpp/lib/ConsumerImpl.cc:116 ``` In the destructor of `ConsumerImpl`, `client->newRequestId` might be called. However, `client` might be a null pointer because it's returned by `std::weak_ptr::lock()`. ### Modifications Add null check to avoid the segfault.
1 parent de94bc5 commit 9f9314a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/ConsumerImpl.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ ConsumerImpl::~ConsumerImpl() {
127127

128128
ClientConnectionPtr cnx = getCnx().lock();
129129
ClientImplPtr client = client_.lock();
130-
int requestId = client->newRequestId();
131-
if (cnx) {
130+
if (client && cnx) {
131+
int requestId = client->newRequestId();
132132
cnx->sendRequestWithId(Commands::newCloseConsumer(consumerId_, requestId), requestId);
133133
cnx->removeConsumer(consumerId_);
134134
LOG_INFO(getName() << "Closed consumer for race condition: " << consumerId_);
135+
} else {
136+
LOG_WARN(getName() << "Client is destroyed and cannot send the CloseConsumer command");
135137
}
136138
}
137139
shutdown();

0 commit comments

Comments
 (0)