From 94c829afd6ab90a71f1ab873370a2ec09235d4c2 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 8 Sep 2021 17:26:48 +0200 Subject: [PATCH] macOS: application hangs when --wait is used with --verbose (fix #132611) --- src/vs/code/node/cli.ts | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/vs/code/node/cli.ts b/src/vs/code/node/cli.ts index 6c5098c277e..de50b33b195 100644 --- a/src/vs/code/node/cli.ts +++ b/src/vs/code/node/cli.ts @@ -7,6 +7,7 @@ import { ChildProcess, spawn, SpawnOptions } from 'child_process'; import { chmodSync, existsSync, readFileSync, statSync, truncateSync, unlinkSync } from 'fs'; import { homedir } from 'os'; import type { ProfilingSession, Target } from 'v8-inspect-profiler'; +import { Event } from 'vs/base/common/event'; import { isAbsolute, join } from 'vs/base/common/path'; import { IProcessEnvironment, isWindows } from 'vs/base/common/platform'; import { randomPort } from 'vs/base/common/ports'; @@ -134,7 +135,7 @@ export async function main(argv: string[]): Promise { child.stdout!.on('data', (data: Buffer) => console.log(data.toString('utf8').trim())); child.stderr!.on('data', (data: Buffer) => console.log(data.toString('utf8').trim())); - await new Promise(resolve => child.once('exit', () => resolve())); + await Event.toPromise(Event.fromNodeEventEmitter(child, 'exit')); }); } @@ -198,6 +199,23 @@ export async function main(argv: string[]): Promise { if (waitMarkerFilePath) { addArg(argv, '--waitMarkerFilePath', waitMarkerFilePath); } + + // When running with --wait, we want to continue running CLI process + // until either: + // - the wait marker file has been deleted (e.g. when closing the editor) + // - the launched process terminates (e.g. due to a crash) + processCallbacks.push(async child => { + try { + await Promise.race([ + whenDeleted(waitMarkerFilePath!), + Event.toPromise(Event.fromNodeEventEmitter(child, 'exit')) + ]); + } finally { + if (stdinFilePath) { + unlinkSync(stdinFilePath); // Make sure to delete the tmp stdin file if we have any + } + } + }); } // If we have been started with `--prof-startup` we need to find free ports to profile @@ -321,23 +339,6 @@ export async function main(argv: string[]): Promise { const child = spawn(process.execPath, argv.slice(2), options); - if (args.wait && waitMarkerFilePath) { - return new Promise(resolve => { - - // Complete when process exits - child.once('exit', () => resolve(undefined)); - - // Complete when wait marker file is deleted - whenDeleted(waitMarkerFilePath!).then(resolve, resolve); - }).then(() => { - - // Make sure to delete the tmp stdin file if we have any - if (stdinFilePath) { - unlinkSync(stdinFilePath); - } - }); - } - return Promise.all(processCallbacks.map(callback => callback(child))); } }