diff --git a/src/chrome-launcher.ts b/src/chrome-launcher.ts index 813e8fa58..5457b6fb1 100644 --- a/src/chrome-launcher.ts +++ b/src/chrome-launcher.ts @@ -323,6 +323,12 @@ class Launcher { this.fs.writeFileSync(this.pidFile, this.chromeProcess.pid.toString()); } + // handle process closed right after it started + this.chromeProcess.on('close', () => { + delete this.chromeProcess; + this.destroyTmp(); + }); + log.verbose( 'ChromeLauncher', `Chrome running with pid ${this.chromeProcess.pid} on port ${this.port}.`); @@ -401,11 +407,6 @@ class Launcher { return; } - this.chromeProcess.on('close', () => { - delete this.chromeProcess; - this.destroyTmp(); - }); - log.log('ChromeLauncher', `Killing Chrome instance ${this.chromeProcess.pid}`); try { if (isWindows) { @@ -428,21 +429,22 @@ class Launcher { } destroyTmp() { + // close all opened files if (this.outFile) { this.fs.closeSync(this.outFile); delete this.outFile; } - // Only clean up the tmp dir if we created it. - if (this.userDataDir === undefined || this.opts.userDataDir !== undefined) { - return; - } - if (this.errFile) { this.fs.closeSync(this.errFile); delete this.errFile; } + // Only clean up the tmp dir if we created it. + if (this.userDataDir === undefined || this.opts.userDataDir !== undefined) { + return; + } + // backwards support for node v12 + v14.14+ // https://nodejs.org/api/deprecations.html#DEP0147 const rmSync = this.fs.rmSync || this.fs.rmdirSync;