Producers or consumers may be closed after the client exits out of scope
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- javascript, node.js
- Domain
- backend, distributed-systems
Research direction
No source files or tests are named. Start by running the reproduction against a local Pulsar broker, then trace producer, consumer, and client lifetime handling in the Node.js client; done means a producer or consumer keeps its client usable after the outer function scope ends and a regression test prevents AlreadyClosed during continued sends.
Written by the indexing model from the issue text.
Description
Here is the reproduction code:
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:
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.
- Dominant language
- C++
- Stars
- 164
- Forks
- 99
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/pulsar-client-node
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
apache/pulsar-client-node#477 ·
-
公司网络无法访问海外站点,如何解决? Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
apache/pulsar-client-node#455 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
apache/pulsar-client-node#432 · 2 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 38/100
apache/pulsar-client-node#431 · 3 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
apache/pulsar-client-node#429 · 1 comment ·
All issues in apache/pulsar-client-node
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100