Skip to content

Commit

Permalink
fix(express): shutdown hooks not firing cased by open http connections
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgap committed Oct 3, 2022
1 parent 7465332 commit ef80e62
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/platform-express/adapters/express-adapter.ts
Expand Up @@ -48,6 +48,7 @@ type VersionedRoute = <
export class ExpressAdapter extends AbstractHttpAdapter {
private readonly routerMethodFactory = new RouterMethodFactory();
private readonly logger = new Logger(ExpressAdapter.name);
private readonly openConnections = new Set<any>();

constructor(instance?: any) {
super(instance || express());
Expand Down Expand Up @@ -134,6 +135,9 @@ export class ExpressAdapter extends AbstractHttpAdapter {
}

public close() {
this.openConnections.forEach(socket => {
socket.destroy();
});
if (!this.httpServer) {
return undefined;
}
Expand Down Expand Up @@ -202,9 +206,11 @@ export class ExpressAdapter extends AbstractHttpAdapter {
options.httpsOptions,
this.getInstance(),
);
this.trackOpenConnections();
return;
}
this.httpServer = http.createServer(this.getInstance());
this.trackOpenConnections();
}

public registerParserMiddleware(prefix?: string, rawBody?: boolean) {
Expand Down Expand Up @@ -377,6 +383,13 @@ export class ExpressAdapter extends AbstractHttpAdapter {
}
}

private trackOpenConnections() {
this.httpServer.on('connection', socket => {
this.openConnections.add(socket);
socket.on('close', () => this.openConnections.delete(socket));
});
}

private isMiddlewareApplied(name: string): boolean {
const app = this.getInstance();
return (
Expand Down

0 comments on commit ef80e62

Please sign in to comment.