[fix][client-cpp] Fix Reader segfault when messageListenerThreads=0#553
Open
zhanglistar wants to merge 2 commits intoapache:mainfrom
Open
[fix][client-cpp] Fix Reader segfault when messageListenerThreads=0#553zhanglistar wants to merge 2 commits intoapache:mainfrom
zhanglistar wants to merge 2 commits intoapache:mainfrom
Conversation
added 2 commits
March 16, 2026 20:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #500
Motivation
When using a Reader with
ClientConfiguration::setMessageListenerThreads(0)(or when the listener executor is otherwise null), the application can crash with a segmentation fault inConsumerImpl::messageReceived(). Two root causes were identified:ExecutorServiceProvider(0) – When
getMessageListenerThreads()is 0, the provider is constructed with an empty executor list. Inget(), the code doesidx %= executors_.size(), which is undefined behavior whensize() == 0(division by zero). This can lead to wrong or null executor pointers.Null listenerExecutor_ – The Reader creates its internal consumer with a null
ExecutorServicePtr. The consumer then falls back toclient->getListenerExecutorProvider()->get(). If that returns null (e.g. due to the above),messageReceivedandexecuteNotifyCallbackcalllistenerExecutor_->postWork(...)without a null check, causing a null pointer dereference (SIGSEGV at address 0x0).This was observed in the wild with Reader usage (e.g. periodic
hasMessageAvailable()andreadNext()) under moderate to high load or long runtime, with stack traces pointing atpulsar::ConsumerImpl::messageReceived.Modifications
lib/ExecutorService.cc
executors_(std::max(1, nthreads))so the executor list is never empty whennthreads == 0, avoiding undefined behavior inget().get(), add an earlyif (executors_.empty()) return nullptr;guard before usingexecutors_.size().#include <algorithm>forstd::max.lib/ConsumerImpl.cc
messageReceived(), after the closing-state check, add a check for nulllistenerExecutor_: log an error, callincreaseAvailablePermits(cnx), and return to avoid dereferencing null.messageListener_block, only calllistenerExecutor_->postWork(...)whenlistenerExecutor_is non-null.executeNotifyCallback(), when notifying a pending async receive: iflistenerExecutor_is non-null, post the callback as before; otherwise callnotifyPendingReceivedCallback(...)on the current thread so the callback is still invoked and no null dereference occurs.Verifying this change
This change added tests and can be verified as follows:
ExecutorServiceProvider(0), callsget()three times, and asserts each return is non-null. Confirms that 0-thread configuration no longer leads to undefined behavior and that a valid executor is always returned.ClientwithsetMessageListenerThreads(0), creates a Reader, produces several messages, then in a loop callshasMessageAvailable()andreadNext()until all messages are consumed. Asserts the correct count and content. Verifies that Reader path with 0 listener threads does not segfault and delivers messages correctly.Run the unit test (no broker):
./tests/pulsar-tests --gtest_filter='ExecutorServiceProviderTest.ZeroThreadsReturnsValidExecutor'Run the Reader integration test (with broker at
pulsar://localhost:6650):./tests/pulsar-tests --gtest_filter='ReaderTest.testReaderWithZeroMessageListenerThreads'Documentation
doc-requireddoc-not-needed(Behavior change is internal and backward-compatible: 0 listener threads is now handled safely; no API or user-facing doc update required.)
docdoc-complete