apache / apache/pulsar-client-node

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

Ouverte
#399 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
C++
Étoiles
164
Forks
98
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Aucun fichier source ni test n’est nommé. Commencez par exécuter la reproduction contre un broker Pulsar local, puis suivez la gestion de la durée de vie du producer, du consumer et du client dans le client Node.js ; le travail est considéré comme terminé lorsqu’un producer ou un consumer conserve son client utilisable après la fin de la portée de la fonction externe et qu’un test de régression empêche AlreadyClosed lors de l’envoi continu.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, node.js
Domaine
backend, distributed-systems
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.