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

shutdown hooks stuck waiting for dispose call #1487

Closed
ssilve1989 opened this issue Sep 28, 2021 · 8 comments
Closed

shutdown hooks stuck waiting for dispose call #1487

ssilve1989 opened this issue Sep 28, 2021 · 8 comments

Comments

@ssilve1989
Copy link

Bug Report

When the process receives a SIGINT, the application gets stuck waiting for the httpAdapter.close() in the dispose() call in the shutdown lifecycles. It never reaches the shutdown hooks.

Current behavior

When enabling shutdown hooks and having an http server receiving requests from a polling source, like Prometheus, this behavior can be seen when sending a SIGINT into the process. The terminal prompt may be returned, but the process is still alive and providing output to the current terminal context.

Input Code

https://github.com/ssilve1989/nest-sigint-no-exit

Expected behavior

The http server should exit properly. I am not sure if this is a bug in Nest or Express, but it only seems to happen when the process is in the midst of receiving requests.

Possible Solution

Environment


Nest version: 8.0.8 and 7.16.18

 
For Tooling issues:
- Node version: 14.17.3 
- Platform:  Mac and Linux

Others:

@ssilve1989 ssilve1989 changed the title Shutdown hooks stuck waiting for httpAdapter.close shutdown hooks stuck waiting for dispose call Sep 28, 2021
@ssilve1989
Copy link
Author

After some further investigation, I was able to reproduce this with a barebones express app instantiated in a similar way to how Nest is doing it. I had to add a call to removeAllListeners for it to properly exit the application when binding a SIGINT handler. Adding this

this.httpServer.removeAllListeners();

in the close() function of the express adapter helps. This eventually ends up resolving the dispose call and running the shutdown hooks, but still it returns the prompt immediately on SIGINT, and then sometime later runs the shutdown hooks.

There is still something funny happening as the next execution cycle after the httpAdapter.close() call seems to take too long after the close() function has ended.

@micalevisk
Copy link
Member

looks like this only happens if we use nest start (and express):

a.mp4

With node dist/main:

b.mp4

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue @micalevisk?

@micalevisk
Copy link
Member

I didn't investigate it enough :/ I would start by looking at https://github.com/nestjs/nest-cli/blob/master/actions/start.action.ts but I don't have time to tackle this soon.

@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/nest Jan 11, 2022
@ssilve1989
Copy link
Author

@micalevisk I don't use nest start in my current project, we are using the Bazel build system. So I don't think its specifically related to the Nest CLI tool.

@micalevisk
Copy link
Member

@ssilve1989 so, to run the project, you're using node directly? I didn't understand why it worked with node dist/main.js but didn't with nest start tbh

@ssilve1989
Copy link
Author

@micalevisk

so, to run the project, you're using node directly?

that's right

I didn't understand why it worked with node dist/main.js but didn't with nest start tbh

i'll try to find some time to try it that way as well with the reproduction repo I provided

@tolgap
Copy link

tolgap commented Mar 17, 2023

FYI, this should've been fixed with the introduction of the forceCloseConnections option: nestjs/nest#10345

If you create your app using

const app = NestFactory.create({ forceCloseConnections: true });
app.enableShutdownHooks();

Nest will keep track of Connection: Keep-Alive HTTP requests and dispose them correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants