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

fix: ReaderListenerProxy will make a segfault #376

Merged
merged 1 commit into from
Apr 9, 2024
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
24 changes: 13 additions & 11 deletions src/Reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ struct ReaderListenerProxyData {
void ReaderListenerProxy(Napi::Env env, Napi::Function jsCallback, ReaderListenerProxyData *data) {
Napi::Object msg = Message::NewInstance({}, data->cMessage);
Reader *reader = data->reader;

Napi::Value ret = jsCallback.Call({msg, reader->Value()});
if (ret.IsPromise()) {
Napi::Promise promise = ret.As<Napi::Promise>();
Napi::Value thenValue = promise.Get("then");
if (thenValue.IsFunction()) {
Napi::Function then = thenValue.As<Napi::Function>();
Napi::Function callback =
Napi::Function::New(env, [data](const Napi::CallbackInfo &info) { data->callback(); });
then.Call(promise, {callback});
return;
// `reader` might be null in certain cases, segmentation fault might happend without this null check.
if (reader) {
Napi::Value ret = jsCallback.Call({msg, reader->Value()});
if (ret.IsPromise()) {
Napi::Promise promise = ret.As<Napi::Promise>();
Napi::Value thenValue = promise.Get("then");
if (thenValue.IsFunction()) {
Napi::Function then = thenValue.As<Napi::Function>();
Napi::Function callback =
Napi::Function::New(env, [data](const Napi::CallbackInfo &info) { data->callback(); });
then.Call(promise, {callback});
return;
}
}
}
data->callback();
Expand Down
41 changes: 41 additions & 0 deletions tests/reader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,46 @@ const baseUrl = 'http://localhost:8080';
await reader.close();
await client.close();
});

test('Reader should not throw segmentation fault when create and close', async () => {
const NUM_ITS = 1000;
const its = Array.from({ length: NUM_ITS }, (_, i) => i);

const client = new Pulsar.Client({
serviceUrl: 'pulsar://localhost:6650',
});

const producer = await client.createProducer({
topic: 'persistent://public/default/my-topic',
sendTimeoutMs: 30000,
batchingEnabled: true,
});

// Send messages
for (let i = 0; i < 10; i += 1) {
const msg = `my-message-${i}`;
producer.send({
data: Buffer.from(msg),
});
console.log(`Sent message: ${msg}`);
}
await producer.flush();

await Promise.all(
its.map(async () => {
const reader = await client.createReader({
topic: 'persistent://public/default/my-topic',
startMessageId: Pulsar.MessageId.earliest(),
listener: (message) => {
console.log(message.getData().toString());
},
});
await reader.close();
}),
);
await producer.close();
await client.close();
expect(true).toBe(true);
});
});
})();
Loading