Skip to content

Commit 8171646

Browse files
committed
feat: Added kill listeners so process gets killed properly
1 parent 267413d commit 8171646

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/plugins/plugin-manager.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export class PluginManager {
3535
this.plugins.set(plugin.name, plugin)
3636
}
3737

38+
this.registerKillListeners(plugins)
39+
3840
const dependencyMap = await this.initializePlugins(plugins, secureMode);
3941
return dependencyMap;
4042
}
@@ -174,4 +176,19 @@ export class PluginManager {
174176

175177
return resourceMap;
176178
}
179+
180+
/** Clean up any stranglers and child processes if the CLI is killed */
181+
private registerKillListeners(plugins: Plugin[]) {
182+
const kill = (code) => {
183+
plugins.forEach((p) => {
184+
p.kill()
185+
})
186+
process.exit(code);
187+
}
188+
189+
process.on('exit', kill)
190+
process.on('SIGINT', kill)
191+
process.on('SIGTERM', kill)
192+
process.on('SIGHUP', kill)
193+
}
177194
}

src/plugins/plugin-process.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ export class PluginProcess {
115115

116116
private static isIpcMessage(message: unknown): message is IpcMessageV2 {
117117
return ipcMessageValidator(message);
118-
}
118+
}
119+
120+
kill() {
121+
this.process.kill(9)
122+
}
119123
}
120124

src/plugins/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ export class Plugin implements IPlugin {
131131
}
132132
}
133133

134+
kill() {
135+
this.process?.kill()
136+
}
137+
134138
private validateInitializeResponse(response: unknown): response is InitializeResponseData {
135139
return initializeResponseValidator(response)
136140
}

src/ui/reporters/default-reporter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class DefaultReporter implements Reporter {
4444
ctx.on(Event.PROCESS_START, (name) => this.onProcessStartEvent(name))
4545
ctx.on(Event.PROCESS_FINISH, (name) => this.onProcessFinishEvent(name))
4646
ctx.on(Event.SUB_PROCESS_START, (name, additionalName) => this.onSubprocessStartEvent(name, additionalName));
47-
ctx.on(Event.SUB_PROCESS_FINISH, (name, additionalName) => this.onSubprocessFinishEvent(name, additionalName))
47+
ctx.on(Event.SUB_PROCESS_FINISH, (name, additionalName) => this.onSubprocessFinishEvent(name, additionalName));
4848
}
4949

5050
async promptUserForValues(resources: Array<ResourceInfo>, promptType: PromptType): Promise<ResourceConfig[]> {

0 commit comments

Comments
 (0)