Merge pull request #137650 from microsoft/issue-133173

Avoid using `setImmediate`
This commit is contained in:
Alexandru Dima 2021-11-23 11:38:52 +01:00 committed by GitHub
commit 465b806ed2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 9 deletions

View file

@ -6,7 +6,6 @@
import { VSBuffer } from 'vs/base/common/buffer';
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, dispose, IDisposable } from 'vs/base/common/lifecycle';
import * as platform from 'vs/base/common/platform';
import { IIPCLogger, IMessagePassingProtocol, IPCClient } from 'vs/base/parts/ipc/common/ipc';
export const enum SocketCloseEventType {
@ -369,7 +368,7 @@ class ProtocolWriter {
private _writeSoon(header: VSBuffer, data: VSBuffer): void {
if (this._bufferAdd(header, data)) {
platform.setImmediate(() => {
setTimeout(() => {
this._writeNow();
});
}

View file

@ -263,7 +263,7 @@ export const originalGlobalValues = {
Date: globalThis.Date,
};
function setTimeout(scheduler: Scheduler, handler: TimerHandler, timeout: number): IDisposable {
function setTimeout(scheduler: Scheduler, handler: TimerHandler, timeout: number = 0): IDisposable {
if (typeof handler === 'string') {
throw new Error('String handler args should not be used and are not supported');
}
@ -324,7 +324,7 @@ function setInterval(scheduler: Scheduler, handler: TimerHandler, interval: numb
}
function overwriteGlobals(scheduler: Scheduler): IDisposable {
globalThis.setTimeout = ((handler: TimerHandler, timeout: number) => setTimeout(scheduler, handler, timeout)) as any;
globalThis.setTimeout = ((handler: TimerHandler, timeout?: number) => setTimeout(scheduler, handler, timeout)) as any;
globalThis.clearTimeout = (timeoutId: any) => {
if (typeof timeoutId === 'object' && timeoutId && 'dispose' in timeoutId) {
timeoutId.dispose();

View file

@ -69,7 +69,7 @@ async function connect(connectDriver: typeof connectElectronDriver, child: cp.Ch
while (true) {
try {
const { client, driver } = await connectDriver(outPath, handlePath);
return new Code(client, driver, logger);
return new Code(client, driver, logger, child?.pid);
} catch (err) {
if (++errCount > 50) {
if (child) {
@ -254,7 +254,8 @@ export class Code {
constructor(
private client: IDisposable,
driver: IDriver,
readonly logger: Logger
readonly logger: Logger,
private readonly pid: number | undefined
) {
this.driver = new Proxy(driver, {
get(target, prop, receiver) {
@ -295,9 +296,27 @@ export class Code {
}
async exit(): Promise<void> {
const veto = await this.driver.exitApplication();
if (veto === true) {
throw new Error('Code exit was blocked by a veto.');
const exitPromise = this.driver.exitApplication();
// If we know the `pid`, use that to await the
// process to terminate (desktop).
const pid = this.pid;
if (typeof pid === 'number') {
await (async () => {
while (true) {
try {
process.kill(pid, 0); // throws an exception if the main process doesn't exist anymore.
await new Promise(c => setTimeout(c, 100));
} catch (error) {
return;
}
}
})();
}
// Otherwise await the exit promise (web).
else {
await exitPromise;
}
}