From e8c512dbeb3f69ba8dde7fc22d7ee27843f1629a Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 28 Oct 2021 19:28:16 -0700 Subject: [PATCH] fix(cleanup): use rimraf.sync on process exit (#9862) Currently we call async `removeFolders` from a synchronous `process.on('exit')` handler, which should not work. --- .../src/utils/processLauncher.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/src/utils/processLauncher.ts b/packages/playwright-core/src/utils/processLauncher.ts index 9586e92e2f..5459340b39 100644 --- a/packages/playwright-core/src/utils/processLauncher.ts +++ b/packages/playwright-core/src/utils/processLauncher.ts @@ -19,6 +19,7 @@ import * as childProcess from 'child_process'; import * as readline from 'readline'; import { eventsHelper } from './eventsHelper'; import { isUnderTest, removeFolders } from './utils'; +import rimraf from 'rimraf'; export type Env = {[key: string]: string | number | boolean | undefined}; @@ -124,7 +125,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise { gracefullyClose().then(() => { @@ -183,7 +184,19 @@ export async function launchProcess(options: LaunchProcessOptions): Promise`); } - cleanup(); + } + + function killProcessAndCleanup() { + killProcess(); + options.log(`[pid=${spawnedProcess.pid || 'N/A'}] starting temporary directories cleanup`); + for (const dir of options.tempDirectories) { + try { + rimraf.sync(dir, { maxBusyTries: 10 }); + } catch (e) { + options.log(`[pid=${spawnedProcess.pid || 'N/A'}] exception while removing ${dir}: ${e}`); + } + } + options.log(`[pid=${spawnedProcess.pid || 'N/A'}] finished temporary directories cleanup`); } function killAndWait() {