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] Incorrect shutdown #9237

Closed
mark-b-ab opened this issue Mar 8, 2022 · 8 comments
Closed

[nestjs] Incorrect shutdown #9237

mark-b-ab opened this issue Mar 8, 2022 · 8 comments
Assignees
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@mark-b-ab
Copy link

Starting 13.8.4 (when @nrwl/node:execute was replaced by @nrwl/node:node) Nx incorrectly handles shutdown on NestJS apps with shutdown hook.

Steps to Reproduce

  1. Create an empty NestJS app using Nx
  2. Use the following AppModule
  3. Enable shutdown hooks
  4. Start the app using nx serve
  5. Send SIGTERM (Ctrl + C)

Current Behavior

1 log message:

  • onModuleDestroy before wait

Expected Behavior

3 log messages:

  • onModuleDestroy before wait
  • onModuleDestroy after wait
  • onApplicationShutdown

Environment

   Node : 17.5.0
   OS   : darwin arm64
   yarn : 3.2.0
   
   nx : 13.8.5
   @nrwl/angular : undefined
   @nrwl/cli : 13.8.5
   @nrwl/cypress : undefined
   @nrwl/detox : undefined
   @nrwl/devkit : 13.8.5
   @nrwl/eslint-plugin-nx : 13.8.5
   @nrwl/express : undefined
   @nrwl/jest : 13.8.5
   @nrwl/js : 13.8.5
   @nrwl/linter : 13.8.5
   @nrwl/nest : 13.8.5
   @nrwl/next : undefined
   @nrwl/node : 13.8.5
   @nrwl/nx-cloud : undefined
   @nrwl/react : undefined
   @nrwl/react-native : undefined
   @nrwl/schematics : undefined
   @nrwl/storybook : undefined
   @nrwl/tao : 13.8.5
   @nrwl/web : undefined
   @nrwl/workspace : 13.8.5
   typescript : 4.6.2
   rxjs : 7.5.4
   ---------------------------------------
   Community plugins:
         @nx-tools/nx-docker: 2.3.0
@AgentEnder AgentEnder added the scope: node Issues related to Node, Express, NestJS support for Nx label Mar 11, 2022
@nartc nartc changed the title Incorrect shutdown [nestjs] Incorrect shutdown Mar 21, 2022
@mveeravalli
Copy link

Facing the same issue, any temporary fix available ?

@mark-b-ab
Copy link
Author

nope(

@mark-b-ab
Copy link
Author

What I did is reverting @nrwl/node to 13.8.3

@mark-b-ab
Copy link
Author

Found the issue: Multiple signals are received by the app:

Received SIGINT
Received SIGINT
Received SIGTERM

On the first signal, NestJS removes all listeners, and the next signal kills the app

A simple way to fix this issue is to add the following code to your main.ts file before NestJS bootstrap.

import { ShutdownSignal } from '@nestjs/common';

Object.values(ShutdownSignal).forEach((signal) => {
  process.on(signal, () => {});
});

It will add additional listeners to signals, and the app will not be killed but gracefully stopped by NestJS.

@nartc
Copy link
Contributor

nartc commented May 14, 2022

@mark-b-ab the bug seems to happen if I have async operations inside of the hooks. Just want to give you an update. I'm still looking into the process handling
#10292

@nartc nartc closed this as completed Jun 25, 2022
@jkossis
Copy link

jkossis commented Aug 4, 2022

@nartc why was this closed?

Using latest everything from nx, I am still experiencing the original behavior noted by @mark-b-ab.

@jkossis
Copy link

jkossis commented Aug 30, 2022

@nartc bumping this again.

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug
Projects
None yet
Development

No branches or pull requests

5 participants