postwait / node-amqp

[UNMAINTAINED] node-amqp is an AMQP client for nodejs
MIT License
1.7k stars 357 forks source link

connection.disconnect() fires the error handler with connection reset #462

Open Gilwyad opened 6 years ago

Gilwyad commented 6 years ago
let connection = amqp.createConnection(...);
connection.on('ready', function () {
    connection.exchange(...,
                        function () {
                            connection.queue(...,
                                             function (q, messageCount, consumerCount) {
                                                 connection.disconnect();
                                             });
                        });
});

connection.on('error', function(e) {
    console.log(e);
}

How should I cleanly disconnect while also handling errors?

soichih commented 6 years ago

Same issue. When I call connection.disconnect() I receive an error event.

 Error: read ECONNRESET
    at exports._errnoException (util.js:1020:11)
    at TCP.onread (net.js:568:26)

I have a script that uses amqp and I need to terminate it after all processing is done. However, calling disconnect won't kill the amqp's even loop(?) so my script keeps running. Calling disconnect() doesn't seem to end the event loop in this case.

mcgG commented 6 years ago

Same issue, calling disconnect() will trigger reconnect() so a new socket will open, this will cause mocha (>= 3.x) unit test cannot exit properly without --exit

mcgG commented 6 years ago

A temporary way to fix this issue is to call self.connection.setImplOptions({reconnect: false}); before connection.disconnect() Then, connection will be reconnected by disconnect()'s exception

rraghav773 commented 6 years ago

hey guys, I have found a fix. According to my understanding,when we call disconnect it sends "client disconnect" first and it receives "connectionCloseOk". On receiving the event, it first tries to call this.socket.end() function which is throwing the and possible reason for that is socket is no more available to communicate with server.

The fix is comment out or remove this.socket.end() and this.socket.destroy() will automatically destroys the socket, which is what a client intends when it calls a disconnect function. Finally, it would look like this

case methods.connectionCloseOk: debug && debug("Received close-ok from server, closing socket");
this.socket.destroy(); break;

As I am beginner, correct me if something is wrong with this,

soichih commented 5 years ago

Is there any update on this issue? I am still hit by this issue. (I am just killing the App manually at the end)