apache / apache/pulsar-client-node

Producers or consumers may be closed after the client exits out of scope

未关闭
#399 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C++
星标
164
派生
98
PR 合并指标
30 天内没有已合并 PR

描述

Here is the reproduction code:
```js
const Pulsar = require('pulsar-client');

(async () => {
// Create a client
const client = new Pulsar.Client({
serviceUrl: 'pulsar://localhost:6650'
});

// Create a producer
const producer = await client.createProducer({
topic: 'persistent://public/default/my-topic',
});

const sendRecords = async () => {
// Send a message
await producer.send({
data: Buffer.from("hello")
});

console.log("sent hello")
setTimeout(()=>sendRecords(), 1000)
}


await sendRecords();
})();
```

The output would be like:
```
➜ node node index.js
sent hello
sent hello
sent hello
sent hello
sent hello
sent hello
sent hello
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^

[Error: Failed to send message: AlreadyClosed]

Node.js v18.19.0
```
After the client exits the outer function's scope, it will eventually be garbage collected. This closes the producers and causes the AlreadyClosed issue.

A workaround is to pass the client ref to the sendRecords function:
```js
const sendRecords = async (client) => {
// Send a message
await producer.send({
data: Buffer.from("hello")
});

console.log("sent hello")
setTimeout(()=>sendRecords(client), 1000)
}


await sendRecords(client);
```

And it works.

A better approach is to keep a reference to the client inside the producer or consumer. This way, as long as we hold a reference to the producer or consumer, the client object will not be garbage collected.

贡献指南

这个仓库没有索引到贡献指南

调研方向

未指定源文件或测试。首先针对本地 Pulsar broker 运行复现,然后跟踪 Node.js 客户端中 producer、consumer 和 client 的生命周期处理;当外层函数作用域结束后,producer 或 consumer 仍能保持其 client 可用,并且回归测试能够防止在持续发送期间出现 AlreadyClosed 时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
backend, distributed-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。