Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(core): prevent concurrent downloads clobbering by using unique temp dirs (#37335)

Loud QA7765583dc806
diff --git a/packages/playwright-core/src/server/registry/browserFetcher.ts b/packages/playwright-core/src/server/registry/browserFetcher.ts
index acd3a52f14..64000580f7 100644
--- a/packages/playwright-core/src/server/registry/browserFetcher.ts
+++ b/packages/playwright-core/src/server/registry/browserFetcher.ts
@@ -24,7 +24,7 @@ import { debugLogger } from '../utils/debugLogger';
import { ManualPromise } from '../../utils/isomorphic/manualPromise';
import { getUserAgent } from '../utils/userAgent';
import { progress as ProgressBar, colors } from '../../utilsBundle';
-import { existsAsync } from '../utils/fileUtils';
+import { existsAsync, removeFolders } from '../utils/fileUtils';
import { browserDirectoryToMarkerFilePath } from '.';
@@ -37,7 +37,10 @@ export async function downloadBrowserWithProgressBar(title: string, browserDirec
return false;
}
- const zipPath = path.join(os.tmpdir(), downloadFileName);
+ // Create a unique temporary directory for this download to prevent concurrent downloads from clobbering each other
+ const uniqueTempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'playwright-download-'));
+ const zipPath = path.join(uniqueTempDir, downloadFileName);
+
try {
const retryCount = 5;
for (let attempt = 1; attempt <= retryCount; ++attempt) {
@@ -63,8 +66,8 @@ export async function downloadBrowserWithProgressBar(title: string, browserDirec
process.exitCode = 1;
throw e;
} finally {
- if (await existsAsync(zipPath))
- await fs.promises.unlink(zipPath);
+ // Clean up the temporary directory and its contents
+ await removeFolders([uniqueTempDir]);
}
logPolitely(`${title} downloaded to ${browserDirectory}`);
return true;
diff --git a/tests/installation/concurrent-download.spec.ts b/tests/installation/concurrent-download.spec.ts
new file mode 100644
index 0000000000..e448087be8
--- /dev/null
+++ b/tests/installation/concurrent-download.spec.ts
@@ -0,0 +1,40 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { test } from './npmTest';
+import fs from 'fs';
+test.use({ isolateBrowsers: true });
+
+test('concurrent browser downloads should not clobber each other', async ({ exec }, testInfo) => {
+ await exec('npm init -y');
+ await exec('npm install playwright');
+ const numProcesses = 3;
+ await Promise.all(Array.from({ length: numProcesses }, async (_, index) => {
+ const browserPath = testInfo.outputPath(`browsers-${index}`);
+ await exec('npx playwright install chromium', {
+ env: {
+ PLAYWRIGHT_BROWSERS_PATH: browserPath,
+ }
+ });
+
+ // Check that each installation has all required binaries
+ const entries = await fs.promises.readdir(browserPath).catch(() => []);
+ const installed = new Set(entries.map(entry => entry.split('-')[0].replace(/_/g, '-')).filter(name => !name.startsWith('.')));
+ test.expect(installed.has('chromium')).toBeTruthy();
+ test.expect(installed.has('chromium-headless-shell')).toBeTruthy();
+ test.expect(installed.has('ffmpeg')).toBeTruthy();
+ }));
+});