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

fix(ws): close existing connections #13531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bettercalljason
Copy link

@bettercalljason bettercalljason commented May 3, 2024

Close all existing websocket connections to ensure a graceful shutdown.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: closes #13534

Existing connections are not closed automatically, according to https://github.com/websockets/ws/blob/master/doc/ws.md#serverclosecallback.

Application shutdown hangs when shutdown hooks are enabled and there are existing websocket connections. This is because the server.close callback that is awaited only resolves after all connections are closed. This is the call chain:

  1. process.cleanup
  2. NestApplication.dispose
  3. SocketModule.close
  4. WsAdapter.close
  5. server.close

What is the new behavior?

All existing websocket connections are closed to ensure a graceful shutdown.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Existing connections must be closed manually. Otherwise, application shutdown hangs when shutdown hooks are enabled and there are existing connections.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 7a20893c-f508-44d3-8a50-e1b7ed027b27

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 92.124%

Totals Coverage Status
Change from base Build e56f6d99-d9bc-426a-b9ef-ed11406d7e1c: 0.0%
Covered Lines: 6737
Relevant Lines: 7313

💛 - Coveralls

Copy link

@benjGam benjGam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, where will this piece of code will be invoked buddy ?

@bettercalljason
Copy link
Author

Hi @benjGam

This will be invoked in here: https://github.com/nestjs/nest/blob/master/packages/websockets/socket-module.ts#L108.

Since WsAdapter extends AbstractWsAdapter the AbstractWsAdapter.close method was called before this proposed change.

@benjGam
Copy link

benjGam commented May 16, 2024

Okay, didn't see that, thanks for your reply

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

Successfully merging this pull request may close these issues.

@nestjs/platform-ws: Application shutdown hangs
3 participants