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

15.4.x - "serve" not terminating application before restart #14024

Closed
seyfert opened this issue Dec 26, 2022 · 3 comments
Closed

15.4.x - "serve" not terminating application before restart #14024

seyfert opened this issue Dec 26, 2022 · 3 comments

Comments

@seyfert
Copy link

seyfert commented Dec 26, 2022

Current Behavior

Starting in v15.4.x, running a Nest application using "serve" will not terminate the previous process before starting resulting in Error: listen EADDRINUSE: address already in use error.

Expected Behavior

When "serve" restarts a Nest application it should stop the old process to free the port. This works correctly in v15.3.x.

Here are the logs using v15.3.3:

> nx run test-app:serve

chunk (runtime: main) main.js (main) 2.91 KiB [entry] [rendered]
webpack compiled successfully (de5400e62644fad2)
Type-checking in progress...
Debugger listening on ws://localhost:9229/9b972a6b-c932-4c34-b7d6-5dd6c7d195af
Debugger listening on ws://localhost:9229/9b972a6b-c932-4c34-b7d6-5dd6c7d195af
For help, see: https://nodejs.org/en/docs/inspector
[Nest] 74751  - 12/26/2022, 1:21:55 PM     LOG [NestFactory] Starting Nest application...
[Nest] 74751  - 12/26/2022, 1:21:55 PM     LOG [InstanceLoader] AppModule dependencies initialized +34ms
[Nest] 74751  - 12/26/2022, 1:21:55 PM     LOG [RoutesResolver] AppController {/api}: +8ms
[Nest] 74751  - 12/26/2022, 1:21:55 PM     LOG [RouterExplorer] Mapped {/api, GET} route +4ms
[Nest] 74751  - 12/26/2022, 1:21:55 PM     LOG [NestApplication] Nest application successfully started +3ms
[Nest] 74751  - 12/26/2022, 1:21:55 PM     LOG 🚀 Application is running on: http://localhost:3333/api
No errors found.
chunk (runtime: main) main.js (main) 2.91 KiB [entry]
webpack compiled successfully (de5400e62644fad2)
No errors found.
[Nest] 74751  - 12/26/2022, 1:22:00 PM     LOG SIGTERM
Debugger listening on ws://localhost:9229/28a87752-5a14-4ba6-af51-3c0368bad778
Debugger listening on ws://localhost:9229/28a87752-5a14-4ba6-af51-3c0368bad778
For help, see: https://nodejs.org/en/docs/inspector
[Nest] 74761  - 12/26/2022, 1:22:00 PM     LOG [NestFactory] Starting Nest application...
[Nest] 74761  - 12/26/2022, 1:22:00 PM     LOG [InstanceLoader] AppModule dependencies initialized +45ms
[Nest] 74761  - 12/26/2022, 1:22:00 PM     LOG [RoutesResolver] AppController {/api}: +7ms
[Nest] 74761  - 12/26/2022, 1:22:00 PM     LOG [RouterExplorer] Mapped {/api, GET} route +3ms
[Nest] 74761  - 12/26/2022, 1:22:00 PM     LOG [NestApplication] Nest application successfully started +2ms
[Nest] 74761  - 12/26/2022, 1:22:00 PM     LOG 🚀 Application is running on: http://localhost:3333/api

Github Repo

No response

Steps to Reproduce

  1. Create workspace using Nx 15.4.1 with Nest app: pnpx create-nx-workspace test-app --preset=nest --appName=test-app
  2. In apps/test-app/src/main.ts, add call to app.enableShutdownHooks() and log a message when process terminates:
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableShutdownHooks();
  process.on('SIGTERM', () => {
    Logger.log('SIGTERM');
  });
  const globalPrefix = 'api';
  ...
}
  1. Start application pnpm nx run test-app:serve
  2. Make change in test-app code to cause restart.

Nx Report

