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(websockets): Prevent HTTP server early close in Socket.IO shutdown #14185

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/websockets/adapters/ws-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export abstract class AbstractWsAdapter<
> implements WebSocketAdapter<TServer, TClient, TOptions>
{
protected readonly httpServer: any;
private _forceCloseConnections: boolean;

constructor(appOrHttpServer?: INestApplicationContext | any) {
if (appOrHttpServer && appOrHttpServer instanceof NestApplication) {
Expand All @@ -26,6 +27,10 @@ export abstract class AbstractWsAdapter<
}
}

public set forceCloseConnections(value: boolean) {
this._forceCloseConnections = value;
}

public bindClientConnect(server: TServer, callback: Function) {
server.on(CONNECTION_EVENT, callback);
}
Expand All @@ -35,6 +40,9 @@ export abstract class AbstractWsAdapter<
}

public async close(server: TServer) {
if (this._forceCloseConnections) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

@sapenlei I just realized that this method should be called no matter the _forceCloseConnections for non-shared WS servers. This doesn't seem to be the case atm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even for non-shared ws servers, their connections are destroyed and the final shutdown is closed by express

Copy link
Member

Choose a reason for hiding this comment

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

How come? What if you use a port different from the HTTP application?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, there's a problem. I'll fix it tomorrow

Copy link
Contributor Author

@sapenlei sapenlei Nov 26, 2024

Choose a reason for hiding this comment

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

@kamilmysliwiec I fixed it #14204

const isCallable = server && isFunction(server.close);
isCallable && (await new Promise(resolve => server.close(resolve)));
}
Expand Down
9 changes: 6 additions & 3 deletions packages/websockets/socket-module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NestApplicationOptions } from '@nestjs/common';
import { InjectionToken } from '@nestjs/common/interfaces';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { GuardsConsumer } from '@nestjs/core/guards/guards-consumer';
import { GuardsContextCreator } from '@nestjs/core/guards/guards-context-creator';
Expand All @@ -25,8 +25,7 @@ import { WebSocketsController } from './web-sockets-controller';

export class SocketModule<
THttpServer = any,
TAppOptions extends
NestApplicationContextOptions = NestApplicationContextOptions,
TAppOptions extends NestApplicationOptions = NestApplicationOptions,
sapenlei marked this conversation as resolved.
Show resolved Hide resolved
> {
private readonly socketsContainer = new SocketsContainer();
private applicationConfig: ApplicationConfig;
Expand Down Expand Up @@ -113,8 +112,11 @@ export class SocketModule<
}

private initializeAdapter() {
const forceCloseConnections = this.appOptions.forceCloseConnections;
const adapter = this.applicationConfig.getIoAdapter();
if (adapter) {
(adapter as AbstractWsAdapter).forceCloseConnections =
forceCloseConnections;
this.isAdapterInitialized = true;
return;
}
Expand All @@ -124,6 +126,7 @@ export class SocketModule<
() => require('@nestjs/platform-socket.io'),
);
const ioAdapter = new IoAdapter(this.httpServer);
ioAdapter.forceCloseConnections = forceCloseConnections;
this.applicationConfig.setIoAdapter(ioAdapter);

this.isAdapterInitialized = true;
Expand Down