apache / apache/pulsar-client-node

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

Abierto
#399 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
C++
Estrellas
164
Forks
98
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

No se nombran archivos fuente ni pruebas. Empieza ejecutando la reproducción contra un broker de Pulsar local y, después, sigue el manejo del ciclo de vida del producer, consumer y client en el cliente de Node.js; se considera completado cuando un producer o consumer mantiene utilizable su client después de que termina el ámbito de la función externa y una prueba de regresión evita AlreadyClosed durante los envíos continuados.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, node.js
Área
backend, distributed-systems
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.