From 84849303eea806722f29f033af7a92731d270811 Mon Sep 17 00:00:00 2001 From: tom-shan Date: Mon, 29 Jul 2019 23:36:14 +0800 Subject: [PATCH] fix leaking java debug process When start debugging using vscode java-debug extension, the debug process will remain in OS process list and never be killed by theia process if the page is reloaded. When terminate plugin server, kill all child processes of plugin-host process. Signed-off-by: tom-shan --- packages/plugin-ext/package.json | 1 + .../src/hosted/node/hosted-plugin-process.ts | 13 ++++++++++++- yarn.lock | 9 ++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/plugin-ext/package.json b/packages/plugin-ext/package.json index 1717979ad32aa..fcccda67d9c0d 100644 --- a/packages/plugin-ext/package.json +++ b/packages/plugin-ext/package.json @@ -29,6 +29,7 @@ "getmac": "^1.4.6", "jsonc-parser": "^2.0.2", "lodash.clonedeep": "^4.5.0", + "ps-tree": "^1.2.0", "request": "^2.82.0", "uuid": "^3.2.1", "vscode-debugprotocol": "^1.32.0", diff --git a/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts b/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts index 09cce85b15f1b..4228e70798f36 100644 --- a/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts +++ b/packages/plugin-ext/src/hosted/node/hosted-plugin-process.ts @@ -25,6 +25,7 @@ import { RPCProtocolImpl } from '../../api/rpc-protocol'; import { MAIN_RPC_CONTEXT } from '../../api/plugin-api'; import { HostedPluginCliContribution } from './hosted-plugin-cli-contribution'; import { HostedPluginProcessesCache } from './hosted-plugin-processes-cache'; +const psTree = require('ps-tree'); export interface IPCConnectionOptions { readonly serverName: string; @@ -129,9 +130,19 @@ export class HostedPluginProcess implements ServerPluginRunner { const hostedPluginManager = rpc.getProxy(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT); hostedPluginManager.$stopPlugin('').then(() => { emitter.dispose(); - cp.kill(); + this.killProcessTree(cp.pid); }); + } + private killProcessTree(parentPid: number): void { + // tslint:disable-next-line:no-any + psTree(parentPid, (err: Error, childProcesses: Array) => { + // tslint:disable-next-line:no-any + childProcesses.forEach((p: any) => { + process.kill(p.PID); + }); + process.kill(parentPid); + }); } public runPluginServer(): void { diff --git a/yarn.lock b/yarn.lock index 22fc627496b65..f32312dbd02d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3685,7 +3685,7 @@ etag@~1.8.1: version "1.8.1" resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" -event-stream@~3.3.0: +event-stream@=3.3.4, event-stream@~3.3.0: version "3.3.4" resolved "http://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz#4ab4c9a0f5a54db9338b4c34d86bfce8f4b35571" dependencies: @@ -7833,6 +7833,13 @@ ps-tree@1.1.0: dependencies: event-stream "~3.3.0" +ps-tree@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/ps-tree/-/ps-tree-1.2.0.tgz#5e7425b89508736cdd4f2224d028f7bb3f722ebd" + integrity sha512-0VnamPPYHl4uaU/nSFeZZpR21QAWRz+sRv4iW9+v/GS/J5U5iZB5BNN6J0RMoOvdx2gWM2+ZFMIm58q24e4UYA== + dependencies: + event-stream "=3.3.4" + pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"