From 58567842887b28bda307c337e98c21b41c72da62 Mon Sep 17 00:00:00 2001 From: syuilo <4439005+syuilo@users.noreply.github.com> Date: Sun, 28 Jun 2026 10:16:57 +0900 Subject: [PATCH] fix --- .github/scripts/chrome.mts | 45 +++++++++++-------- .../measure-frontend-browser-comparison.mts | 9 ++-- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/.github/scripts/chrome.mts b/.github/scripts/chrome.mts index 75b13cd15e..9bc43de030 100644 --- a/.github/scripts/chrome.mts +++ b/.github/scripts/chrome.mts @@ -189,6 +189,19 @@ async function launchChrome(label: string): Promise { throw new Error('Timed out waiting for Chrome DevTools Protocol'); } +async function closeChrome(handle: ChromeHandle) { + if (handle.process.exitCode == null) { + handle.process.kill(); + } + await waitForProcessExit(handle.process); + await rm(handle.userDataDir, { + recursive: true, + force: true, + maxRetries: 10, + retryDelay: 200, + }); +} + type CdpResponse = { id?: number; method?: string; @@ -302,14 +315,19 @@ export class Chrome { static async create(label: string, options: ChromeOptions): Promise { const chromeHandle = await launchChrome(label); - const url = await fetchJson<{ webSocketDebuggerUrl: string }>( - `http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`, - { method: 'PUT' }, - ).catch(async () => await fetchJson<{ webSocketDebuggerUrl: string }>( - `http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`, - )); - const cdpClient = await CdpClient.connect(url.webSocketDebuggerUrl); - return new Chrome(chromeHandle, cdpClient, options); + try { + const url = await fetchJson<{ webSocketDebuggerUrl: string }>( + `http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`, + { method: 'PUT' }, + ).catch(async () => await fetchJson<{ webSocketDebuggerUrl: string }>( + `http://127.0.0.1:${chromeHandle.port}/json/new?${encodeURIComponent('about:blank')}`, + )); + const cdpClient = await CdpClient.connect(url.webSocketDebuggerUrl); + return new Chrome(chromeHandle, cdpClient, options); + } catch (err) { + await closeChrome(chromeHandle); + throw err; + } } public async enableNetworkTracking() { @@ -513,16 +531,7 @@ export class Chrome { public async close() { this.cdp.close(); - if (this.handle.process.exitCode == null) { - this.handle.process.kill(); - } - await waitForProcessExit(this.handle.process); - await rm(this.handle.userDataDir, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 200, - }); + await closeChrome(this.handle); } } diff --git a/.github/scripts/measure-frontend-browser-comparison.mts b/.github/scripts/measure-frontend-browser-comparison.mts index e03f123a87..d2361e145b 100644 --- a/.github/scripts/measure-frontend-browser-comparison.mts +++ b/.github/scripts/measure-frontend-browser-comparison.mts @@ -414,12 +414,11 @@ function summarizeSamples(label: 'base' | 'head', samples: BrowserMeasurementSam } async function measureSample(label: 'base' | 'head', round: number, heapSnapshotSavePath?: string) { - let chrome: Chrome | null = null; + await prepareInstance(); + + const chrome = await Chrome.create(label, { scenarioTimeoutMs }); try { - await prepareInstance(); - - chrome = await Chrome.create(label, { scenarioTimeoutMs }); await chrome.enableNetworkTracking(); const startedAt = Date.now(); @@ -442,7 +441,7 @@ async function measureSample(label: 'base' | 'head', round: number, heapSnapshot return measurement; } finally { - if (chrome != null) await chrome.close(); + await chrome.close(); } }