Node : 16.18.0
   OS   : darwin x64
   pnpm : 7.13.6
   
   nx : 15.4.1
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.4.1
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.4.1
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.4.1
   @nrwl/js : 15.4.1
   @nrwl/linter : 15.4.1
   @nrwl/nest : 15.4.1
   @nrwl/next : Not Found
   @nrwl/node : 15.4.1
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/rollup : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/webpack : 15.4.1
   @nrwl/workspace : 15.4.1
   @nrwl/vite : Not Found
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:

Failure Logs

$ pnpm nx run test-app:serve                                                 

> nx run test-app:serve

chunk (runtime: main) main.js (main) 2.91 KiB [entry] [rendered]
webpack compiled successfully (de5400e62644fad2)
Debugger listening on ws://localhost:9229/78362889-df62-4997-9b25-e787dd5b6ebb
Debugger listening on ws://localhost:9229/78362889-df62-4997-9b25-e787dd5b6ebb
For help, see: https://nodejs.org/en/docs/inspector
Type-checking in progress...
[Nest] 74415  - 12/26/2022, 1:16:54 PM     LOG [NestFactory] Starting Nest application...
[Nest] 74415  - 12/26/2022, 1:16:54 PM     LOG [InstanceLoader] AppModule dependencies initialized +59ms
[Nest] 74415  - 12/26/2022, 1:16:54 PM     LOG [RoutesResolver] AppController {/api}: +16ms
[Nest] 74415  - 12/26/2022, 1:16:54 PM     LOG [RouterExplorer] Mapped {/api, GET} route +3ms
[Nest] 74415  - 12/26/2022, 1:16:54 PM     LOG [NestApplication] Nest application successfully started +2ms
[Nest] 74415  - 12/26/2022, 1:16:54 PM     LOG 🚀 Application is running on: http://localhost:3333/api
No errors found.
chunk (runtime: main) main.js (main) 2.91 KiB [entry]
webpack compiled successfully (de5400e62644fad2)
No errors found.
Starting inspector on localhost:9229 failed: address already in use
[Nest] 74427  - 12/26/2022, 1:16:59 PM     LOG [NestFactory] Starting Nest application...
[Nest] 74427  - 12/26/2022, 1:16:59 PM     LOG [InstanceLoader] AppModule dependencies initialized +33ms
[Nest] 74427  - 12/26/2022, 1:16:59 PM     LOG [RoutesResolver] AppController {/api}: +6ms
[Nest] 74427  - 12/26/2022, 1:16:59 PM     LOG [RouterExplorer] Mapped {/api, GET} route +3ms
[Nest] 74427  - 12/26/2022, 1:16:59 PM     LOG [NestApplication] Nest application successfully started +2ms
[Nest] 74427  - 12/26/2022, 1:16:59 PM   ERROR [NestApplication] Error: listen EADDRINUSE: address already in use :::3333 +2ms
Error: listen EADDRINUSE: address already in use :::3333
    at Server.setupListenHandle [as _listen2] (node:net:1463:16)
    at listenInCluster (node:net:1511:12)
    at Server.listen (node:net:1599:7)
    at ExpressAdapter.listen (/Users/test/test-app/node_modules/.pnpm/@nestjs+platform-express@9.2.1_hjcqpoaebdr7gdo5hgc22hthbe/node_modules/@nestjs/platform-express/adapters/express-adapter.js:78:32)
    at /Users/test/test-app/node_modules/.pnpm/@nestjs+core@9.2.1_e6la6qvsclaae2becwjnmfvsuq/node_modules/@nestjs/core/nest-application.js:167:30
    at new Promise (<anonymous>)
    at NestApplication.listen (/Users/test/test-app/node_modules/.pnpm/@nestjs+core@9.2.1_e6la6qvsclaae2becwjnmfvsuq/node_modules/@nestjs/core/nest-application.js:156:16)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Additional Information

Original process is not terminated (SIGTERM is not called). This is a regression. If you switch to v15.3.3 the same code works correctly. Node inspector also appears to be impacted by this bug.

@seyfert
Copy link
Author

seyfert commented Dec 26, 2022

Just noticed this same issue was already reported. Closing this.

@sahilpurav
Copy link
Contributor

Just noticed this same issue was already reported. Closing this.

Yes I raised a PR to fix it - #14023 Waiting for review 👍

@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 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants