Properly closing a connection in Node.JS

2,651 views
Skip to first unread message

Andras Barthazi

unread,
May 21, 2017, 3:52:31 AM5/21/17
to rabbitmq-users
Hi,

I'm wondering how can I properly close a connection with Node.JS and amqplib.


If there are a lot of messages in the queue, and I'm terminating the connection, I can see this error:

 [x] Received '"jjdjdj"'
 [x] Received '"jjdjdj"'
 [x] Received '"jjdjdj"'
 [x] Received '"jjdjdj"'
 [*] Finished
(node:72547) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): IllegalOperationError: Channel closed
(node:72547) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any ideas what am I doing wrong?

Thanks,
  Andras

Michael Klishin

unread,
May 21, 2017, 4:37:18 AM5/21/17
to rabbitm...@googlegroups.com
I'm not an expert on promise libraries but the warning seems to say that you did not define
a handler for the error (rejection) path. However,

IllegalOperationError: Channel closed

suggests that you may be double closing a channel? A channel can be closed by the server in case
of certain errors, they are easy to post in server logs.

To cleanly close a connection you need to close conn, not just the channel. That's not
technically required but will avoid noise in server logs.


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

Andras Barthazi

unread,
May 21, 2017, 8:33:06 AM5/21/17
to rabbitm...@googlegroups.com
Hi Michael,

Thanks for your answer, it helped me figuring out the problem is with acking a message after the channel have been closed.
So if I catch the error with a try catch around ch.ack(msg);, then it's a bit better, but that message should be acked.


- introduced a counter variable processing -- if it's zero, then no messages are processed, so it's safe to close the channel
- added cancelling the consumer, it should stop new messages coming
- when no messages are processed anymore (retrying it every 100 ms), then I'm closing the channel and the connection

It seems to be working, but I'm interested in any thoughts about this solution.

Bye,
  Andras

To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.

To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

--
You received this message because you are subscribed to a topic in the Google Groups "rabbitmq-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rabbitmq-users/y0fm-97w0xM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages