Skip to content

Fix message might lost when use listener #406

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

Merged
merged 4 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 20 additions & 34 deletions src/Consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,28 @@ void MessageListenerProxy(Napi::Env env, Napi::Function jsCallback, MessageListe
Napi::Object msg = Message::NewInstance({}, data->cMessage);
Consumer *consumer = data->consumer;

// `consumer` might be null in certain cases, segmentation fault might happend without this null check. We
// need to handle this rare case in future.
if (consumer) {
Napi::Value ret;
try {
ret = jsCallback.Call({msg, consumer->Value()});
} catch (std::exception &exception) {
logMessageListenerError(consumer, exception.what());
}
Napi::Value ret;
try {
ret = jsCallback.Call({msg, consumer->Value()});
} catch (std::exception &exception) {
logMessageListenerError(consumer, exception.what());
}

if (ret.IsPromise()) {
Napi::Promise promise = ret.As<Napi::Promise>();
Napi::Function catchFunc = promise.Get("catch").As<Napi::Function>();
if (ret.IsPromise()) {
Napi::Promise promise = ret.As<Napi::Promise>();
Napi::Function catchFunc = promise.Get("catch").As<Napi::Function>();

ret = catchFunc.Call(promise, {Napi::Function::New(env, [consumer](const Napi::CallbackInfo &info) {
Napi::Error error = info[0].As<Napi::Error>();
logMessageListenerError(consumer, error.what());
})});
ret = catchFunc.Call(promise, {Napi::Function::New(env, [consumer](const Napi::CallbackInfo &info) {
Napi::Error error = info[0].As<Napi::Error>();
logMessageListenerError(consumer, error.what());
})});

promise = ret.As<Napi::Promise>();
Napi::Function finallyFunc = promise.Get("finally").As<Napi::Function>();
promise = ret.As<Napi::Promise>();
Napi::Function finallyFunc = promise.Get("finally").As<Napi::Function>();

finallyFunc.Call(
promise, {Napi::Function::New(env, [data](const Napi::CallbackInfo &info) { data->callback(); })});
return;
}
finallyFunc.Call(
promise, {Napi::Function::New(env, [data](const Napi::CallbackInfo &info) { data->callback(); })});
return;
}
data->callback();
}
Expand All @@ -111,7 +107,7 @@ void MessageListener(pulsar_consumer_t *rawConsumer, pulsar_message_t *rawMessag
std::shared_ptr<pulsar_message_t> cMessage(rawMessage, pulsar_message_free);
MessageListenerCallback *listenerCallback = (MessageListenerCallback *)ctx;

Consumer *consumer = (Consumer *)listenerCallback->consumer;
Consumer *consumer = static_cast<Consumer *>(listenerCallback->consumerFuture.get());

if (listenerCallback->callback.Acquire() != napi_ok) {
return;
Expand All @@ -135,7 +131,7 @@ void Consumer::SetListenerCallback(MessageListenerCallback *listener) {
}

if (listener != nullptr) {
listener->consumer = this;
listener->consumerPromise.set_value(this);
// If a consumer listener is set, the Consumer instance is kept alive even if it goes out of scope in JS
// code.
this->Ref();
Expand Down Expand Up @@ -168,23 +164,13 @@ struct ConsumerNewInstanceContext {
auto cConsumer = std::shared_ptr<pulsar_consumer_t>(rawConsumer, pulsar_consumer_free);
auto listener = consumerConfig->GetListenerCallback();

if (listener) {
// pause, will resume in OnOK, to prevent MessageListener get a nullptr of consumer
pulsar_consumer_pause_message_listener(cConsumer.get());
}

deferred->Resolve([cConsumer, consumerConfig, listener](const Napi::Env env) {
Napi::Object obj = Consumer::constructor.New({});
Consumer *consumer = Consumer::Unwrap(obj);

consumer->SetCConsumer(cConsumer);
consumer->SetListenerCallback(listener);

if (listener) {
// resume to enable MessageListener function callback
resume_message_listener(cConsumer.get());
}

return obj;
});
}
Expand Down
8 changes: 5 additions & 3 deletions src/MessageListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
#define MESSAGELISTENER_H

#include <napi.h>
#include <future>

struct MessageListenerCallback {
Napi::ThreadSafeFunction callback;

// Using consumer as void* since the ListenerCallback is shared between Config and Consumer.
void *consumer;
// Use future store consumer point, because need ensure sync.
std::promise<void *> consumerPromise;
std::shared_future<void *> consumerFuture;

MessageListenerCallback() : consumer(nullptr) {}
MessageListenerCallback() : consumerPromise(), consumerFuture(consumerPromise.get_future()) {}
};

#endif
Loading