Skip to content

Commit 50e643f

Browse files
fix(core): unsubscribe from shutdown signals on close
1 parent f980abd commit 50e643f

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

packages/core/nest-application-context.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { Module } from './injector/module';
3131
export class NestApplicationContext implements INestApplicationContext {
3232
protected isInitialized = false;
3333
protected readonly injector = new Injector();
34+
private shutdownCleanupRef?: (...args: unknown[]) => unknown;
3435
private readonly activeShutdownSignals = new Array<string>();
3536
private readonly containerScanner: ContainerScanner;
3637

@@ -109,6 +110,7 @@ export class NestApplicationContext implements INestApplicationContext {
109110
await this.callBeforeShutdownHook();
110111
await this.dispose();
111112
await this.callShutdownHook();
113+
await this.unsubscribeFromProcessSignals();
112114
}
113115

114116
public useLogger(logger: LoggerService) {
@@ -179,13 +181,26 @@ export class NestApplicationContext implements INestApplicationContext {
179181
process.exit(1);
180182
}
181183
};
184+
this.shutdownCleanupRef = cleanup as (...args: unknown[]) => unknown;
182185

183186
signals.forEach((signal: string) => {
184187
this.activeShutdownSignals.push(signal);
185188
process.on(signal as any, cleanup);
186189
});
187190
}
188191

192+
/**
193+
* Unsubscribes from shutdown signals (process events)
194+
*/
195+
protected unsubscribeFromProcessSignals() {
196+
if (!this.shutdownCleanupRef) {
197+
return;
198+
}
199+
this.activeShutdownSignals.forEach(signal => {
200+
process.removeListener(signal, this.shutdownCleanupRef);
201+
});
202+
}
203+
189204
/**
190205
* Calls the `onModuleInit` function on the registered
191206
* modules and its children.

0 commit comments

Comments
 (0)