From 51e61696c1c20cc09bd4fc068ae1dfa259c41745 Mon Sep 17 00:00:00 2001 From: Henrik Skupin Date: Thu, 11 Nov 2021 09:19:23 +0100 Subject: [PATCH] fix: only kill the process when there is no browser instance available (#7762) When the browser has been started and we have a valid reference lets make use of it instead of force-killing the process. A force kill should probably be the last resort in cleaning up the process. This will help with Firefox as described on #7668 (comment). --- src/node/Launcher.ts | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/node/Launcher.ts b/src/node/Launcher.ts index 9a79350d6a9cd..2bb83394fe115 100644 --- a/src/node/Launcher.ts +++ b/src/node/Launcher.ts @@ -174,6 +174,7 @@ class ChromeLauncher implements ProductLauncher { pipe: usePipe, }); + let browser; try { const connection = await runner.setupConnection({ usePipe, @@ -181,7 +182,7 @@ class ChromeLauncher implements ProductLauncher { slowMo, preferredRevision: this._preferredRevision, }); - const browser = await Browser.create( + browser = await Browser.create( connection, [], ignoreHTTPSErrors, @@ -189,13 +190,21 @@ class ChromeLauncher implements ProductLauncher { runner.proc, runner.close.bind(runner) ); - if (waitForInitialPage) - await browser.waitForTarget((t) => t.type() === 'page', { timeout }); - return browser; } catch (error) { runner.kill(); throw error; } + + if (waitForInitialPage) { + try { + await browser.waitForTarget((t) => t.type() === 'page', { timeout }); + } catch (error) { + await browser.close(); + throw error; + } + } + + return browser; } defaultArgs(options: BrowserLaunchArgumentOptions = {}): string[] { @@ -371,6 +380,7 @@ class FirefoxLauncher implements ProductLauncher { pipe, }); + let browser; try { const connection = await runner.setupConnection({ usePipe: pipe, @@ -378,7 +388,7 @@ class FirefoxLauncher implements ProductLauncher { slowMo, preferredRevision: this._preferredRevision, }); - const browser = await Browser.create( + browser = await Browser.create( connection, [], ignoreHTTPSErrors, @@ -386,13 +396,21 @@ class FirefoxLauncher implements ProductLauncher { runner.proc, runner.close.bind(runner) ); - if (waitForInitialPage) - await browser.waitForTarget((t) => t.type() === 'page', { timeout }); - return browser; } catch (error) { runner.kill(); throw error; } + + if (waitForInitialPage) { + try { + await browser.waitForTarget((t) => t.type() === 'page', { timeout }); + } catch (error) { + await browser.close(); + throw error; + } + } + + return browser; } executablePath(): string {