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

Terminate not working with shutdown hooks enabled after API request #10131

Closed
5 of 15 tasks
robbielitebit opened this issue Aug 18, 2022 · 1 comment
Closed
5 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@robbielitebit
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Nestjs won't terminate the program after an API call is done. A cronjob keeps running and onApplicationShutdown is not called.

Minimum reproduction code

https://github.com/robbielitebit/NestjsShutdownIssue

Steps to reproduce

  1. yarn install
  2. yarn start
  3. Go to localhost:3000 (app will terminate correctly if you don't call the api)
  4. Terminate program with Ctrl + C
  5. See cronjob keeps running and onApplicationShutdown is not called
  6. Go to localhost:3000 again
  7. See now the program will terminate in a few seconds

Expected behavior

Expected behavior is that the nestjs application is terminated correctly after terminate signal:

  • Cronjob is cancelled
  • onApplicationShutdown is called

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

9.0.0

Packages versions

{
  "name": "foo",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/schedule": "^2.1.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.4",
    "@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.1.2",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.5",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.0.0",
    "typescript": "^4.3.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

v16.13.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@robbielitebit robbielitebit added the needs triage This issue has not been looked into label Aug 18, 2022
@quangtran88
Copy link
Contributor

quangtran88 commented Aug 26, 2022

Basically this problem is caused by the behavior when we try to close our HTTP server while clients are opening some keep-alive connections, the server then hangout until all connections are closed, as mentioned on issue nodejs/node-v0.x-archive#9066 and nodejs/node#2642

So before Nest perform callShutdownHook (which will terminate the cron job)

await this.callShutdownHook(signal);

it will wait until the HTTP server completely closed.

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

3 participants