question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Messages to publish are lost when RabbitMQ connection fails

See original GitHub issue

I have this simple publication code:

const broker = await Rascal.BrokerAsPromised.create(Rascal.withDefaultConfig(config));

broker.on('error', (error) => {
    console.error(`Broker error: ${error.message}`);
});

setInterval(async function() {
      try {
        const publication = await broker.publish('pub', 'hello world');
        publication.on('error', (error) => {
            console.error(`Publication error: ${error.message}`);
        });
      } catch (error) {
        console.error(`Rascal config error: ${error.message}`);
      }
}, 1000);

It’s running fine until I shutdown the RabbitMQ server. Then I see this error in the logs:

Broker error: Connection closed: 320 (CONNECTION-FORCED) with message "CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'"

The messages are not published now, obviously. There is no Publication error in the logs though, which caused some confusion for me.

When I start the RabbitMQ server, I’d expect the in-meantime messages to be published after Rascal reconnects. This doesn’t happen, the messages are lost. If I understand the code correctly they’re queued in poolQueue: https://github.com/guidesmiths/rascal/blob/a68834d46ccc0b476d4530a21368ca2b234348d6/lib/amqp/Vhost.js#L211 but then after reconnection the queue is overridden with an empty one in https://github.com/guidesmiths/rascal/blob/a68834d46ccc0b476d4530a21368ca2b234348d6/lib/amqp/Vhost.js#L245

My workaround is to add a local queue and pause/resume it based on the connection status:

const broker = await Rascal.BrokerAsPromised.create(Rascal.withDefaultConfig(config));

const messagesToPublishQueue = async.queue((_, next) => {
	next();
}, 1);

messagesToPublishQueue.pause();

broker
    .on('error', (error) => {
        console.error(`Broker error: ${error.message}`);
    })
	.on('connect', () => {
		messagesToPublishQueue.resume();
	})
	.on('disconnect', () => {
		messagesToPublishQueue.pause();
	});

setInterval(async function() {
    messagesToPublishQueue.push(null, async () => {
      try {
        const publication = await broker.publish('pub', 'hello world');
        publication.on('error', (error) => {
            console.error(`Publication error: ${error.message}`);
        });
      } catch (error) {
        console.error(`Rascal config error: ${error.message}`);
      }
    });
}, 1000);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cressie176commented, Dec 20, 2019

Thanks for explaining again @rogatty, I understand the issue now. Agree it’s a problem that needs fixing.

0reactions
rogattycommented, Jan 9, 2020

Understood. It’s up to you if and how you want to document the current behaviour 🙂 If someone tries to debug what’s happening they’ll probably end up here and can learn from the discussion above.

Thanks again for fixing the issue so quickly!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why message can be lost when rabbitmq basic.publish return ok
Using standard AMQP 0-9-1, the only way to guarantee that a message isn't lost is by using transactions -- make the channel transactional...
Read more >
Reliability Guide - RabbitMQ
Without acknowledgements, message loss is possible during publish and consume operations and only at most once delivery is guaranteed. Detecting Dead TCP ...
Read more >
ConfirmChannel.publish does not fail (or hangs) if RabbitMQ ...
Short version: Problem: I want a publish to fail immediately so that clients know that they need to retry a send. If the...
Read more >
Types of Publishing Failures - RabbitMq Publishing Part 1
There are many scenarios where things can go wrong when publishing messages to RabbitMq - the connection can fail, the exchange might not ......
Read more >
MT and RabbitMQ: Connection failures and messages being ...
We are trying to track down the root cause for an extremely sporadic issue where messages are seemingly "lost". Our algorithm involves an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found