From 0b2d988cc134b4bcd49c2c17771b0dc629362366 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Wed, 5 Apr 2023 15:09:30 +0200 Subject: [PATCH] chore: improve debugging for EBUSY errors on Windows (#9976) Co-authored-by: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> --- packages/browsers/src/launcher.ts | 21 ++++++------ .../browsers/test/src/chrome/launcher.spec.ts | 29 ++++++++++++++-- .../test/src/firefox/launcher.spec.ts | 33 +++++++++++++++++-- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/packages/browsers/src/launcher.ts b/packages/browsers/src/launcher.ts index 69722ff25d8..7d9a495a0f7 100644 --- a/packages/browsers/src/launcher.ts +++ b/packages/browsers/src/launcher.ts @@ -291,17 +291,16 @@ class Process { ) { try { if (process.platform === 'win32') { - childProcess.exec( - `taskkill /pid ${this.#browserProcess.pid} /T /F`, - error => { - if (error) { - // taskkill can fail to kill the process e.g. due to missing permissions. - // Let's kill the process via Node API. This delays killing of all child - // processes of `this.proc` until the main Node.js process dies. - this.#browserProcess.kill(); - } - } - ); + try { + childProcess.execSync( + `taskkill /pid ${this.#browserProcess.pid} /T /F` + ); + } catch (error) { + // taskkill can fail to kill the process e.g. due to missing permissions. + // Let's kill the process via Node API. This delays killing of all child + // processes of `this.proc` until the main Node.js process dies. + this.#browserProcess.kill(); + } } else { // on linux the process group can be killed with the group id prefixed with // a minus sign. The process group id is the group leader's pid. diff --git a/packages/browsers/test/src/chrome/launcher.spec.ts b/packages/browsers/test/src/chrome/launcher.spec.ts index 8c9fc3ee30b..a112afc1186 100644 --- a/packages/browsers/test/src/chrome/launcher.spec.ts +++ b/packages/browsers/test/src/chrome/launcher.spec.ts @@ -90,11 +90,36 @@ describe('Chrome', () => { const process = launch({ executablePath, args: [ + '--allow-pre-commit-input', + '--disable-background-networking', + '--disable-background-timer-throttling', + '--disable-backgrounding-occluded-windows', + '--disable-breakpad', + '--disable-client-side-phishing-detection', + '--disable-component-extensions-with-background-pages', + '--disable-component-update', + '--disable-default-apps', + '--disable-dev-shm-usage', + '--disable-extensions', + '--disable-features=Translate,BackForwardCache,AcceptCHFrame,MediaRouter,OptimizationHints,DialMediaRouteProvider', + '--disable-hang-monitor', + '--disable-ipc-flooding-protection', + '--disable-popup-blocking', + '--disable-prompt-on-repost', + '--disable-renderer-backgrounding', + '--disable-sync', + '--enable-automation', + '--enable-features=NetworkServiceInProcess2', + '--export-tagged-pdf', + '--force-color-profile=srgb', '--headless=new', - '--use-mock-keychain', - '--disable-features=DialMediaRouteProvider', + '--metrics-recording-only', + '--no-first-run', + '--password-store=basic', '--remote-debugging-port=9222', + '--use-mock-keychain', `--user-data-dir=${path.join(tmpDir, 'profile')}`, + 'about:blank', ], }); const url = await process.waitForLineOutput(CDP_WEBSOCKET_ENDPOINT_REGEX); diff --git a/packages/browsers/test/src/firefox/launcher.spec.ts b/packages/browsers/test/src/firefox/launcher.spec.ts index 34aabec1ff8..7561b5b1a52 100644 --- a/packages/browsers/test/src/firefox/launcher.spec.ts +++ b/packages/browsers/test/src/firefox/launcher.spec.ts @@ -15,6 +15,7 @@ */ import assert from 'assert'; +import {execSync} from 'child_process'; import fs from 'fs'; import os from 'os'; import path from 'path'; @@ -26,6 +27,7 @@ import { Browser, BrowserPlatform, Cache, + createProfile, } from '../../../lib/cjs/main.js'; import {testFirefoxBuildId} from '../versions.js'; @@ -59,10 +61,37 @@ describe('Firefox', () => { }); afterEach(() => { - new Cache(tmpDir).clear(); + try { + new Cache(tmpDir).clear(); + } catch (err) { + if (os.platform() === 'win32') { + console.log(execSync('tasklist').toString('utf-8')); + } + throw err; + } }); it('should launch a Firefox browser', async () => { + const userDataDir = path.join(tmpDir, 'profile'); + function getArgs(): string[] { + const firefoxArguments = ['--no-remote']; + switch (os.platform()) { + case 'darwin': + firefoxArguments.push('--foreground'); + break; + case 'win32': + firefoxArguments.push('--wait-for-browser'); + break; + } + firefoxArguments.push('--profile', userDataDir); + firefoxArguments.push('--headless'); + firefoxArguments.push('about:blank'); + return firefoxArguments; + } + await createProfile(Browser.FIREFOX, { + path: userDataDir, + preferences: {}, + }); const executablePath = computeExecutablePath({ cacheDir: tmpDir, browser: Browser.FIREFOX, @@ -70,7 +99,7 @@ describe('Firefox', () => { }); const process = launch({ executablePath, - args: [`--user-data-dir=${path.join(tmpDir, 'profile')}`], + args: getArgs(), }); await process.close(); });