Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app.startAllMicroservices hangs on bad connection to RMQ microservice #9749

Closed
4 of 15 tasks
Turentkm opened this issue Jun 9, 2022 · 6 comments
Closed
4 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@Turentkm
Copy link

Turentkm commented Jun 9, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When building hybrid application, in my case its graphql + RMQ microservice, the bootstrapping is done the same way as in documentation

async function bootstrap() {
const app = await NestFactory.create(AppModule);
  app.enableCors();
  app.useGlobalPipes(new ValidationPipe());
  Logger.log('Connecting microservics');
  app.connectMicroservice({
    transport: Transport.RMQ,
    options: {
      urls: [
        {
          protocol: 'amqp',
          hostname: process.env.RABBITMQ_HOST,
          port: 5672,
          username: process.env.RABBITMQ_USERNAME + 's', // intentional mangling of username
          password: process.env.RABBITMQ_PASSWORD,
        },
      ],
      queue: 'resources_queue',
      queueOptions: {
        durable: false,
      },
    },
  });
  Logger.log('Starting microservices');
  await app.startAllMicroservices();
  Logger.log('Finalizing start of microservices');
  await app.listen(3000);
}

when there is no connection to RMQ - wrong host/username/password, the connection process is being stuck, without any error output.

From the code snippet above, the output is as follows:

Debugger attached.
[Nest] 6504  - 06/09/2022, 3:44:55 PM     LOG [NestFactory] Starting Nest application...
[Nest] 6504  - 06/09/2022, 3:44:55 PM     LOG [InstanceLoader] AppModule dependencies initialized +60ms
[Nest] 6504  - 06/09/2022, 3:44:57 PM     LOG Connecting microservics
[Nest] 6504  - 06/09/2022, 3:44:58 PM     LOG Starting microservices

And the process just hangs.

Minimum reproduction code

https://github.com/Turentkm/test-rmq-connection

Steps to reproduce

  1. npm ci
  2. add .env with the variables for your RMQ.
  3. npm run start:dev

Expected behavior

Throw either operational error of end the process instead of eternal connections hanging.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

8.0.0

Packages versions

  "dependencies": {
    "@nestjs/common": "^8.0.0",
    "@nestjs/core": "^8.0.0",
    "@nestjs/microservices": "^8.4.6",
    "@nestjs/platform-express": "^8.0.0",
    "amqp-connection-manager": "^4.1.3",
    "amqplib": "^0.10.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "dotenv": "^16.0.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "27.5.0",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.0.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.1",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.0.0",
    "typescript": "^4.3.5"
  },

Node.js version

14.17.5

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@Turentkm Turentkm added the needs triage This issue has not been looked into label Jun 9, 2022
@micalevisk
Copy link
Member

maybe this is related with #9726

@delucca
Copy link
Contributor

delucca commented Jun 9, 2022

The problem happens because (for all servers) if the connection is not succeeded we never execute the callback on the start method for the servers. Check this:

public async start(callback?: () => void) {
this.server = this.createClient();
this.server.on(CONNECT_EVENT, () => {
if (this.channel) {
return;
}
this.channel = this.server.createChannel({
json: false,
setup: (channel: any) => this.setupChannel(channel, callback),
});
});
this.server.on(DISCONNECT_EVENT, (err: any) => {
this.logger.error(DISCONNECTED_RMQ_MESSAGE);
this.logger.error(err);
});
}

So, this promise is never resolved and the server hangs

I can work in a solution, @micalevisk don't know if you're already working on this :)

In any case, we would need to decide if we should handle this error silently or not. The optimal way would be breaking the entire app an throwing an error? Or we should fail, logging an error, but without preventing the app from being launched?

@micalevisk
Copy link
Member

I've been pretty busy lately. PRs are more than welcomed 😺

@delucca
Copy link
Contributor

delucca commented Jun 9, 2022

@micalevisk do you think we should throw an error and stop the app execution? Or we should simply log the error and continue with the bootstrap of the application?

In any case, I tried to add myself as assignee for this issue, but I don't have permissions for the repo. You can add me as assignee 😄 I'll fix this

@micalevisk
Copy link
Member

You can add me as assignee

don't worry. We don't use that feat xD (I don't have permissions to)


I guess we could log the error in v8, and raise it in v9

@kamilmysliwiec
Copy link
Member

Let's track this here #9751

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants