Skip to content

[RabbitMQ] Unhandled AggregateError when connection is lost during pending AmqpConnection.request() timeoutΒ #1079

@rkarbowiak

Description

@rkarbowiak

Overview

When using AmqpConnection.request(), if the RabbitMQ connection is lost while a request is pending timeout, an unhandled AggregateError [ECONNREFUSED] crashes the application. This error occurs before the timeout error is thrown and cannot be caught with a standard try/catch block.​

Steps to Reproduce

  • Configure RabbitMQ module with valid connection settings
  • Make a request using amqp.requestwith a timeout (e.g., 5000ms)
  • While the request is waiting to timeout, close/stop the RabbitMQ broker
  • Observe the unhandled AggregateError [ECONNREFUSED] that crashes the application

Let me know whether should I prepare a reproduction repository, but similar case is in this issue already and it seems to have a reproduction repo.

Expected Behavior

The connection loss error should be caught and handled gracefully, allowing the application to continue running. The timeout error should be thrown and catchable by the caller's try/catch block.​

Actual Behavior

An unhandled AggregateError [ECONNREFUSED] is thrown, followed by the timeout error, crashing the entire application.

Error message:

AggregateError [ECONNREFUSED]: 
    at internalConnectMultiple (node:net:1139:18)
    at afterConnectMultiple (node:net:1714:7)
    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)
node_modules/@golevelup/nestjs-rabbitmq/lib/amqp/connection.js:212
            throw new Error(`Failed to receive response within timeout of ${timeout}ms for exchange "${requestOptions.exchange}" and routing key "${requestOptions.routingKey}"`);
                  ^

Error: Failed to receive response within timeout of 5000ms for exchange "test-exchange" and routing key "*"
    at <anonymous> (node_modules/@golevelup/nestjs-rabbitmq/src/amqp/connection.ts:431:15)
    at <anonymous> (node_modules/rxjs/src/internal/operators/map.ts:57:33)
    at OperatorSubscriber.OperatorSubscriber._this._next (node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13)
    at OperatorSubscriber.Subscriber.next (node_modules/rxjs/src/internal/Subscriber.ts:71:12)
    at <anonymous> (node_modules/rxjs/src/internal/operators/throwIfEmpty.ts:50:22)
    at OperatorSubscriber.OperatorSubscriber._this._next (node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13)
    at OperatorSubscriber.Subscriber.next (node_modules/rxjs/src/internal/Subscriber.ts:71:12)
    at <anonymous> (node_modules/rxjs/src/internal/operators/take.ts:60:26)
    at OperatorSubscriber.OperatorSubscriber._this._next (node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13)
    at OperatorSubscriber.Subscriber.next (node_modules/rxjs/src/internal/Subscriber.ts:71:12)

Environment

Node.js version: v23.11.0
@golevelup/nestjs-rabbitmq version: 6.0.1
NestJS version: 11.1.2

Related issues

#259
#686

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions