From 87485948ad6598d8a1f480b1f5dba2fe2e12c121 Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 29 Oct 2019 14:41:18 -0500 Subject: [PATCH] Kill inner process if parent process dies Fixes #1076. --- src/node/cli.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/node/cli.ts b/src/node/cli.ts index 175708e1..ca96a9b9 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -237,6 +237,7 @@ export class WrapperProcess { env: { ...process.env, LAUNCH_VSCODE: "true", + VSCODE_PARENT_PID: process.pid.toString(), }, stdio: ["inherit", "inherit", "inherit", "ipc"], }); @@ -257,6 +258,20 @@ process.exit = function (code?: number) { console.warn(err.stack); } as (code?: number) => never; +// Copy the extension host behavior of killing oneself if the parent dies. This +// also exists in bootstrap-fork.js but spawning with that won't work because we +// override process.exit. +if (typeof process.env.VSCODE_PARENT_PID !== "undefined") { + const parentPid = parseInt(process.env.VSCODE_PARENT_PID, 10); + setInterval(() => { + try { + process.kill(parentPid, 0); // Throws an exception if the process doesn't exist anymore. + } catch (e) { + exit(); + } + }, 5000); +} + // It's possible that the pipe has closed (for example if you run code-server // --version | head -1). Assume that means we're done. if (!process.stdout.isTTY) {