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

NestJS does not await asynchronous shutdown hooks in some circumstances #9649

Closed
4 of 15 tasks
andrew-sol opened this issue May 23, 2022 · 2 comments
Closed
4 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@andrew-sol
Copy link

andrew-sol commented May 23, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I use JaniAnttonen/winston-loki package.

After creating a transport, NestJS stops awaiting shutdown hooks. app.enableShutdownHooks() is enabled.
See the reproduction code repo.

app.module.ts:

import { Logger, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
  private readonly logger = new Logger(AppModule.name);

  async beforeApplicationShutdown(): Promise<void> {
    this.logger.log(1);

    await new Promise((resolve) => setTimeout(resolve, 1000));

    this.logger.log(2);
  }
}

main.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import LokiTransport from 'winston-loki';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.enableShutdownHooks();

  // comment out the following statement and shutdown hooks will be awaited
  const lokiTransport = new LokiTransport({
    host: 'http://qweasd',
  });

  await app.listen(3000);
}

bootstrap();

Only the first log appears.

Minimum reproduction code

https://github.com/andrew-sol/nestjs-reproduction-1

Steps to reproduce

  1. yarn install --frozen-lockfile or npm install
  2. yarn start:dev or npm run start:dev
  3. Ctrl+C

Expected behavior

Both logs from the shutdown hook should be printed.

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

JaniAnttonen/winston-loki

NestJS version

8.4.5

Packages versions

@nestjs/cli@8.2.6
@nestjs/common@8.4.5
@nestjs/core@8.4.5
@nestjs/platform-express@8.4.5
@nestjs/schematics@8.0.11
@nestjs/testing@8.4.5

Node.js version

16.14.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@andrew-sol andrew-sol added the needs triage This issue has not been looked into label May 23, 2022
@micalevisk
Copy link
Member

I believe this is duplicate of nestjs/nest-cli#1487

@kamilmysliwiec
Copy link
Member

winston-loki uses async-exit-hook which overrides the default framework's behavior. This issue should be reported there (in the winston-loki repo) instead. There's nothing we can do on the NestJS side.

@nestjs nestjs locked and limited conversation to collaborators May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants