You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constPulsar=require('pulsar-client');(async()=>{// Create a clientconstclient=newPulsar.Client({serviceUrl: 'pulsar://localhost:6650'});// Create a producerconstproducer=awaitclient.createProducer({topic: 'persistent://public/default/my-topic',});constsendRecords=async()=>{// Send a messageawaitproducer.send({data: Buffer.from("hello")});console.log("sent hello")setTimeout(()=>sendRecords(),1000)}awaitsendRecords();})();
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:
constsendRecords=async(client)=>{// Send a messageawaitproducer.send({data: Buffer.from("hello")});console.log("sent hello")setTimeout(()=>sendRecords(client),1000)}awaitsendRecords(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.
The text was updated successfully, but these errors were encountered:
Here is the reproduction code:
The output would be like:
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:
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.
The text was updated successfully, but these errors were encountered: