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.

Message handling infinite loop problem

See original GitHub issue

Hi, I created a new subscription according to the following document, and then after I return new Nack(), I still receive the previous message indefinitely. Is this the problem I set?

dependencies:

Message Handling NestJS Plus provides sane defaults for message handling with automatic acking of messages that have been successfully processed by either RPC or PubSub handlers. However, there are situtations where an application may want to Negatively Acknowledge (or Nack) a message. To support this, the library exposes the Nack object which when returned from a handler allows a developer to control the message handling behavior. Simply return a Nack instance to negatively acknowledge the message.

By default, messages that are Nacked will not be requeued. However, if you would like to requeue the message so that another handler has an opportunity to process it use the optional requeue constructor argument set to true.

import { RabbitRPC } from '@golevelup/nestjs-rabbitmq';
import { Injectable } from '@nestjs/common';

@Injectable()
export class MessagingService {
  @RabbitRPC({
    exchange: 'exchange1',
    routingKey: 'rpc-route',
    queue: 'rpc-queue'
  })
  public async rpcHandler(msg: {}) {
    return {
      if (someCondition) {
        return 42;
      } else if (requeueCondition) {
        return new Nack(true);
      } else {
        // Will not be requeued
        return new Nack();
      }
    };
  }
}

my code:

import { RabbitMQModule } from '@nestjs-plus/rabbitmq';
import { Module } from '@nestjs/common';
import { MessagingService } from './messaging.service';
import { MessagingController } from './messaging.controller';
import MSG from './msg.constant';

@Module({
  imports: [
    RabbitMQModule.forRoot({
      exchanges: [
        {
          name: 'emailExchange',
          type: 'topic',
        }
      ],
      uri: 'amqp://root:123456@localhost:5672',
    }),
  ],
  providers: [MessagingService],
  controllers: [MessagingController],
})
export class MessagingModule {}
import { Injectable } from '@nestjs/common';
import { RabbitSubscribe, Nack, RabbitRPC } from '@nestjs-plus/rabbitmq';
@Injectable()
export class MessagingService {
  @RabbitSubscribe( {
      exchange: 'emailExchange',
      routingKey: 'emailRouting',
      queue: 'emailQueue',
  }  )
  public async subscribeHandler(payload: {}) {
    console.log(`Subscribe Received message: ${JSON.stringify(payload)}`);
    return new Nack();
  }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
chromeycommented, Feb 18, 2022

Hi @WonderPanda

I just stumbled over this exact problem. Do you still stand by your assessment that using the ClassSerializerInterceptor globally is discouraged? In particular, you mention that global interceptors are applied to all Nest providers, but the Nest docs state that this only true for “every controller and every route handler”, and that’s also my experience - which would mean they in fact only act at network boundaries.

So in light of this, would you reconsider updating the Nack check?

0reactions
WonderPandacommented, Apr 23, 2020

@sjmittal The link you’ve provided is a bit misleading. Interceptors are often used in conjunction with transforming responses to be sent over the network as one of the primary use cases for NestJS is building REST APIs. However, Interceptors make no distinction in behavior based on whether they’re being used at a network boundary or not.

They simply grab the result produced by the method they are “intercepting” and then do something to that payload. There’s no reason to be applying a serialization interceptor globally on all your services. You are actually incurring a performance penalty at the moment because all your messages from Nest Providers need to be processed an additional time. It would be much better for you to configure your serialization interceptor on all of your Controllers so it handles outbound API traffic instead of all internal.

I’m going to close this as it’s not a problem with this library and can be fixed with proper use of Interceptors. However, I have some thoughts about possibly making the Nack check more intelligent so I may add something to the library regardless depending on my availability. If I improve the Nack check in the future I’ll update you in this thread

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite loop occurs when using LEAVE trigger and MESSAGE ...
An infinite loop may occur in an application which uses multiple embedded windows. The loop may occur when a LEAVE trigger executes the...
Read more >
Infinite message handling loop due to error queue deletion
Handler goes into infinite loop of handling the same message. Nothing goes to error queue. The text was updated successfully, but these errors ......
Read more >
NServiceBus Message Handler Endless Loop - Stack Overflow
We've looked at the code and it appears to be endless loop bug in the message handler. Is there any way to configure...
Read more >
Troubleshooting Infinite loops - IBM
An infinite loop can result. To resolve this problem, do one of the following: Message Limit. Specify a positive value to cause the...
Read more >
How to handle infinite loops within user code - CodeSignal
Infinite loops are a pain. In general, running an infinite loop can eat up your computer's resources and then freeze your IDE—and sometimes ......
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